-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
84 lines (65 loc) · 1.89 KB
/
day9.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
83
84
class Marble:
def __init__(self, val):
self.val = val
self.next = self
self.previous = self
def play(self, marble):
if marble.val % 23 != 0:
one = self.next
two = self.next.next
one.next = marble
two.previous = marble
marble.next = two
marble.previous = one
return marble, 0
else:
current = self
for i in range(0, 7):
current = current.previous
current.next.previous = current.previous
current.previous.next = current.next
return current.next, current.val + marble.val
def createArr(x, y = 0):
arr = []
for i in range(x):
arr.append(0)
return arr
def part1(inputData = "input/input9.txt"):
with open(inputData) as f:
data = f.read().split()
playersN = int(data[0])
last = int(data[6])
marble = Marble(0)
players = createArr(playersN)
current = 0
for i in range(1, last):
marble, score = marble.play(Marble(i))
players[current] += score
current += 1
if current == playersN:
current = 0
if marble.val == last:
break
print("day 9, part 1:", max(players))
def part2(inputData = "input/input9.txt"):
with open(inputData) as f:
data = f.read().split()
playersN = int(data[0])
last = int(data[6]) * 100
marble = Marble(0)
players = createArr(playersN)
current = 0
for i in range(1, last):
marble, score = marble.play(Marble(i))
players[current] += score
current += 1
if current == playersN:
current = 0
if marble.val == last:
break
print("day 9, part 2:", max(players))
def main(inputData = "input/input9.txt"):
part1(inputData)
part2(inputData)
if __name__ == "__main__":
main()