diff --git a/.gitignore b/.gitignore index 68fa3b0..fea2439 100644 --- a/.gitignore +++ b/.gitignore @@ -160,5 +160,5 @@ cython_debug/ #.idea/ **/*Zone.Identifier -**/*input* -cookies.txt \ No newline at end of file +**/input* +cookies.txt diff --git a/day5/example_input.txt b/day5/example_input.txt new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/day5/example_input.txt @@ -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 \ No newline at end of file diff --git a/day6/example_input.txt b/day6/example_input.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/day6/example_input.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/day7/example_input.txt b/day7/example_input.txt new file mode 100644 index 0000000..bf2815e --- /dev/null +++ b/day7/example_input.txt @@ -0,0 +1,5 @@ +32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483 \ No newline at end of file diff --git a/day8/example_input.txt b/day8/example_input.txt new file mode 100644 index 0000000..59e2d47 --- /dev/null +++ b/day8/example_input.txt @@ -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) \ No newline at end of file diff --git a/day9/common.py b/day9/common.py new file mode 100644 index 0000000..dec84a9 --- /dev/null +++ b/day9/common.py @@ -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 diff --git a/day9/example_input.txt b/day9/example_input.txt new file mode 100644 index 0000000..70c5595 --- /dev/null +++ b/day9/example_input.txt @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45 \ No newline at end of file diff --git a/day9/part1.py b/day9/part1.py new file mode 100755 index 0000000..5c4f18b --- /dev/null +++ b/day9/part1.py @@ -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)) diff --git a/day9/part2.py b/day9/part2.py new file mode 100755 index 0000000..4072df3 --- /dev/null +++ b/day9/part2.py @@ -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)) diff --git a/day9/test.py b/day9/test.py new file mode 100644 index 0000000..cb753b0 --- /dev/null +++ b/day9/test.py @@ -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()