Compare commits

...

2 Commits

Author SHA1 Message Date
cdaa539b79 Refinement in tests Output
I use logging.debug, now
2023-12-09 10:21:55 +01:00
f641fbac07 Day 9 Completed
put example_input.txt back in repos, for unittest purposes
2023-12-09 09:35:56 +01:00
10 changed files with 171 additions and 2 deletions

4
.gitignore vendored
View File

@ -160,5 +160,5 @@ cython_debug/
#.idea/
**/*Zone.Identifier
**/*input*
cookies.txt
**/input*
cookies.txt

33
day5/example_input.txt Normal file
View 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
View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

5
day7/example_input.txt Normal file
View File

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

9
day8/example_input.txt Normal file
View 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
View File

@ -0,0 +1,82 @@
from __future__ import annotations
from dataclasses import dataclass
import logging
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:
logging.debug(color.RED + f"reducing {self.sequence}" + color.END)
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
line = self.sequence
logging.debug(str(line))
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)
logging.debug(str(line))
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
return self
def extrapolate(self):
logging.debug(color.RED + f"extrapolating {self.sequence}" + color.END)
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
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[len(line) - 1] + next_increment
logging.debug(str(line) + " " + color.BOLD + color.GREEN + f" {next_increment}" + color.END + "\n")
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
return next_increment
def extrapolate_back(self):
logging.debug(color.RED + f"extrapolating back {self.sequence}" + color.END)
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
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
logging.debug(color.BOLD + color.GREEN + f"{next_increment} " + color.END + str(line))
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
return next_increment
@classmethod
def parse(cls, input_file: str) -> List[OASISLine]:
the_list = []
with open(input_file) as input:
while line := input.readline():
line = line.rstrip("\n").rstrip()
logging.debug(color.RED + f'parsing "{line}"' + color.END)
the_list.append(OASISLine(line))
logging.debug(color.GREEN + f"parsed {the_list[len(the_list) - 1].sequence}" + color.END)
return the_list

3
day9/example_input.txt Normal file
View 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
View 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
View 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))

21
day9/test.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3.11
import unittest
from common import OASISLine
import logging
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__":
logging.basicConfig(level=logging.DEBUG)
unittest.main(verbosity=2)