aboutsummaryrefslogtreecommitdiff
path: root/aoc2023
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-08 07:33:41 -0500
committerHJ <thechairman@thechairman.info>2023-12-08 07:33:41 -0500
commit583e26e8308753d929c113125b5ef43fd183461b (patch)
tree24511e06962dba6e76a70b67af493264c794334f /aoc2023
parentb9c4b4dfadd305c55fb09d421252780cb254f89b (diff)
downloadgleam_aoc-583e26e8308753d929c113125b5ef43fd183461b.tar.gz
gleam_aoc-583e26e8308753d929c113125b5ef43fd183461b.zip
day 8 complete
Diffstat (limited to 'aoc2023')
-rw-r--r--aoc2023/src/day8/solve.gleam31
-rw-r--r--aoc2023/test/day8/day8_test.gleam8
2 files changed, 20 insertions, 19 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() {
diff --git a/aoc2023/test/day8/day8_test.gleam b/aoc2023/test/day8/day8_test.gleam
index e54d887..2cd499a 100644
--- a/aoc2023/test/day8/day8_test.gleam
+++ b/aoc2023/test/day8/day8_test.gleam
@@ -4,10 +4,10 @@ import adglent.{type Example, Example}
import day8/solve
type Problem1AnswerType =
- String
+ Int
type Problem2AnswerType =
- String
+ Int
/// Add examples for part 1 here:
/// ```gleam
@@ -20,7 +20,7 @@ const part1_examples: List(Example(Problem1AnswerType)) = [
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)",
- "6",
+ 6,
),
]
@@ -40,7 +40,7 @@ const part2_examples: List(Example(Problem2AnswerType)) = [
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)",
- "6",
+ 6,
),
]