Day 13 - Advent of Code 2023
Mix.install([:kino, :benchee])
Links
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"#.###.##..#....\n.#.###.....####\n...##...###.##.\n...######..#..#\n##.#..#.##..##.\n.#.##..#....##.\n##..##.#...####\n.####..##.#####\n#.####.#....##.\n..#.###.#..#..#\n##...####...##.\n##...####...##.\n..#.###.#..#..#\n#.###..#....##.\n.####..##.#####\n\n##..#.#......#..#\n.....#..#.#######\n...########...##.\n..#.#.#.###...##.\n..#.#..#.#.#.#..#\n..##...#.##.#.##.\n###.####..##.#...\n....#..#.#..#.##.\n....#.###..##....\n..##.#..##.######\n...##..#.#.##....\n...#.#..#........\n###....##..######\n..#...##..##.#..#\n..#.......#...##.\n\n#####.###\n.#.###..#\n##...##.#\n....###.#\n....###.#\n##...##.#\n.#.###..#\n#####.###\n..##.#.##\n###...##.\n.##.#.###\n.##.#.#.#\n###...##.\n\n..###...#####\n#######..#..#\n########.#..#\n..###...#####\n##.###..#####\n#.....###.##.\n#..###..##..#\n.#.#.##...##.\n.#.##########\n..###.###.##.\n####..#.#....\n##.##.....##.\n###...##.....\n...#.#.##....\n.##..#.#.#..#\n\n#......#...\n.#.#.##....\n##..####.##\n...##....##\n..#...##...\n#..#.......\n##..#.#..##\n###..#.####\n###..#.#.##\n\n..#......#...\n###.####.####\n.#..#..#.##..\n.#..#..#..#..\n##..####..###\n##..####..###\n.....##......\n.###.##.###..\n##..####..###\n#.########.##\n.##......##..\n.#..#..#..#..\n#...####...##\n\n#....##.#..#.##\n.#..#.##.####.#\n.#..#.##.####.#\n#....##.#..####\n#.##.#..#.####.\n......#..#....#\n.#..#...#..##.#\n.#..#.###.##.##\n..##...##...#.#\n......#.####.#.\n#.##.#..#......\n..##...###.#.#.\n......#..##.#.#\n\n..##.######\n##.....#..#\n..#.###.##.\n#.###..####\n.##.#..#..#\n.##....#..#\n#.###..####\n\n...#.####.#\n##.#..##..#\n##.........\n..##..##..#\n##..#....#.\n##....##...\n..#........\n##..#.##.#.\n...#......#\n...#.###..#\n...########\n####..##..#\n..#.##..##.\n###..####..\n..#.######.\n\n##.....###...\n##.....###...\n##.##.####.##\n###..###.##..\n........#..#.\n#.#..#.#..##.\n##.##.###..#.\n\n...##.####.#.#.\n..####.#...#...\n.....##.#.#..##\n##.....###.#.#.\n######.###.#...\n##.##....#.####\n...#.####..####\n..###....##.##.\n..###..#...#..#\n..###..##..#..#\n..###....##.##.\n...#.####..####\n##.##....#.####\n######.###.#...\n##.....###.#.#.\n\n##.#####.####..\n..#.##..#.##.#.\n..#.########...\n###.###.###....\n...#####.##.#..\n##.#..#..#...#.\n...##....#..#..\n##..##...##.##.\n##..##..####.#.\n##....#.#.##..#\n###...#.#.##..#\n##..##..####.#.\n##..##...##.##.\n\n...#.##.##.##.#..\n#....#..##..#....\n.#..#.##..##.#..#\n.#..#.##..##.#..#\n#....#..##..#....\n...#.##.##.##.#..\n####.##....##.###\n.#.##........##.#\n######.#..#.#####\n#.##...#..#...#..\n.#.#....##....#.#\n....#.#.##.#.#...\n##..###.##.###..#\n#.#.#.#....#.#.#.\n.######....######\n\n..#..###.\n..#.#.###\n..#...###\n.##....##\n....#.#.#\n####.##..\n####.##..\n....#.#.#\n.##....##\n..#...###\n..#.#.###\n\n....##.##\n#.##.....\n###....#.\n###....#.\n#.##.....\n....##.##\n#.#..####\n#.#.....#\n.##.#.#.#\n##...#.#.\n##...#.#.\n.#..#.#.#\n#.#.....#\n#.#..####\n....##.##\n#.##.....\n###....#.\n\n###.#........\n#.#.#.#.##.#.\n##.##.#.##.#.\n..##.#.#..#.#\n.##...#....#.\n.#...##.##.##\n.#...##.##.##\n.##...#....#.\n..##.#.#..#.#\n##.##.#.##.#.\n#.#...#.##.#.\n###.#........\n#.....#.##.#.\n\n#.##..###..####\n.##...#..###..#\n.##...#..#.#..#\n.#.######..##.#\n#....##.###.###\n.###..#.##.##..\n...#.#.##..#...\n#.........###.#\n###.#.....##..#\n###.#.....##..#\n#.........###.#\n...#.#.##..#...\n.###..#.##.##..\n#....##.###.###\n.#.######..##.#\n.##...#..#.#..#\n.##...#..###..#\n\n.....######....\n..#.#..##..#.#.\n##.#........#.#\n###....##....##\n##.....##.....#\n..############.\n..#...#..#.....\n\n##...##\n#..###.\n#...#.#\n#...#.#\n#..###.\n##...##\n###.##.\n#.#..##\n..##..#\n.#..#..\n#.##..#\n#....#.\n#....#.\n#.#...#\n.#..#..\n\n#..##.###..\n.##....#.#.\n......##.#.\n#..#.###.##\n######.....\n######.....\n#..#.######\n\n...##..##.#..###.\n..#.#..#.#....##.\n####.##.####....#\n.##.#..#.##..#..#\n.#.#....#.#...###\n##..####..###..#.\n##..####..###..#.\n.#.#....#.#...###\n.##.#..#.##..#..#\n####.##.####....#\n..#.#..#.#....##.\n\n####.#...\n...#.####\n##...##..\n###.#....\n..#.#.#..\n###....##\n####.####\n..##.#..#\n##.###..#\n.#.#..#.#\n###.###..\n###.#.###\n######.#.\n..#.#.###\n####....#\n####....#\n..#.#.###\n\n#..##..##..##..##\n##.#....####....#\n##.######..######\n...######..######\n#..#.##.#..#.##.#\n#####.########.##\n###..##..##..##..\n\n###....####\n####...##.#\n##.##.##..#\n####..##..#\n##.##.#.##.\n##.###.####\n####.######\n####.#..##.\n....." <> ...
Solution
defmodule Day13 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
input
|> parse()
|> Stream.map(&find_reflection/1)
|> Stream.map(fn
{:x, x} -> x
{:y, y} -> y * 100
end)
|> Enum.sum()
end
def part2(input) do
input
|> parse()
|> Stream.map(&find_smudge_reflection/1)
|> Stream.map(fn
{:x, x} -> x
{:y, y} -> y * 100
end)
|> Enum.sum()
end
defp find_smudge_reflection(grid) do
reflection = find_reflection(grid)
Enum.find_value(grid, fn
{{x, y}, ?.} ->
find_reflection(Map.put(grid, {x, y}, ?#), reflection)
{{x, y}, ?#} ->
find_reflection(Map.put(grid, {x, y}, ?.), reflection)
_ ->
nil
end)
end
defp find_reflection(grid, reflection \\ nil) do
find_reflection(:x, grid, reflection) || find_reflection(:y, grid, reflection)
end
defp find_reflection(:x, grid, reflection) do
Enum.find_value(1..grid.max_x, fn x ->
if reflection != {:x, x} and is_reflection?(:x, x, grid) do
{:x, x}
end
end)
end
defp find_reflection(:y, grid, reflection) do
Enum.find_value(1..grid.max_y, fn y ->
if reflection != {:y, y} and is_reflection?(:y, y, grid) do
{:y, y}
end
end)
end
@doc """
><
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
><
When x == 5, for every y, we want to check if:
- {4, y} == {5, y}
- {3, y} == {6, y}
- {2, y} == {7, y}
- {1, y} == {8, y}
- {0, y} == {9, y} (OFF GRID! this is ok)
"""
def is_reflection?(:x, line_x, grid) do
Enum.all?(0..grid.max_y, fn y ->
Enum.all?((line_x - 1)..0, fn x ->
a = grid[{x, y}]
b = grid[{2 * line_x - x - 1, y}]
is_nil(a) or is_nil(b) or a == b
end)
end)
end
def is_reflection?(:y, line_y, grid) do
Enum.all?(0..grid.max_x, fn x ->
Enum.all?((line_y - 1)..0, fn y ->
a = grid[{x, y}]
b = grid[{x, 2 * line_y - y - 1}]
is_nil(a) or is_nil(b) or a == b
end)
end)
end
defmodule Input do
def parse(input) when is_binary(input) do
input
|> String.splitter("\n\n", trim: true)
|> Stream.map(&parse_grid/1)
end
def parse(input) do
Stream.map(input, &parse_grid/1)
end
def parse_grid(input) do
input
|> String.split("\n", trim: true)
|> Enum.with_index()
|> Enum.reduce(%{}, &parse_line/2)
|> add_max_xy()
end
def parse_line({line, row}, grid) do
line
|> String.to_charlist()
|> Enum.with_index()
|> Enum.reduce(grid, fn {char, col}, acc ->
Map.put(acc, {col, row}, char)
end)
end
defp add_max_xy(grid) do
x = Map.keys(grid) |> Enum.map(&elem(&1, 0)) |> Enum.max()
y = Map.keys(grid) |> Enum.map(&elem(&1, 1)) |> Enum.max()
Map.merge(grid, %{max_x: x, max_y: y})
end
end
end
{:module, Day13, <<70, 79, 82, 49, 0, 0, 24, ...>>,
{:module, Day13.Input, <<70, 79, 82, ...>>, {:add_max_xy, 1}}}
Find the line of reflection in each of the patterns in your notes. What number do you get after summarizing all of your notes?
Your puzzle answer was 34918
.
Day13.part1(input)
34918
In each pattern, fix the smudge and find the different line of reflection. What number do you get after summarizing the new reflection line in each pattern in your notes?
Your puzzle answer was 33054
.
Day13.part2(input)
33054
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Tests
ExUnit.start(auto_run: false)
defmodule Day13Test do
use ExUnit.Case, async: false
setup_all do
[
input:
"#.##..##.\n..#.##.#.\n##......#\n##......#\n..#.##.#.\n..##..##.\n#.#.##.#.\n\n#...##..#\n#....#..#\n..##..###\n#####.##.\n#####.##.\n..##..###\n#....#..#"
]
end
describe "part1/1" do
test "returns expected value", %{input: input} do
assert Day13.part1(input) == 405
end
end
describe "part2/1" do
test "returns expected value", %{input: input} do
assert Day13.part2(input) == 400
end
end
end
ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures
Randomized with seed 648000
%{total: 2, failures: 0, excluded: 0, skipped: 0}
Benchmarking
# https://github.com/bencheeorg/benchee
Benchee.run(
%{
"Part 1" => fn -> Day13.part1(input) end,
"Part 2" => fn -> Day13.part2(input) end
},
memory_time: 2,
reduction_time: 2
)
nil
Warning: the benchmark Part 1 is using an evaluated function.
Evaluated functions perform slower than compiled functions.
You can move the Benchee caller to a function in a module and invoke `Mod.fun()` instead.
Alternatively, you can move the benchmark into a benchmark.exs file and run mix run benchmark.exs
Warning: the benchmark Part 2 is using an evaluated function.
Evaluated functions perform slower than compiled functions.
You can move the Benchee caller to a function in a module and invoke `Mod.fun()` instead.
Alternatively, you can move the benchmark into a benchmark.exs file and run mix run benchmark.exs
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 32 GB
Elixir 1.15.6
Erlang 26.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s
Benchmarking Part 1 ...
Benchmarking Part 2 ...
Name ips average deviation median 99th %
Part 1 247.11 4.05 ms ±2.01% 4.04 ms 4.29 ms
Part 2 14.05 71.17 ms ±0.93% 71.00 ms 74.56 ms
Comparison:
Part 1 247.11
Part 2 14.05 - 17.59x slower +67.12 ms
Memory usage statistics:
Name average deviation median 99th %
Part 1 7.12 MB ±0.06% 7.12 MB 7.13 MB
Part 2 89.13 MB ±0.00% 89.13 MB 89.14 MB
Comparison:
Part 1 7.12 MB
Part 2 89.13 MB - 12.51x memory usage +82.01 MB
Reduction count statistics:
Name Reduction count
Part 1 0.44 M
Part 2 9.67 M - 22.19x reduction count +9.23 M
**All measurements for reduction count were the same**
nil