-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
55 lines (51 loc) · 1.5 KB
/
__init__.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
from itertools import accumulate, islice, repeat
from typing import List
def part1(lines: List[str]):
init = [int(i) - 1 for i in lines[0]]
init_offset = init[1:] + init[:1]
cups = dict(zip(init, init_offset))
N = len(init)
curr = init[0]
for _ in range(100):
rem1 = cups[curr]
rem2 = cups[rem1]
rem3 = cups[rem2]
all_rem = {rem1, rem2, rem3}
cups[curr] = cups[rem3]
dest = next(
(curr - i) % N for i in range(1, 5) if (curr - i) % N not in all_rem
)
temp = cups[dest]
cups[dest] = rem1
cups[rem3] = temp
curr = cups[curr]
result = "".join(
str(i + 1)
for i in islice(
accumulate(repeat(None), lambda x, _: cups[x], initial=cups[0]), N - 1
)
)
return result
def part2(lines: List[str]):
init = [int(i) - 1 for i in lines[0]]
init.extend(range(len(init), 1_000_000))
init_offset = init[1:] + init[:1]
cups = dict(zip(init, init_offset))
N = len(init)
curr = init[0]
for _ in range(10_000_000):
rem1 = cups[curr]
rem2 = cups[rem1]
rem3 = cups[rem2]
all_rem = {rem1, rem2, rem3}
cups[curr] = cups[rem3]
dest = next(
(curr - i) % N for i in range(1, 5) if (curr - i) % N not in all_rem
)
temp = cups[dest]
cups[dest] = rem1
cups[rem3] = temp
curr = cups[curr]
next1 = cups[0]
next2 = cups[next1]
return (next1 + 1) * (next2 + 1)