-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
45 lines (30 loc) · 1.01 KB
/
day14.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
import time
def load(file):
with open(file) as f:
return [row.strip() for row in f]
def points(p):
return sum(row.count('O') * (len(p) - i) for i, row in enumerate(p))
def roll(p):
p = [''.join(col) for col in zip(*p)]
p = ['#'.join(''.join(sorted(sub, reverse=True)) for sub in row.split('#')) for row in p]
return [''.join(col) for col in zip(*p)]
def cycle(p):
for _ in range(4):
p = [''.join(col) for col in zip(*p)]
p = ['#'.join(''.join(sorted(sub, reverse=True)) for sub in row.split('#')) for row in p]
p = [row[::-1] for row in p]
return p
def solve(p):
part1 = points(roll(p))
pattern = [p]
while True:
p = cycle(p)
if p in pattern: break
pattern.append(p)
offset = pattern.index(p)
cycle_length = len(pattern) - offset
part2 = points(pattern[(1_000_000_000 - offset) % cycle_length + offset])
return part1, part2
time_start = time.perf_counter()
print(f'Part 1: {solve(load("day14.txt"))}')
print(f'Solved in {time.perf_counter()-time_start:.5f} Sec.')