add day 13
This commit is contained in:
parent
0e7d2d7c6d
commit
01432b8e0f
3 changed files with 1456 additions and 0 deletions
1385
23/elixir/inputs/day13.txt
Normal file
1385
23/elixir/inputs/day13.txt
Normal file
File diff suppressed because it is too large
Load diff
15
23/elixir/inputs/day13_example.txt
Normal file
15
23/elixir/inputs/day13_example.txt
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#.##..##.
|
||||||
|
..#.##.#.
|
||||||
|
##......#
|
||||||
|
##......#
|
||||||
|
..#.##.#.
|
||||||
|
..##..##.
|
||||||
|
#.#.##.#.
|
||||||
|
|
||||||
|
#...##..#
|
||||||
|
#....#..#
|
||||||
|
..##..###
|
||||||
|
#####.##.
|
||||||
|
#####.##.
|
||||||
|
..##..###
|
||||||
|
#....#..#
|
56
23/elixir/lib/days/day13.ex
Normal file
56
23/elixir/lib/days/day13.ex
Normal 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
|
Loading…
Reference in a new issue