1
0
Fork 0

solve 2023 8.2

This commit is contained in:
sloane 2023-12-08 12:49:55 -05:00
parent eb278c807f
commit 44c277ff94
No known key found for this signature in database
2 changed files with 31 additions and 7 deletions

View file

@ -5,20 +5,40 @@ aoc 2023, 8 do
def p1(input) do def p1(input) do
{instructions, mappings} = read_input(input) {instructions, mappings} = read_input(input)
start = "AAA"
finish = "ZZZ"
all_but_last_step = all_but_last_step =
instructions instructions
|> Stream.cycle() |> Stream.cycle()
|> Stream.scan(start, &step(&1, &2, mappings)) |> Stream.scan("AAA", &step(&1, &2, mappings))
|> Stream.take_while(&(&1 != finish)) |> Stream.take_while(&(&1 != "ZZZ"))
|> Enum.count() |> Enum.count()
all_but_last_step + 1 all_but_last_step + 1
end end
def p2(_input) do def p2(input) do
{instructions, mappings} = read_input(input)
starts =
mappings
|> Map.keys()
|> Enum.filter(&String.ends_with?(&1, "A"))
lengths =
starts
|> Enum.map(fn start ->
n =
instructions
|> Stream.cycle()
|> Stream.scan(start, &step(&1, &2, mappings))
|> Stream.take_while(&(not String.ends_with?(&1, "Z")))
|> Enum.count()
n + 1
end)
Enum.reduce(lengths, fn a, b ->
div(a * b, Integer.gcd(a, b))
end)
end end
def step("L", curr, mappings) do def step("L", curr, mappings) do
@ -33,6 +53,10 @@ aoc 2023, 8 do
|> elem(1) |> elem(1)
end end
def step_all(inst, currs, mappings) do
Enum.map(currs, &step(inst, &1, mappings))
end
def read_input(input) do def read_input(input) do
[instructions_string, _ | mapping_lines] = lines(input) [instructions_string, _ | mapping_lines] = lines(input)
instructions = letters(instructions_string) instructions = letters(instructions_string)

View file

@ -14,7 +14,7 @@
1. [2020] **17/50** 🌟 1. [2020] **17/50** 🌟
1. [2021] **43/50** 🌟 1. [2021] **43/50** 🌟
1. [2022] **14/50** 🌟 1. [2022] **14/50** 🌟
1. [2023] **12/50** 🌟 1. [2023] **13/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017