Created
November 24, 2023 10:41
-
-
Save sotolf2/5952247b6d5089f4fd16a1dbea8bf615 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gleam/io | |
import gleam/int | |
import gleam/string | |
import gleam/result | |
import simplifile | |
fn update_floor(i: String, floor: Int) -> Int { | |
case i { | |
")" -> floor - 1 | |
"(" -> floor + 1 | |
_ -> floor | |
} | |
} | |
fn follow(inst: List(String), floor floor: Int) -> Int { | |
case inst { | |
[] -> floor | |
[i, ..tl] -> follow(tl, update_floor(i, floor)) | |
} | |
} | |
fn find_basement( | |
inst: List(String), | |
floor floor: Int, | |
position pos: Int | |
) -> Int { | |
case inst { | |
[] -> pos | |
[i, ..tl] if floor >= 0 -> find_basement(tl, update_floor(i, floor), pos + 1) | |
_ -> pos | |
} | |
} | |
fn part1(input: String) { | |
let floor = input | |
|> string.to_graphemes() | |
|> follow(floor: 0) | |
io.print("Part1: ") | |
io.println(int.to_string(floor)) | |
} | |
fn part2(input: String) { | |
let pos_basement = | |
input | |
|> string.to_graphemes() | |
|> find_basement(floor: 0, position: 0) | |
io.print("Part2: ") | |
io.println(int.to_string(pos_basement)) | |
} | |
pub fn main() { | |
let data = simplifile.read(from: "./day01.txt") |> result.unwrap(or: "") | |
part1(data) | |
part2(data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment