-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_02.gleam
68 lines (58 loc) · 1.79 KB
/
day_02.gleam
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
import gleam/dict.{type Dict}
import gleam/int
import gleam/list
import gleam/option.{Some}
import gleam/regex.{Match}
import gleam/result
import gleam/string
type Color =
String
type MaxCubeAmounts =
Dict(Color, Int)
pub fn part_1(input: String) -> Int {
let max_cube_amounts = parse_input(input)
max_cube_amounts
|> list.index_fold(0, fn(sum, cube_amounts, index) {
let game_number = index + 1
let is_valid_game =
True
&& cube_amounts |> dict.get("red") |> result.unwrap(0) <= 12
&& cube_amounts |> dict.get("green") |> result.unwrap(0) <= 13
&& cube_amounts |> dict.get("blue") |> result.unwrap(0) <= 14
case is_valid_game {
True -> sum + game_number
False -> sum
}
})
}
pub fn part_2(input: String) -> Int {
let max_cube_amounts = parse_input(input)
max_cube_amounts
|> list.fold(0, fn(sum, amounts) {
sum
+ { amounts |> dict.get("red") |> result.unwrap(0) }
* { amounts |> dict.get("green") |> result.unwrap(0) }
* { amounts |> dict.get("blue") |> result.unwrap(0) }
})
}
fn parse_input(input: String) -> List(MaxCubeAmounts) {
input
|> string.trim
|> string.split("\n")
|> list.map(fn(game) {
let assert Ok(re) = regex.from_string("[:;] ")
let assert [_, ..handfuls] = regex.split(re, game)
handfuls
|> list.fold(dict.new(), fn(cube_amounts, handful) {
let assert Ok(re) = regex.from_string("(\\d+) (red|green|blue)")
regex.scan(re, handful)
|> list.fold(cube_amounts, fn(cube_amounts, re_match) {
let assert Match(submatches: [Some(amount), Some(color)], ..) = re_match
let assert Ok(amount) = amount |> int.parse
dict.upsert(cube_amounts, color, fn(prev_amount) {
int.max(amount, prev_amount |> option.unwrap(0))
})
})
})
})
}