Day 13
This commit is contained in:
46
lib/day13.ex
Normal file
46
lib/day13.ex
Normal file
@@ -0,0 +1,46 @@
|
||||
defmodule Aoc2023.Day13 do
|
||||
use Aoc2023
|
||||
|
||||
def parse(input) do
|
||||
input
|
||||
|> String.split("\n\n")
|
||||
|> Enum.map(pipe(lines |> Enum.map(&String.to_charlist/1)))
|
||||
end
|
||||
|
||||
defp reflected_row(block, smudges) do
|
||||
0..(length(block) - 2)
|
||||
|> Enum.filter(fn i ->
|
||||
Enum.zip_with(
|
||||
Enum.take(block, i + 1) |> Enum.reverse() |> Enum.concat(),
|
||||
Enum.drop(block, i + 1) |> Enum.concat(),
|
||||
&Kernel.==/2
|
||||
)
|
||||
|> Enum.count(&(not &1))
|
||||
|> Kernel.==(smudges)
|
||||
end)
|
||||
|> head
|
||||
end
|
||||
|
||||
defp reflected_column(block, smudges) do
|
||||
block |> transpose() |> reflected_row(smudges)
|
||||
end
|
||||
|
||||
defp reflection(block, smudges \\ 0) do
|
||||
row = reflected_row(block, smudges)
|
||||
if row != nil, do: {:h, row + 1}, else: {:v, reflected_column(block, smudges) + 1}
|
||||
end
|
||||
|
||||
def part1(input) do
|
||||
reflections = input |> Enum.map(&reflection/1)
|
||||
|
||||
(Keyword.get_values(reflections, :h) |> Enum.sum()) * 100 +
|
||||
(Keyword.get_values(reflections, :v) |> Enum.sum())
|
||||
end
|
||||
|
||||
def part2(input) do
|
||||
reflections = input |> Enum.map(pipe(reflection(1)))
|
||||
|
||||
(Keyword.get_values(reflections, :h) |> Enum.sum()) * 100 +
|
||||
(Keyword.get_values(reflections, :v) |> Enum.sum())
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user