-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
82 lines (63 loc) · 1.95 KB
/
day20.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
from __future__ import annotations
from dataclasses import dataclass
from .shared import Solution
DECRYPTION_KEY = 811589153
@dataclass
class Node:
val: int
prev: Node | None = None
next: Node | None = None
def main(input_: list[str]) -> Solution:
head, node_list = build_linked_list(input_)
for node in node_list:
mix(node, len(node_list))
part1 = score(head)
head, node_list = build_linked_list(input_, apply_key=True)
for node in node_list * 10:
mix(node, len(node_list))
part2 = score(head)
return Solution(part1, part2)
def build_linked_list(lines: list[str], apply_key: bool = False) -> (Node, list[Node]):
nodes = []
for val in map(int, lines):
if apply_key:
val = val * DECRYPTION_KEY
nodes.append(Node(val))
for idx, node in enumerate(nodes):
prev_idx = (idx - 1) % len(nodes)
next_idx = (idx + 1) % len(nodes)
node.prev = nodes[prev_idx]
node.next = nodes[next_idx]
return find_zero(nodes[0]), nodes
def find_zero(head: Node) -> Node:
if head.val == 0:
return head
current = head.next
while current != head:
if current.val == 0:
return current
current = current.next
def mix(node: Node, node_count: int):
steps = node.val % (node_count - 1)
if node.val == 0 or steps == 0:
return
# Remove node
node.prev.next = node.next
node.next.prev = node.prev
# Find insert position
target = node.prev
for _ in range(steps):
target = target.next
# Insert node
node.next = target.next
node.prev = target
target.next.prev = node
target.next = node
def score(head: Node) -> int:
total = 0
current = head.next
for i in range(1, 3_001):
if i % 1000 == 0:
total += current.val
current = current.next
return total