-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday12.py
executable file
·116 lines (81 loc) · 1.97 KB
/
day12.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
from utils import advent
import heapq
advent.setup(2018, 12)
fin = advent.get_input()
##################################################
def simulate(days=20):
seen = set()
sseen = False
pseen = False
for d in range(1, days+1):
alive = set()
for i in range(2, len(pots)-2):
k = ''.join(pots[i-2:i+3])
# print(i-offset, k, end='')
if k in info:
# print(':', info[k] + ':', end=' ')
# print(pots[i], '->', end=' ')
if info[k] == '#':
alive.add(i)
# print(pots[i], end='')
# print()
for i in range(len(pots)):
if i in alive:
pots[i] = '#'
else:
pots[i] = '.'
state = ''.join(pots)
idx = state.find('#')
state = state[idx:]
state = state[:state.rfind('#')+1]
# print(''.join(pots[offset-10:offset+200]))
if state in seen:
print('SEEN', d, idx)
print(state)
sseen = True
if pseen:
break
pseen = sseen
seen.add(state)
# print(d, ':', ''.join(pots))
# break
return d, state
offset = 1000
pots = list(fin.readline().replace('initial state:', '').strip())
print(''.join(pots))
pots = ['.'] * offset + pots + ['.'] * offset
fin.readline()
strings = list(map(str.strip, fin))
info = {}
for s in strings:
k, v = s.replace(' ', '').split('=>')
info[k] = v
print(info)
## Part 1:
# endsim, endstate = simulate()
# print('sim ended on day:', endsim)
# print(endstate)
# alive = sum(c == '#' for c in pots)
# print('alive:', alive)
# ans = 0
# for i in range(len(pots)):
# if pots[i] == '#':
# ans += i - offset
# advent.submit_answer(1, ans)
# day 91 ans 2391
# day 92 ans 2412
## Part 2 (only works if part 1 is commented)
endsim, endstate = simulate(50000000000)
print('sim ended on day:', endsim)
print(endstate)
alive = sum(c == '#' for c in pots)
print('alive:', alive)
remaining = 50000000000 - endsim
print(remaining, 'days left to sim')
ans = 0
for i in range(len(pots)):
if pots[i] == '#':
ans += i - offset
ans += alive*remaining
advent.submit_answer(2, ans)