Day 9 Completed
put example_input.txt back in repos, for unittest purposes
This commit is contained in:
parent
7b73d8402a
commit
f641fbac07
2
.gitignore
vendored
2
.gitignore
vendored
@ -160,5 +160,5 @@ cython_debug/
|
|||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
**/*Zone.Identifier
|
**/*Zone.Identifier
|
||||||
**/*input*
|
**/input*
|
||||||
cookies.txt
|
cookies.txt
|
33
day5/example_input.txt
Normal file
33
day5/example_input.txt
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
seeds: 79 14 55 13
|
||||||
|
|
||||||
|
seed-to-soil map:
|
||||||
|
50 98 2
|
||||||
|
52 50 48
|
||||||
|
|
||||||
|
soil-to-fertilizer map:
|
||||||
|
0 15 37
|
||||||
|
37 52 2
|
||||||
|
39 0 15
|
||||||
|
|
||||||
|
fertilizer-to-water map:
|
||||||
|
49 53 8
|
||||||
|
0 11 42
|
||||||
|
42 0 7
|
||||||
|
57 7 4
|
||||||
|
|
||||||
|
water-to-light map:
|
||||||
|
88 18 7
|
||||||
|
18 25 70
|
||||||
|
|
||||||
|
light-to-temperature map:
|
||||||
|
45 77 23
|
||||||
|
81 45 19
|
||||||
|
68 64 13
|
||||||
|
|
||||||
|
temperature-to-humidity map:
|
||||||
|
0 69 1
|
||||||
|
1 0 69
|
||||||
|
|
||||||
|
humidity-to-location map:
|
||||||
|
60 56 37
|
||||||
|
56 93 4
|
2
day6/example_input.txt
Normal file
2
day6/example_input.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
5
day7/example_input.txt
Normal file
5
day7/example_input.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KK677 28
|
||||||
|
KTJJT 220
|
||||||
|
QQQJA 483
|
9
day8/example_input.txt
Normal file
9
day8/example_input.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
RL
|
||||||
|
|
||||||
|
AAA = (BBB, CCC)
|
||||||
|
BBB = (DDD, EEE)
|
||||||
|
CCC = (ZZZ, GGG)
|
||||||
|
DDD = (DDD, DDD)
|
||||||
|
EEE = (EEE, EEE)
|
||||||
|
GGG = (GGG, GGG)
|
||||||
|
ZZZ = (ZZZ, ZZZ)
|
82
day9/common.py
Normal file
82
day9/common.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import sys
|
||||||
|
from typing import List, Self
|
||||||
|
|
||||||
|
|
||||||
|
class color:
|
||||||
|
PURPLE = "\033[95m"
|
||||||
|
CYAN = "\033[96m"
|
||||||
|
DARKCYAN = "\033[36m"
|
||||||
|
BLUE = "\033[94m"
|
||||||
|
GREEN = "\033[92m"
|
||||||
|
YELLOW = "\033[93m"
|
||||||
|
RED = "\033[91m"
|
||||||
|
BOLD = "\033[1m"
|
||||||
|
UNDERLINE = "\033[4m"
|
||||||
|
END = "\033[0m"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(init=False, eq=False)
|
||||||
|
class OASISLine:
|
||||||
|
sequence: List[int]
|
||||||
|
reduce_map: List[List[int]]
|
||||||
|
|
||||||
|
def __init__(self, input_line: str):
|
||||||
|
self.sequence = list(map(lambda i: int(i), input_line.split(" ")))
|
||||||
|
self.reduce_map = [
|
||||||
|
self.sequence,
|
||||||
|
]
|
||||||
|
|
||||||
|
def reduce(self) -> Self:
|
||||||
|
# sys.stderr.write("=".join(["" for i in range(40)])+"\n")
|
||||||
|
line = self.sequence
|
||||||
|
# sys.stderr.write(str(line)+"\n")
|
||||||
|
while not all(map(lambda i: i == 0, line)):
|
||||||
|
new_line = []
|
||||||
|
for idx, number in enumerate(line):
|
||||||
|
if idx > 0:
|
||||||
|
new_line.append(number - line[idx - 1])
|
||||||
|
line = new_line
|
||||||
|
self.reduce_map.append(line)
|
||||||
|
# sys.stderr.write(str(line)+"\n")
|
||||||
|
# sys.stderr.write("=".join(["" for i in range(40)])+"\n")
|
||||||
|
return self
|
||||||
|
|
||||||
|
def extrapolate(self):
|
||||||
|
# sys.stderr.write("\n" + color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
|
||||||
|
next_increment = 0
|
||||||
|
for line in reversed(self.reduce_map):
|
||||||
|
# sys.stderr.write(str(line))
|
||||||
|
if all(map(lambda i: i == line[0], line)):
|
||||||
|
next_increment = line[0]
|
||||||
|
else:
|
||||||
|
next_increment = line[len(line) - 1] + next_increment
|
||||||
|
# sys.stderr.write(color.BOLD + f" {next_increment}" + color.END + "\n")
|
||||||
|
# sys.stderr.write(color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
|
||||||
|
return next_increment
|
||||||
|
|
||||||
|
def extrapolate_back(self):
|
||||||
|
sys.stderr.write("\n" + color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
|
||||||
|
next_increment = 0
|
||||||
|
for line in reversed(self.reduce_map):
|
||||||
|
if all(map(lambda i: i == line[0], line)):
|
||||||
|
next_increment = line[0]
|
||||||
|
else:
|
||||||
|
next_increment = line[0] - next_increment
|
||||||
|
sys.stderr.write(color.BOLD + f"{next_increment} " + color.END + str(line) + "\n")
|
||||||
|
sys.stderr.write(color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
|
||||||
|
return next_increment
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def parse(cls, input_file: str) -> List[OASISLine]:
|
||||||
|
# sys.stderr.write("\n")
|
||||||
|
the_list = []
|
||||||
|
with open(input_file) as input:
|
||||||
|
while line := input.readline():
|
||||||
|
line = line.rstrip("\n").rstrip()
|
||||||
|
# sys.stderr.write(color.RED + f'parsing "{line}"' + color.END + "\n")
|
||||||
|
the_list.append(OASISLine(line))
|
||||||
|
# sys.stderr.write(color.GREEN + f"parsed {the_list[len(the_list) - 1]}" + color.END + "\n")
|
||||||
|
return the_list
|
3
day9/example_input.txt
Normal file
3
day9/example_input.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
0 3 6 9 12 15
|
||||||
|
1 3 6 10 15 21
|
||||||
|
10 13 16 21 30 45
|
7
day9/part1.py
Executable file
7
day9/part1.py
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
from common import OASISLine
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
analysis = OASISLine.parse("input.txt")
|
||||||
|
extrapolation = map(lambda o: o.reduce().extrapolate(), analysis)
|
||||||
|
print(sum(extrapolation))
|
7
day9/part2.py
Executable file
7
day9/part2.py
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
from common import OASISLine
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
analysis = OASISLine.parse("input.txt")
|
||||||
|
extrapolation = map(lambda o: o.reduce().extrapolate_back(), analysis)
|
||||||
|
print(sum(extrapolation))
|
17
day9/test.py
Normal file
17
day9/test.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import unittest
|
||||||
|
from common import OASISLine
|
||||||
|
|
||||||
|
|
||||||
|
class Day9Tests(unittest.TestCase):
|
||||||
|
def test_part1(self):
|
||||||
|
analysis = OASISLine.parse("example_input.txt")
|
||||||
|
extrapolation = map(lambda o: o.reduce().extrapolate(), analysis)
|
||||||
|
self.assertEqual(sum(extrapolation), 114)
|
||||||
|
|
||||||
|
def test_part2(self):
|
||||||
|
analysis = OASISLine.parse("example_input.txt")
|
||||||
|
extrapolation = map(lambda o: o.reduce().extrapolate_back(), analysis)
|
||||||
|
self.assertEqual(sum(extrapolation), 2)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user