blob: a688376a13167ddcebec2719041d83b576e04d08 (
plain) (
blame)
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import sys
sys.path.append("../common")
import aoc
lines = aoc.data.split("\n")
istate = lines[0].split(": ")[1]
rules = [l.split(" => ") for l in lines[2:] if l != ""]
rules = [r[0] for r in rules if r[1] == "#"]
def next_gen(pots):
pmin = min(pots)
pmax = max(pots)
npots = list()
for i in range(pmin-4, pmax+1):
for r in rules:
match = True
for j in range(5):
if (r[j] == "#") != ((i+j) in pots):
match = False
break
if match:
npots.append(i+2)
break
return npots
def get_pots():
pots = list()
for i in range(len(istate)):
if istate[i] == "#":
pots.append(i)
return pots
def solve1(args):
pots = get_pots()
for i in range(20):
pots = next_gen(pots)
return sum(pots)
def solve2(args):
pots = get_pots()
psum = sum(pots)
pdif = None
i = 0
while (True):
pots = next_gen(pots)
i += 1
csum = sum(pots)
if pdif == csum - psum:
return csum + pdif * (50000000000 - 164)
pdif = csum - psum
psum = csum
aoc.run(solve1, solve2, sols=[1987, 1150000000358])
|