-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd09.py
More file actions
95 lines (68 loc) · 2.34 KB
/
d09.py
File metadata and controls
95 lines (68 loc) · 2.34 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
import re
from typing import Self
from day import Day, register_day
@register_day(2018, 9)
class Day09(Day):
def part_one(self) -> str:
match = PATTERN.match(self.read_input())
players = int(match.group(1))
last_marble = int(match.group(2))
winner_points = self.__run_game(players, last_marble)
return str(winner_points)
# TODO: This is running very slow. Should try to optmize it.
def part_two(self) -> str:
match = PATTERN.match(self.read_input())
players = int(match.group(1))
last_marble = int(match.group(2)) * 100
winner_points = self.__run_game(players, last_marble)
return str(winner_points)
def __run_game(self, players: int, last_marble: int) -> int:
points = [0] * players
circle = Circle()
current_marble = 1
while current_marble <= last_marble:
for player in range(0, players):
award_points = circle.insert(current_marble)
points[player] += award_points
current_marble += 1
if current_marble > last_marble:
break
return max(points)
PATTERN = re.compile(r"(\d+) players; last marble is worth (\d+) points")
class Node:
value: int
previous: Self
next: Self
class Circle:
current_node: Node
def __init__(self) -> None:
root = Node()
root.value = 0
root.previous = root
root.next = root
self.current_node = root
def remove_node(self, node: Node):
temp_prev = node.previous
temp_next = node.next
temp_prev.next = temp_next
temp_next.previous = temp_prev
def insert(self, marble: int) -> int:
if marble % 23 == 0:
points = marble
extract_node = self.current_node
for _ in range(0, 7):
extract_node = extract_node.previous
points += extract_node.value
self.remove_node(extract_node)
self.current_node = extract_node.next
return points
node = Node()
node.value = marble
node_1 = self.current_node.next
node_2 = node_1.next
node.previous = node_1
node.next = node_2
node_1.next = node
node_2.previous = node
self.current_node = node
return 0