-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.mar
33 lines (27 loc) · 873 Bytes
/
day2.mar
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
import ../stdlib.mar
| returns the game id if it's possible, 0 otherwise
fun game_score(game: String): Int {
var prefix_and_turns = game.split(": ")
var prefix = prefix_and_turns.get(0)
var turns = prefix_and_turns.get(1)
prefix.&.trim_prefix("Game ")
var id = prefix.parse_int().unwrap()
for turn in turns.split("; ") do
for cubes in turn.split(", ") do {
var parts = cubes.split(" ")
var num = parts.get(0).parse_int().unwrap()
var color = parts.get(1)
var is_possible = color == "red" and num <= 12
or color == "green" and num <= 13
or color == "blue" and num <= 14
if not(is_possible) then return 0
}
id | Game is possible
}
fun main(): Never {
var sum = 0
for line in read_file("advent/day2.big").unwrap().to_string().lines() do
sum = sum + line.game_score()
println(sum)
exit(0)
}