diff --git a/2018/day12.py b/2018/day12.py new file mode 100644 index 0000000..158d31d --- /dev/null +++ b/2018/day12.py @@ -0,0 +1,34 @@ +import fileinput + + +INITIAL = None +RULES = {} +GENS = 200 # 200 generations is safe for the automata to converge +TARGET_GEN = 50000000000 + +for i, line in enumerate(fileinput.input()): + if i == 0: + INITIAL = line.split()[-1] + elif i >= 2: + a, _, b = line.split() + RULES[a] = b + + +state = ['.'] * GENS + list(INITIAL) + ['.'] * GENS +pot_sums = [] + +for it in range(GENS): + amt = sum(i - GENS for i, p in enumerate(state) if p == '#') + pot_sums.append(amt) + + next_state = ['.'] * len(state) + for i in range(len(state) - 2): + next_state[i + 2] = RULES.get(''.join(state[i:i+5]), '.') + + state = next_state + +print "Sum of plants after 20 generations:", pot_sums[20] + +growth_per_gen = pot_sums[-1] - pot_sums[-2] +remaining_gens = TARGET_GEN - GENS + 1 +print "Sum after 50000000000 generations:", growth_per_gen * remaining_gens + pot_sums[-1] diff --git a/2018/inputs/12.txt b/2018/inputs/12.txt new file mode 100644 index 0000000..da50317 --- /dev/null +++ b/2018/inputs/12.txt @@ -0,0 +1,34 @@ +initial state: #...####.##..####..#.##....##...###.##.#..######..#..#..###..##.#.###.#####.##.#.#.#.##....#..#..#.. + +...## => . +...#. => # +....# => . +###.# => # +..... => . +..#.. => . +#.#.# => . +#..#. => . +#...# => . +##... => . +.#.#. => # +.#..# => . +.###. => . +#..## => # +..#.# => # +.#### => # +##..# => # +##.#. => # +.#... => # +#.#.. => . +##### => . +###.. => # +.##.# => . +#.##. => . +..### => . +.#.## => # +..##. => # +#.### => . +.##.. => # +##.## => . +#.... => . +####. => # diff --git a/2018/outputs/12.txt b/2018/outputs/12.txt new file mode 100644 index 0000000..1c3e8d5 --- /dev/null +++ b/2018/outputs/12.txt @@ -0,0 +1,2 @@ +3410 +4000000001480