aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/day8/solve.gleam
diff options
context:
space:
mode:
Diffstat (limited to 'aoc2023/src/day8/solve.gleam')
-rw-r--r--aoc2023/src/day8/solve.gleam31
1 files changed, 16 insertions, 15 deletions
diff --git a/aoc2023/src/day8/solve.gleam b/aoc2023/src/day8/solve.gleam
index dee24a4..cbd2f6a 100644
--- a/aoc2023/src/day8/solve.gleam
+++ b/aoc2023/src/day8/solve.gleam
@@ -2,7 +2,7 @@ import adglent.{First, Second}
import gleam/bool
import gleam/dict.{type Dict}
import gleam/io
-import gleam/iterator.{Next}
+import gleam/iterator.{type Iterator, Next}
import gleam/list
import gleam/option.{Some}
import gleam/string
@@ -16,7 +16,7 @@ type Paths {
type Maze =
Dict(String, Paths)
-fn parse(input: String) {
+fn parse(input: String) -> #(Iterator(String), Dict(String, Paths)) {
let [directions_str, maze_str] = string.split(input, "\n\n")
let directions =
@@ -39,7 +39,13 @@ fn parse(input: String) {
#(directions, maze)
}
-fn to_next_step(current, stop_at, count, directions, maze: Maze) {
+fn to_next_step(
+ current: String,
+ stop_at: String,
+ count: Int,
+ directions: Iterator(String),
+ maze: Maze,
+) -> Int {
use <- bool.guard(string.ends_with(current, stop_at), count)
let assert Next(next_direction, rest_directions) = iterator.step(directions)
let assert Ok(paths) = dict.get(maze, current)
@@ -50,27 +56,22 @@ fn to_next_step(current, stop_at, count, directions, maze: Maze) {
|> to_next_step(stop_at, count + 1, rest_directions, maze)
}
-pub fn part1(input: String) {
+pub fn part1(input: String) -> Int {
let #(directions, maze) = parse(input)
to_next_step("AAA", "ZZZ", 0, directions, maze)
- |> string.inspect
}
-pub fn part2(input: String) {
+pub fn part2(input: String) -> Int {
let #(directions, maze) = parse(input)
- maze
- |> dict.keys
- |> list.filter(string.ends_with(_, "A"))
- |> list.fold(
- 1,
- fn(acc, name) {
+ use acc, name <- list.fold(dict.keys(maze), 1)
+ case string.ends_with(name, "A") {
+ False -> acc
+ True ->
to_next_step(name, "Z", 0, directions, maze)
|> arithmetics.lcm(acc)
- },
- )
- |> string.inspect
+ }
}
pub fn main() {