1
0
Fork 0

solve 2023 8.1

This commit is contained in:
sloane 2023-12-08 12:13:03 -05:00
parent dfda215b9d
commit eb278c807f
No known key found for this signature in database
3 changed files with 54 additions and 2 deletions

View file

@ -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
<!-- [6]: ./lib/2023/6.ex -->
[7]: ./lib/2023/7.ex
[8]: ./lib/2023/8.ex

51
2023/lib/2023/8.ex Normal file
View file

@ -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

View file

@ -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