1
0
Fork 0

solve 2023 3.2

This commit is contained in:
sloane 2023-12-03 10:25:43 -05:00
parent 11ba699188
commit 663165a30d
No known key found for this signature in database
2 changed files with 37 additions and 2 deletions

View file

@ -31,7 +31,18 @@ aoc 2023, 3 do
|> Enum.sum() |> Enum.sum()
end end
def p2(_input) do def p2(input) do
grid =
input
|> String.split("\n")
|> Enum.map(&String.to_charlist/1)
|> to_grid()
for {coord, "*"} <- grid, is_gear(grid, coord), reduce: 0 do
sum ->
{_, [a, b]} = neighboring_numbers(grid, coord)
sum + a * b
end
end end
def to_grid(lines) do def to_grid(lines) do
@ -90,4 +101,28 @@ aoc 2023, 3 do
{y, x} {y, x}
end end
end end
def neighboring_numbers(grid, coord, already_seen \\ MapSet.new()) do
for c <- neighboring_coords(coord), reduce: {already_seen, []} do
{seen, numbers} ->
case Map.get(grid, c) do
{id, n} ->
if MapSet.member?(seen, id) do
{seen, numbers}
else
{MapSet.put(seen, id), [n | numbers]}
end
_ ->
{seen, numbers}
end
end
end
def is_gear(grid, coord) do
case neighboring_numbers(grid, coord) do
{_, [_, _]} -> true
_ -> false
end
end
end end

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] **5/50** 🌟 1. [2023] **6/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017