my solutions to advent of code
aoc advent-of-code
at main 1.1 kB view raw
1import gleam/int 2import gleam/io 3import gleam/list 4import gleam/result 5import gleam/string 6import simplifile as file 7 8pub fn main() { 9 let input = 10 file.read(from: "../input.txt") 11 |> result.unwrap("") 12 |> string.trim 13 |> string.split("\n") 14 |> list.map(fn(str) { 15 string.split(str, "x") 16 |> list.map(fn(str) { int.base_parse(str, 10) |> result.unwrap(0) }) 17 }) 18 19 // part 1 20 input 21 |> list.fold(0, fn(acc, cur) { 22 let sides = 23 cur 24 |> list.combinations(2) 25 |> list.map(fn(v) { list.fold(v, 1, int.multiply) }) 26 27 acc 28 + { list.fold(sides, 0, int.add) |> int.multiply(2) } 29 + { list.reduce(sides, int.min) |> result.unwrap(0) } 30 }) 31 |> int.to_string 32 |> io.println 33 34 // part 2 35 input 36 |> list.fold(0, fn(acc, cur) { 37 let smallest_perimeter = 38 cur 39 |> list.map(fn(v) { v |> int.multiply(2) }) 40 |> list.combinations(2) 41 |> list.map(fn(v) { list.fold(v, 0, int.add) }) 42 |> list.reduce(int.min) 43 |> result.unwrap(0) 44 45 acc + smallest_perimeter + list.fold(cur, 1, int.multiply) 46 }) 47 |> int.to_string 48 |> io.println 49}