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