-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9_part1.jakt
96 lines (72 loc) · 2 KB
/
day9_part1.jakt
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
96
/// Expect:
/// - output: "424639\n"
import reader
import utility
class Marble {
public previous: Marble?
public next: Marble?
public value: i32
}
function add_marble(mut current: Marble, value: i32) throws -> Marble {
for i in 0..2 {
current = current.next!
}
let new_marble = Marble(
previous: current.previous
next: current
value
)
current.previous!.next = new_marble
current.previous = new_marble
return new_marble
}
function player_score(mut current: Marble) throws -> (Marble, i32) {
for i in 0..7 {
current = current.previous!
}
let score = current.value
mut previous = current.previous
previous!.next = current.next
mut next = current.next
next!.previous = previous
return (current.next!, score)
}
function main() {
let values = reader::lines_string(day: 9)[0].split(' ')
let players = values[0].to_int()! as !usize
let marbles = values[6].to_int()!
mut player_scores: [usize:i32] = [:]
mut current = Marble(
previous: None,
next: None
value: 1
)
current.previous = current
current.next = current
mut current_player = 2uz
for marble in 1..(marbles + 1) {
if marble % 23 == 0 {
let update = player_score(current)
current = update.0
if not player_scores.contains(current_player) {
player_scores[current_player] = 0
}
player_scores[current_player] += update.1 + marble
} else {
current = add_marble(current, value: marble)
}
current_player = (current_player + 1) % (players + 1)
if current_player == 0 {
current_player++
}
}
mut high_score_player = 0uz
mut high_score = 0i32
for (player, score) in player_scores.iterator() {
if score > high_score {
high_score = score
high_score_player = player
}
}
println("{}", high_score)
}