-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday16.py
51 lines (40 loc) · 1.27 KB
/
day16.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
from collections import deque
with open('./inputs/16.txt') as f:
instructions = f.readline().split(',')
def clean_up(instructions):
result = []
for instr in instructions:
first = instr[0]
if first == 's':
result.append((first, int(instr[1:])))
elif first == 'x':
a, b = map(int, instr[1:].split('/'))
result.append((first, a, b))
else:
result.append((first, instr[1], instr[-1]))
return result
def dance(dancers):
dancers = deque(dancers)
for first, *rest in cleaned:
if first == 's':
dancers.rotate(rest[0])
elif first == 'x':
a, b = rest
dancers[a], dancers[b] = dancers[b], dancers[a]
elif first == 'p':
x, y = rest
a = dancers.index(x)
b = dancers.index(y)
dancers[a], dancers[b] = y, x
return ''.join(dancers)
def long_dance(dancers, iterations=1_000_000_000):
seen = [dancers]
for i in range(1, iterations):
dancers = dance(dancers)
if dancers == programs:
return seen[iterations % i]
seen.append(dancers)
programs = 'abcdefghijklmnop'
cleaned = clean_up(instructions)
print(dance(programs))
print(long_dance(programs))