This commit is contained in:
Stefan Harmuth 2023-11-12 18:58:28 +01:00
parent e8b37bdddb
commit 643ad7fd8d
4 changed files with 145 additions and 0 deletions

View File

@ -103,5 +103,19 @@
"wrong": [],
"correct": "90,214,15"
}
},
"12": {
"1": {
"wrong": [],
"correct": 3494
},
"2": {
"wrong": [
8211,
2852454,
2850000002511
],
"correct": 2850000002454
}
}
}

81
day12.py Normal file
View File

@ -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)

34
inputs/input12 Normal file
View File

@ -0,0 +1,34 @@
initial state: #.#####.#.#.####.####.#.#...#.......##..##.#.#.#.###..#.....#.####..#.#######.#....####.#....##....#
##.## => .
#.#.. => .
..... => .
##..# => #
###.. => #
.##.# => .
..#.. => #
##.#. => #
.##.. => .
#..#. => .
###.# => #
.#### => #
.#.## => .
#.##. => #
.###. => #
##### => .
..##. => .
#.#.# => .
...#. => #
..### => .
.#.#. => #
.#... => #
##... => #
.#..# => #
#.### => #
#..## => #
....# => .
####. => .
#...# => #
#.... => .
...## => .
..#.# => #

16
inputs/input12_test Normal file
View File

@ -0,0 +1,16 @@
initial state: #..#.#..##......###...###
...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #