From eb278c807fc7406f807fed395897388aa92403fb Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:13:03 -0500 Subject: [PATCH] solve 2023 8.1 --- 2023/README.md | 3 ++- 2023/lib/2023/8.ex | 51 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 2023/lib/2023/8.ex diff --git a/2023/README.md b/2023/README.md index f919199..47ecd57 100644 --- a/2023/README.md +++ b/2023/README.md @@ -3,7 +3,7 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | | | | | | [1] | [2] | -| [3] | [4] | [5] | 6 | [7] | 8 | 9 | +| [3] | [4] | [5] | 6 | [7] | [8] | 9 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 24 | 25 | | | | | | @@ -16,3 +16,4 @@ [5]: ./lib/2023/5.ex [7]: ./lib/2023/7.ex +[8]: ./lib/2023/8.ex diff --git a/2023/lib/2023/8.ex b/2023/lib/2023/8.ex new file mode 100644 index 0000000..c6d37a1 --- /dev/null +++ b/2023/lib/2023/8.ex @@ -0,0 +1,51 @@ +import AOC +import AOCHelpers + +aoc 2023, 8 do + def p1(input) do + {instructions, mappings} = read_input(input) + + start = "AAA" + finish = "ZZZ" + + all_but_last_step = + instructions + |> Stream.cycle() + |> Stream.scan(start, &step(&1, &2, mappings)) + |> Stream.take_while(&(&1 != finish)) + |> Enum.count() + + all_but_last_step + 1 + end + + def p2(_input) do + end + + def step("L", curr, mappings) do + mappings + |> Map.get(curr) + |> elem(0) + end + + def step("R", curr, mappings) do + mappings + |> Map.get(curr) + |> elem(1) + end + + def read_input(input) do + [instructions_string, _ | mapping_lines] = lines(input) + instructions = letters(instructions_string) + + mappings = + mapping_lines + |> Enum.map(fn mapping_line -> + [_, key, left, right] = Regex.run(~r/^(.+) = \((.+), (.+)\)$/, mapping_line) + + {key, {left, right}} + end) + |> Enum.into(%{}) + + {instructions, mappings} + end +end diff --git a/README.md b/README.md index a4256b4..ed36e90 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **11/50** 🌟 +1. [2023] **12/50** 🌟 [2015]: ./2015 [2017]: ./2017