-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday_2.gleam
67 lines (57 loc) · 1.51 KB
/
day_2.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
import gleam/int
import gleam/list
import gleam/string
pub type Present {
Box(length: Int, width: Int, height: Int)
}
pub type Data =
List(Present)
pub fn parse(input: String) -> Data {
input
|> string.trim()
|> string.split(on: "\n")
|> list.map(fn(input) {
input
|> string.split("x")
|> list.map(int.parse)
|> fn(present) {
case present {
[Ok(length), Ok(width), Ok(height)] -> Box(length, width, height)
_ -> panic as { "Bad present: " <> input }
}
}
})
}
pub fn pt_1(input: Data) -> Int {
input
|> list.map(fn(present) { surface_area(present) + smallest_side(present) })
|> int.sum()
}
pub fn pt_2(input: Data) -> Int {
input
|> list.map(fn(present) { smallest_perimeter(present) + volume(present) })
|> int.sum()
}
fn shortest_sides(box: Present) -> #(Int, Int) {
case box.length, box.width, box.height {
ln, wd, ht if ln >= wd && ln >= ht -> #(wd, ht)
ln, wd, ht if wd >= ln && wd >= ht -> #(ht, ln)
ln, wd, ht if ht >= ln && ht >= wd -> #(ln, wd)
_, _, _ -> panic as "bad box"
}
}
pub fn surface_area(box: Present) -> Int {
let Box(length, width, height) = box
2 * { length * width + width * height + height * length }
}
pub fn smallest_side(box: Present) -> Int {
let #(first, second) = box |> shortest_sides
first * second
}
pub fn smallest_perimeter(box: Present) -> Int {
let #(first, second) = box |> shortest_sides
2 * { first + second }
}
pub fn volume(box: Present) -> Int {
box.length * box.width * box.height
}