-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12part1.py
executable file
·114 lines (91 loc) · 2.19 KB
/
day12part1.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
#!/usr/bin/env python3
""" https://adventofcode.com/2018/day/2 """
inputstr = """
initial state: #..#.#..##......###...###
...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #
"""
inputstr = """
initial state: ##.###.......#..#.##..#####...#...#######....##.##.##.##..#.#.##########...##.##..##.##...####..####
#.#.# => #
.##.. => .
#.#.. => .
..### => #
.#..# => #
..#.. => .
####. => #
###.. => #
#.... => .
.#.#. => #
....# => .
#...# => #
..#.# => #
#..#. => #
.#... => #
##..# => .
##... => .
#..## => .
.#.## => #
.##.# => .
#.##. => #
.#### => .
.###. => .
..##. => .
##.#. => .
...## => #
...#. => .
..... => .
##.## => .
###.# => #
##### => #
#.### => .
"""
import re, collections, itertools
Node = collections.namedtuple('Node', 'a children metadata')
regex = re.compile('^(\d+) players; last marble is worth (\d+) points')
def display_state(r, state):
print("%02d: (%04d)" % (r, sum(state)), end='')
for i in range(-40, 100):
print('#' if i in state else '.', end='')
print()
def main():
lines = inputstr.strip().splitlines()
initial_state = lines[0][15:]
rules = [ s.split(" => ") for s in lines[2:] ]
rules = [ (m, r) for m, r in rules if r == '#' ]
print("INITIAL", initial_state)
# Let's try a set of all of the potted indexes.
state = set( i for i, c in enumerate(initial_state) if c == '#' )
display_state(0, state)
last = 0
for r in range(1, 500000):
newstate = set()
# print("!!", min(state.keys()), max(state.keys()))
print("now=", sum(state), "last=", last, "diff=", (sum(state)-last))
if r > 200:
predicted = 23 * r + 434
assert predicted == sum(state)
last = sum(state)
for i in range(min(state)-4, max(state)+4):
s = "".join('#' if i+j in state else '.' for j in range(-2, 3))
for match, result in rules:
if match == s:
newstate.add(i)
break
state = newstate
display_state(r, state)
print(sum(state))
if __name__ == '__main__':
main()