Created
February 24, 2026 17:38
-
-
Save alex-popov-tech/84e97f4765fe2e62a83af8532cff40ab to your computer and use it in GitHub Desktop.
This file contains hidden or 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/int | |
| import gleam/io | |
| import gleam/list | |
| import gleam/string | |
| import simplifile | |
| pub fn execute() -> Nil { | |
| case simplifile.read(from: "./inputs/day1.txt") { | |
| Error(e) -> io.println("cannot read input " <> string.inspect(e)) | |
| Ok(content) -> { | |
| let result = | |
| string.trim(content) | |
| |> string.split("\n") | |
| |> list.filter(fn(it) { !string.is_empty(it) }) | |
| |> list.map(fn(it) { | |
| let #(multiplier, text) = case string.pop_grapheme(it) { | |
| Ok(#("R", rest)) -> #(1, rest) | |
| Ok(#("L", rest)) -> #(-1, rest) | |
| Ok(#(it, _)) -> panic as {"Expected R or L but got " <> it} | |
| Error(e) -> panic as {"cannot parse " <> string.inspect(e)} | |
| } | |
| let assert Ok(i) = int.parse(text) | |
| multiplier * i | |
| }) | |
| |> list.fold(#(50, 0), fn(acc, curr) { | |
| let #(previous_state, counter) = acc | |
| let state_after_rotation = previous_state + curr | |
| let normalized_state = normalize(state_after_rotation) | |
| case normalized_state { | |
| 0 -> #(normalized_state, counter + 1) | |
| _ -> #(normalized_state, counter) | |
| } | |
| }) | |
| let #(_, counter) = result | |
| io.println("result is " <> int.to_string(counter)) | |
| } | |
| } | |
| } | |
| fn normalize(state: Int) -> Int { | |
| let state = state % 100 | |
| case state { | |
| 100 -> 0 | |
| it if it < 0 -> it + 100 | |
| _ -> state | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment