[WIP] Day 12 2018

This commit is contained in:
Luke Hubmayer-Werner 2018-12-12 16:41:25 +10:30
parent 2c6235013e
commit 55250c9d64
3 changed files with 84 additions and 0 deletions

34
2018/day12-input Normal file
View File

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

16
2018/day12-inputtest Normal file
View File

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

34
2018/day12.py Normal file
View File

@ -0,0 +1,34 @@
with open('day12-input', 'r') as file:
data = [l.strip('\n') for l in file]
import numpy as np
import re
# numbers = np.array([[int(s) for s in re.findall(r'-?\d+', d)] for d in data], dtype=np.int64)
max_gens = 50000000000
initial_state = np.array([0 if s=='.' else 1 for s in data[0].split()[-1]])
rules = [np.array([0 if s=='.' else 1 for s in d.split()[0]]+[0 if s=='.' else 1 for s in d.split()[-1]]) for d in data[2:]]
l = len(initial_state)
state = np.zeros(l*20, dtype=np.int64)
state[l*10:l*11] = initial_state
last_state = state.copy()
pots = np.arange(-l*10, l*10)
for gen in range(max_gens):
print(''.join(['#' if s else '.' for s in state[l*10-2:l*11+5]]), gen, max_gens-gen)
for plant in range(2, len(state)-2):
s = last_state[plant-2:plant+3]
matched = False
for rule in rules:
if np.array_equal(rule[:-1], s):
state[plant] = rule[-1]
# print('Matched!', rule)
matched = True
break
if not matched:
state[plant] = 0
last_state[:] = state[:]
print(pots[state > 0].sum())