add day 13

This commit is contained in:
Pim Kunis 2023-12-13 15:50:49 +01:00
parent 0e7d2d7c6d
commit 01432b8e0f
3 changed files with 1456 additions and 0 deletions

1385
23/elixir/inputs/day13.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

View file

@ -0,0 +1,56 @@
defmodule AOC.Day13 do
use AOC.Day, day: 13
def parse_input(lines) do
lines
|> Enum.chunk_by(&(&1 == ""))
|> Enum.reject(&(&1 == [""]))
|> Enum.map(fn pattern ->
Enum.map(pattern, &String.to_charlist/1)
end)
end
def find_horizontal_reflection(pattern, required_differences) do
Enum.find(1..(length(pattern) - 1), fn cut_size ->
{upper_pattern, lower_pattern} = Enum.split(pattern, cut_size)
upper_pattern =
upper_pattern
|> Enum.reverse()
|> List.flatten()
lower_pattern = List.flatten(lower_pattern)
differences =
Enum.zip(upper_pattern, lower_pattern)
|> Enum.count(fn {symbol1, symbol2} -> symbol1 != symbol2 end)
differences == required_differences
end)
end
def find_vertical_reflection(pattern, required_differences) do
pattern
|> List.zip()
|> Enum.map(&Tuple.to_list/1)
|> find_horizontal_reflection(required_differences)
end
def calculate_summary(patterns, required_differences) do
Enum.map(patterns, fn pattern ->
case find_horizontal_reflection(pattern, required_differences) do
nil -> find_vertical_reflection(pattern, required_differences)
x -> 100 * x
end
end)
|> Enum.sum()
end
def part1(input) do
calculate_summary(input, 0)
end
def part2(input) do
calculate_summary(input, 1)
end
end