-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.jl
82 lines (72 loc) · 1.9 KB
/
Day9.jl
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
module Day9
export solvePart1, solvePart2
numberOfPlayers = 418
lastMarblePoints = 71339
mutable struct Marble
next
prev
number::Int64
end
function indexCounterClockwise(ind, numPlaces, len)
i = ind - numPlaces
while i < 0
i += len
end
return i
end
function indexClockwise(ind, numPlaces, len)
i = ind + numPlaces
while i > len
i -= len
end
return i
end
function removeMarbleSevenPlacesCounterClockwise(current::Marble)
toRemove = current
for i in 1:7
toRemove = toRemove.prev
end
before = toRemove.prev
after = toRemove.next
before.next = after
after.prev = before
return toRemove
end
function winningElfScore(numberOfPlayers, lastNumberScore)
# Start with a marble whose previous and next is itself
firstMarble = Marble(nothing, nothing, 0)
firstMarble.next = firstMarble
firstMarble.prev = firstMarble
whoseTurn = 1
points = Dict()
current = firstMarble
for nextMarbleNumber in 1:lastNumberScore
if nextMarbleNumber % 23 == 0
removed = removeMarbleSevenPlacesCounterClockwise(current)
toGain = nextMarbleNumber + removed.number
points[whoseTurn] = get(points, whoseTurn, 0) + toGain
current = removed.next
else
before = current.next
after = before.next
toInsert = Marble(before, after, nextMarbleNumber)
before.next = toInsert
after.prev = toInsert
toInsert.prev = before
toInsert.next = after
current = toInsert
end
whoseTurn += 1
if whoseTurn > numberOfPlayers
whoseTurn = 1
end
end
return max(values(points)...)
end
function solvePart1()
winningElfScore(numberOfPlayers, lastMarblePoints)
end
function solvePart2()
winningElfScore(numberOfPlayers, lastMarblePoints*100)
end
end