Skip to content

Commit

Permalink
2023 day 3, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 3, 2023
1 parent 13b7af4 commit 615a4f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years).

<!-- stars start -->
<p><a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=5%20stars&style=for-the-badge&color=red" alt="5 stars" /></a><br />
<p><a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=6%20stars&style=for-the-badge&color=red" alt="6 stars" /></a><br />
<a href="./lib/y2022/"><img src="https://img.shields.io/static/v1?label=2022&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2021/"><img src="https://img.shields.io/static/v1?label=2021&message=46%20stars&style=for-the-badge&color=green" alt="46 stars" /></a><br />
<a href="./lib/y2020/"><img src="https://img.shields.io/static/v1?label=2020&message=38%20stars&style=for-the-badge&color=yellow" alt="38 stars" /></a><br />
Expand Down
4 changes: 3 additions & 1 deletion lib/y2023/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

My Elixir solutions for [Advent of Code 2023](https://adventofcode.com/2023).

<!-- stars 2023 start --><img src="https://img.shields.io/static/v1?label=2023&message=5%20stars&style=for-the-badge&color=red" alt="5 stars" /><!-- stars 2023 end -->
<!-- stars 2023 start --><img src="https://img.shields.io/static/v1?label=2023&message=6%20stars&style=for-the-badge&color=red" alt="6 stars" /><!-- stars 2023 end -->

## Benchmarks

Expand All @@ -18,4 +18,6 @@ day 01, part 1 110.07 9.09 ms ±1.38% 9.07 ms 9.
day 01, part 2 85.23 11.73 ms ±0.77% 11.71 ms 11.96 ms
day 02, part 1 1.17 K 0.86 ms ±75.22% 0.77 ms 4.03 ms
day 02, part 2 1.17 K 0.85 ms ±67.69% 0.79 ms 2.49 ms
day 03, part 1 30.82 32.45 ms ±2.29% 32.36 ms 36.61 ms
day 03, part 2 13.71 72.96 ms ±0.50% 72.89 ms 73.86 ms
```
42 changes: 33 additions & 9 deletions lib/y2023/day03.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ defmodule Y2023.Day03 do
|> Enum.any?(&Enum.member?(symbol_positions, &1))
end

defp calculate_surrounding_positions({row, col}, length) do
# 27998156 - too low
def part2(%{numbers: numbers, symbols: symbols}) do
number_positions = invert_numbers(numbers)

symbols
|> Enum.filter(fn {_position, symbol} -> symbol == "*" end)
|> Enum.map(&elem(&1, 0))
|> Enum.map(&adjacent_numbers(&1, number_positions))
|> Enum.filter(fn list -> length(list) == 2 end)
|> Enum.reduce(0, fn [a, b], acc -> acc + a * b end)
end

defp calculate_surrounding_positions({row, col}, length \\ 1) do
[{row, col - 1}, {row, col + length}] ++
row_positions(row + 1, col, length) ++ row_positions(row - 1, col, length)
end
Expand All @@ -25,13 +37,25 @@ defmodule Y2023.Day03 do
Enum.map((col - 1)..(col + length), &{row, &1})
end

# @doc """
# iex> Day03.part2("update or delete me")
# "update or delete me"
# """
# def part2(input) do
# input
# end
# For part 1, numbers were useful to store as `%{position -> %{number, length}}`
# But for part 2, we want the set of positions each number covers
defp invert_numbers(numbers) do
numbers
|> Enum.reduce([], fn {{row, col}, %{number: number, length: length}}, acc ->
positions = Enum.map(col..(col + length - 1), fn col -> {row, col} end) |> MapSet.new()
[{number, positions} | acc]
end)
end

defp adjacent_numbers(position, numbers) do
surrounding_positions = MapSet.new(calculate_surrounding_positions(position))

numbers
|> Enum.reject(fn {_number, positions} ->
MapSet.disjoint?(positions, surrounding_positions)
end)
|> Enum.map(&elem(&1, 0))
end

def parse_input(input) do
input
Expand Down Expand Up @@ -74,5 +98,5 @@ defmodule Y2023.Day03 do
end

def part1_verify, do: input() |> parse_input() |> part1()
# def part2_verify, do: input() |> parse_input() |> part2()
def part2_verify, do: input() |> parse_input() |> part2()
end
9 changes: 8 additions & 1 deletion test/y2023/day03_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Y2023.Day03Test do
doctest Day03

test "verification, part 1", do: assert(Day03.part1_verify() == 536_202)
# test "verification, part 2", do: assert(Day03.part2_verify() == "update or delete me")
test "verification, part 2", do: assert(Day03.part2_verify() == 78_272_573)

@sample_input """
467..114..
Expand All @@ -25,4 +25,11 @@ defmodule Y2023.Day03Test do

assert actual == expected
end

test "part 2" do
actual = @sample_input |> Day03.parse_input() |> Day03.part2()
expected = 467_835

assert actual == expected
end
end

0 comments on commit 615a4f2

Please sign in to comment.