-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16.py
More file actions
40 lines (34 loc) · 1.03 KB
/
day16.py
File metadata and controls
40 lines (34 loc) · 1.03 KB
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
#AoC Day 16 -- Permutation Prom
import collections, itertools
with open('day16.txt', 'r') as f:
file = f.read().strip().split(',')
def dance(pos, file):
for line in file:
first = line[0]
line = line[1:]
if first == 's':
num = int(line)
pos[:] = pos[-num:] + pos[:-num]
elif first == 'x':
a, b = map(int, line.split('/'))
pos[a], pos[b] = pos[b], pos[a]
elif first == 'p':
a, b = line.split('/')
ai = pos.index(a)
bi = pos.index(b)
pos[ai], pos[bi] = pos[bi], pos[ai]
return ''.join(pos)
def part1(file):
return ''.join(dance(list('abcdefghijklmnop'), file))
def part2(file):
s = 'abcdefghijklmnop'
pos = list(s)
seen = collections.deque([s])
for i in itertools.count(1):
res = dance(pos, file)
if res == s:
return seen[1_000_000_000 % i]
else:
seen.append(res)
print(part1(file))
print(part2(file))