diff options
-rw-r--r-- | gleam/aoc2015/.gitignore | 1 | ||||
-rw-r--r-- | gleam/aoc2015/src/aoc2015.gleam | 4 | ||||
-rw-r--r-- | gleam/aoc2015/src/aoc_2015/day_1.gleam | 46 |
3 files changed, 49 insertions, 2 deletions
diff --git a/gleam/aoc2015/.gitignore b/gleam/aoc2015/.gitignore index 599be4e..34df305 100644 --- a/gleam/aoc2015/.gitignore +++ b/gleam/aoc2015/.gitignore @@ -1,4 +1,5 @@ *.beam *.ez /build +/input erl_crash.dump diff --git a/gleam/aoc2015/src/aoc2015.gleam b/gleam/aoc2015/src/aoc2015.gleam index dae067e..12d05ab 100644 --- a/gleam/aoc2015/src/aoc2015.gleam +++ b/gleam/aoc2015/src/aoc2015.gleam @@ -1,5 +1,5 @@ -import gleam/io +import gladvent pub fn main() { - io.println("Hello from aoc2015!") + gladvent.run() } diff --git a/gleam/aoc2015/src/aoc_2015/day_1.gleam b/gleam/aoc2015/src/aoc_2015/day_1.gleam new file mode 100644 index 0000000..c398f28 --- /dev/null +++ b/gleam/aoc2015/src/aoc_2015/day_1.gleam @@ -0,0 +1,46 @@ +import gleam/bool +import gleam/list +import gleam/string + +type Direction { + Up + Down +} + +fn parse(input: String) { + use ch <- list.map(string.to_graphemes(input)) + case ch { + "(" -> Up + ")" -> Down + _ -> panic + } +} + +fn traverse(directions: List(Direction), starting_at current_floor: Int) -> Int { + case directions { + [] -> current_floor + [Up, ..rest] -> traverse(rest, current_floor + 1) + [Down, ..rest] -> traverse(rest, current_floor - 1) + } +} + +pub fn pt_1(input: String) { + input |> parse |> traverse(starting_at: 0) +} + +fn find_basement( + directions: List(Direction), + starting_at current_floor: Int, + initial_count count: Int, +) -> Int { + use <- bool.guard(current_floor == -1, count) + case directions { + [] -> panic + [Up, ..rest] -> find_basement(rest, current_floor + 1, count + 1) + [Down, ..rest] -> find_basement(rest, current_floor - 1, count + 1) + } +} + +pub fn pt_2(input: String) { + input |> parse |> find_basement(starting_at: 0, initial_count: 0) +} |