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