my solutions to advent of code
aoc advent-of-code
at main 2.1 kB view raw
1import gleam/dict 2import gleam/int 3import gleam/io 4import gleam/list 5import gleam/result 6import gleam/string 7import simplifile as file 8 9fn check_containers_part1(input, remaining, right_options) { 10 case remaining { 11 0 -> right_options + 1 12 r if r < 0 -> right_options 13 _ -> { 14 let #(_, right_options) = 15 input 16 |> dict.fold(#(input, right_options), fn(acc, cid, cv) { 17 let #(input, right_options) = acc 18 let input = input |> dict.delete(cid) 19 #(input, check_containers_part1(input, remaining - cv, right_options)) 20 }) 21 right_options 22 } 23 } 24} 25 26fn check_containers_part2(input, remaining, right_options, jars_used) { 27 case remaining { 28 0 -> 29 right_options 30 |> dict.insert( 31 jars_used, 32 { dict.get(right_options, jars_used) |> result.unwrap(0) } + 1, 33 ) 34 r if r < 0 -> right_options 35 _ -> { 36 let #(_, right_options) = 37 input 38 |> dict.fold(#(input, right_options), fn(acc, cid, cv) { 39 let #(input, right_options) = acc 40 let input = input |> dict.delete(cid) 41 #( 42 input, 43 check_containers_part2( 44 input, 45 remaining - cv, 46 right_options, 47 jars_used + 1, 48 ), 49 ) 50 }) 51 right_options 52 } 53 } 54} 55 56pub fn main() { 57 let assert Ok(input) = file.read(from: "../input.txt") 58 as "Input file not found" 59 let #(input, _) = 60 input 61 |> string.trim 62 |> string.split("\n") 63 |> list.fold(#(dict.new(), 1), fn(l, v) { 64 #(dict.insert(l.0, l.1, int.parse(v) |> result.unwrap(0)), l.1 + 1) 65 }) 66 67 check_containers_part1(input, 150, 0) 68 |> int.to_string 69 |> io.println 70 71 let #(_number_of_jars, possibilities) = 72 check_containers_part2(input, 150, dict.new(), 0) 73 |> dict.fold(#(999_999, 0), fn(last, number_of_jars, possibilities) { 74 case last.0 > number_of_jars { 75 True -> #(number_of_jars, possibilities) 76 False -> last 77 } 78 }) 79 possibilities 80 |> int.to_string 81 |> io.println 82}