my solutions to advent of code
aoc advent-of-code
at test 975 B view raw
1import gleam/int.{to_string} 2import gleam/io.{println} 3import gleam/list.{Continue, Stop, fold, fold_until} 4import gleam/result.{unwrap} 5import gleam/string.{split, trim} 6import simplifile.{read} 7 8pub type FloorIndex { 9 FloorIndex(floor: Int, index: Int) 10} 11 12pub fn main() { 13 let input = read(from: "../input.txt") |> unwrap("") |> trim() |> split("") 14 15 // part 1 - get final floor 16 println( 17 fold(input, 0, fn(fl, cur) { 18 case cur { 19 "(" -> fl + 1 20 ")" -> fl - 1 21 ___ -> 0 22 } 23 }) 24 |> to_string, 25 ) 26 27 // part 2 - get first time in the basement 28 println( 29 { 30 input 31 |> fold_until(FloorIndex(0, 1), fn(fi, cur) { 32 let new = case cur { 33 "(" -> fi.floor + 1 34 ")" -> fi.floor - 1 35 ___ -> 0 36 } 37 case new < 0 { 38 True -> Stop(FloorIndex(new, fi.index)) 39 False -> Continue(FloorIndex(new, fi.index + 1)) 40 } 41 }) 42 }.index 43 |> to_string, 44 ) 45}