My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2018/12

+70
+34
2018/day12.py
··· 1 + import fileinput 2 + 3 + 4 + INITIAL = None 5 + RULES = {} 6 + GENS = 200 # 200 generations is safe for the automata to converge 7 + TARGET_GEN = 50000000000 8 + 9 + for i, line in enumerate(fileinput.input()): 10 + if i == 0: 11 + INITIAL = line.split()[-1] 12 + elif i >= 2: 13 + a, _, b = line.split() 14 + RULES[a] = b 15 + 16 + 17 + state = ['.'] * GENS + list(INITIAL) + ['.'] * GENS 18 + pot_sums = [] 19 + 20 + for it in range(GENS): 21 + amt = sum(i - GENS for i, p in enumerate(state) if p == '#') 22 + pot_sums.append(amt) 23 + 24 + next_state = ['.'] * len(state) 25 + for i in range(len(state) - 2): 26 + next_state[i + 2] = RULES.get(''.join(state[i:i+5]), '.') 27 + 28 + state = next_state 29 + 30 + print "Sum of plants after 20 generations:", pot_sums[20] 31 + 32 + growth_per_gen = pot_sums[-1] - pot_sums[-2] 33 + remaining_gens = TARGET_GEN - GENS + 1 34 + print "Sum after 50000000000 generations:", growth_per_gen * remaining_gens + pot_sums[-1]
+34
2018/inputs/12.txt
··· 1 + initial state: #...####.##..####..#.##....##...###.##.#..######..#..#..###..##.#.###.#####.##.#.#.#.##....#..#..#.. 2 + 3 + ...## => . 4 + ...#. => # 5 + ....# => . 6 + ###.# => # 7 + ..... => . 8 + ..#.. => . 9 + #.#.# => . 10 + #..#. => . 11 + #...# => . 12 + ##... => . 13 + .#.#. => # 14 + .#..# => . 15 + .###. => . 16 + #..## => # 17 + ..#.# => # 18 + .#### => # 19 + ##..# => # 20 + ##.#. => # 21 + .#... => # 22 + #.#.. => . 23 + ##### => . 24 + ###.. => # 25 + .##.# => . 26 + #.##. => . 27 + ..### => . 28 + .#.## => # 29 + ..##. => # 30 + #.### => . 31 + .##.. => # 32 + ##.## => . 33 + #.... => . 34 + ####. => #
+2
2018/outputs/12.txt
··· 1 + 3410 2 + 4000000001480