Skip to content

Commit

Permalink
2023 day 3, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 3, 2023
1 parent 076d6e6 commit 13b7af4
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 2 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=4%20stars&style=for-the-badge&color=red" alt="4 stars" /></a><br />
<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 />
<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
2 changes: 1 addition & 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=4%20stars&style=for-the-badge&color=red" alt="4 stars" /><!-- stars 2023 end -->
<!-- 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 -->

## Benchmarks

Expand Down
78 changes: 78 additions & 0 deletions lib/y2023/day03.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
defmodule Y2023.Day03 do
use Advent.Day, no: 03

def part1(%{numbers: numbers, symbols: symbols}) do
symbol_positions = Map.keys(symbols)

numbers
|> Enum.filter(&next_to_symbol?(&1, symbol_positions))
|> Enum.map(fn {_, %{number: number}} -> number end)
|> Enum.sum()
end

defp next_to_symbol?({position, %{length: length}}, symbol_positions) do
position
|> calculate_surrounding_positions(length)
|> Enum.any?(&Enum.member?(symbol_positions, &1))
end

defp calculate_surrounding_positions({row, col}, length) do
[{row, col - 1}, {row, col + length}] ++
row_positions(row + 1, col, length) ++ row_positions(row - 1, col, length)
end

defp row_positions(row, col, length) 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

def parse_input(input) do
input
|> String.split("\n", trim: true)
|> Enum.with_index()
|> Enum.reduce(%{numbers: %{}, symbols: %{}}, fn {row, row_no}, acc ->
%{
numbers: read_numbers(row, row_no, acc.numbers),
symbols: read_symbols(row, row_no, acc.symbols)
}
end)
end

# Store a map of all of the numbers in the input, in the format
# `%{{row, col} => %{number: number, length: length}`
defp read_numbers(row, row_no, acc) do
numbers = Regex.scan(~r/[0-9]+/, row, return: :binary)
indexes = Regex.scan(~r/[0-9]+/, row, return: :index)

numbers
|> Enum.zip(indexes)
|> Enum.reduce(acc, fn {[number], [{col, length}]}, acc ->
Map.put(acc, {row_no, col}, %{number: String.to_integer(number), length: length})
end)
end

# Store a map of all of the symbols in the input, in the format
# `%{{row, col} => "symbol"}`
defp read_symbols(row, row_no, acc) do
row
|> String.graphemes()
|> Enum.with_index()
|> Enum.reduce(acc, fn {val, col_no}, acc ->
if val not in [".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] do
Map.put(acc, {row_no, col_no}, val)
else
acc
end
end)
end

def part1_verify, do: input() |> parse_input() |> part1()
# def part2_verify, do: input() |> parse_input() |> part2()
end
Loading

0 comments on commit 13b7af4

Please sign in to comment.