solve 2023 3.2
This commit is contained in:
parent
11ba699188
commit
663165a30d
2 changed files with 37 additions and 2 deletions
|
@ -31,7 +31,18 @@ aoc 2023, 3 do
|
|||
|> Enum.sum()
|
||||
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
|
||||
|
||||
def to_grid(lines) do
|
||||
|
@ -90,4 +101,28 @@ aoc 2023, 3 do
|
|||
{y, x}
|
||||
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
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
1. [2020] **17/50** 🌟
|
||||
1. [2021] **43/50** 🌟
|
||||
1. [2022] **14/50** 🌟
|
||||
1. [2023] **5/50** 🌟
|
||||
1. [2023] **6/50** 🌟
|
||||
|
||||
[2015]: ./2015
|
||||
[2017]: ./2017
|
||||
|
|
Loading…
Reference in a new issue