From 643ad7fd8d5380706b3617e97df4a3baa5902cea Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 12 Nov 2023 18:58:28 +0100 Subject: [PATCH] day12 --- answer_cache.json | 14 ++++++++ day12.py | 81 +++++++++++++++++++++++++++++++++++++++++++++ inputs/input12 | 34 +++++++++++++++++++ inputs/input12_test | 16 +++++++++ 4 files changed, 145 insertions(+) create mode 100644 day12.py create mode 100644 inputs/input12 create mode 100644 inputs/input12_test diff --git a/answer_cache.json b/answer_cache.json index 0752fb5..10307be 100644 --- a/answer_cache.json +++ b/answer_cache.json @@ -103,5 +103,19 @@ "wrong": [], "correct": "90,214,15" } + }, + "12": { + "1": { + "wrong": [], + "correct": 3494 + }, + "2": { + "wrong": [ + 8211, + 2852454, + 2850000002511 + ], + "correct": 2850000002454 + } } } \ No newline at end of file diff --git a/day12.py b/day12.py new file mode 100644 index 0000000..c2a2c76 --- /dev/null +++ b/day12.py @@ -0,0 +1,81 @@ +from tools.aoc import AOCDay +from typing import Any + + +def simulate_generation(state: str, transform: dict, shift: int) -> (str, int): + while not state.startswith("....."): + state = '.' + state + shift += 1 + + while not state.endswith('.....'): + state += '.' + + new_state = '..' + for x in range(len(state) - 5): + key = state[x:x+5] + if key not in transform: + new_state += '.' + else: + new_state += transform[key] + + while new_state.startswith('.'): + new_state = new_state[1:] + shift -= 1 + + return new_state, shift + + +def get_pot_value(state: str, shift: int) -> int: + sum = 0 + for i, x in enumerate(state): + if x == '#': + sum += i - shift + + return sum + + +class Day(AOCDay): + inputs = [ + [ + (325, "input12_test"), + (3494, "input12"), + ], + [ + (2850000002454, "input12"), + ] + ] + + def parse_input(self) -> (str, dict): + state = self.getInput()[0].split(": ")[1] + + transform = {} + for line in self.getInput()[2:]: + k, v = line.split(" => ") + transform[k] = v + + return state, transform + + def part1(self) -> Any: + state, transform = self.parse_input() + shift = 0 + for x in range(20): + state, shift = simulate_generation(state, transform, shift) + + return get_pot_value(state, shift) + + def part2(self) -> Any: + cache = {} + num_gens = 50_000_000_000 + state, transform = self.parse_input() + cache[state] = (0, 0) + shift = 0 + for x in range(1, num_gens + 1): + state, shift = simulate_generation(state, transform, shift) + if state in cache: + return get_pot_value(state, (-num_gens + 11)) + cache[state] = (x, shift) + + +if __name__ == '__main__': + day = Day(2018, 12) + day.run(verbose=True) diff --git a/inputs/input12 b/inputs/input12 new file mode 100644 index 0000000..67d1e34 --- /dev/null +++ b/inputs/input12 @@ -0,0 +1,34 @@ +initial state: #.#####.#.#.####.####.#.#...#.......##..##.#.#.#.###..#.....#.####..#.#######.#....####.#....##....# + +##.## => . +#.#.. => . +..... => . +##..# => # +###.. => # +.##.# => . +..#.. => # +##.#. => # +.##.. => . +#..#. => . +###.# => # +.#### => # +.#.## => . +#.##. => # +.###. => # +##### => . +..##. => . +#.#.# => . +...#. => # +..### => . +.#.#. => # +.#... => # +##... => # +.#..# => # +#.### => # +#..## => # +....# => . +####. => . +#...# => # +#.... => . +...## => . +..#.# => # diff --git a/inputs/input12_test b/inputs/input12_test new file mode 100644 index 0000000..8335d88 --- /dev/null +++ b/inputs/input12_test @@ -0,0 +1,16 @@ +initial state: #..#.#..##......###...### + +...## => # +..#.. => # +.#... => # +.#.#. => # +.#.## => # +.##.. => # +.#### => # +#.#.# => # +#.### => # +##.#. => # +##.## => # +###.. => # +###.# => # +####. => # \ No newline at end of file