-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.py
86 lines (63 loc) · 1.86 KB
/
21.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
from lib import *
input = read_input(2017, 21)
lines = input.splitlines()
def rotate(p):
return tuple("".join(p[j][i] for j in range(len(p))) for i in reversed(range(len(p[0]))))
rules = {}
pattern = ".#.", "..#", "###"
for line in lines:
a, b = [tuple(k.split("/")) for k in line.split(" => ")]
rules[a] = b
def find_rule(p):
for _ in range(4):
if p in rules:
return rules[p]
p = rotate(p)
p = tuple(reversed(p))
for _ in range(4):
if p in rules:
return rules[p]
p = rotate(p)
def apply():
k = 3 if len(pattern) % 2 else 2
out = []
for i in range(len(pattern) // k):
b = []
for j in range(len(pattern) // k):
new = find_rule(tuple("".join(pattern[i * k + y][j * k + x] for x in range(k)) for y in range(k)))
b.append(new)
for line in zip(*b):
out.append("".join(line))
return tuple(out)
for _ in range(5):
pattern = apply()
print(sum(line.count("#") for line in pattern))
rules = {}
pattern = ".#.", "..#", "###"
for line in lines:
a, b = [tuple(k.split("/")) for k in line.split(" => ")]
rules[a] = b
def find_rule(p):
for _ in range(4):
if p in rules:
return rules[p]
p = rotate(p)
p = tuple(reversed(p))
for _ in range(4):
if p in rules:
return rules[p]
p = rotate(p)
def apply():
k = 3 if len(pattern) % 2 else 2
out = []
for i in range(len(pattern) // k):
b = []
for j in range(len(pattern) // k):
new = find_rule(tuple("".join(pattern[i * k + y][j * k + x] for x in range(k)) for y in range(k)))
b.append(new)
for line in zip(*b):
out.append("".join(line))
return tuple(out)
for _ in range(18):
pattern = apply()
print(sum(line.count("#") for line in pattern))