adventofcode-2018/12/solve01.py
2018-12-18 00:00:09 +01:00

55 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python
import re
RE_INIT=re.compile(r'^initial state: ([\#\.]+)$')
RE_RULE=re.compile(r'^([\#|\.]+) => ([\'#\.])$')
rules = dict()
start = None
left_index = 0
def read_input():
with open('input.txt', 'r') as f:
for line in f:
yield line.strip()
def run_generation(pots, left_index = 0):
result = ""
if not pots.startswith('.....'):
pots = '....' + pots
left_index -= 4
if not pots.endswith('....'):
pots = pots + '.....'
for pos in range(2, len(pots) -2):
r_key = pots[pos-2:pos+3]
if r_key not in rules:
rules[r_key] = '.'
result = result + rules[r_key]
return (result, left_index+2)
for line in read_input():
m = RE_INIT.match(line)
if m:
start = m.group(1)
continue
m = RE_RULE.match(line)
if m:
rules[m.group(1)] = m.group(2)
continue
if len(line) == 0:
continue
raise Execption('unparseable input')
pots = start
for g in range(20):
(pots, left_index) = run_generation(pots, left_index)
print("(g:{})(s:{}): {}".format(g+1, left_index,pots))
print(sum([k+left_index for k,v in enumerate(pots) if v == '#']))