Day 13
This commit is contained in:
@@ -37,7 +37,9 @@ defmodule Aoc2023.Common do
|
||||
def lines(x), do: String.split(x, "\n")
|
||||
|
||||
def head([head | _]), do: head
|
||||
def head([]), do: nil
|
||||
def tail([_ | tail]), do: tail
|
||||
def tail([]), do: nil
|
||||
|
||||
def lcm(a, b), do: div(abs(a*b), Integer.gcd(a,b))
|
||||
def lcm(ls), do: ls |> Enum.reduce(&lcm/2)
|
||||
@@ -55,6 +57,6 @@ defmodule Aoc2023.Common do
|
||||
|> Stream.map(pipe(elem(0)))
|
||||
end
|
||||
|
||||
def repeat(lst, 0), do: []
|
||||
def repeat(_, 0), do: []
|
||||
def repeat(lst, n), do: Enum.concat(lst, repeat(lst, n - 1))
|
||||
end
|
||||
|
||||
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
|
||||
@@ -14,6 +14,7 @@ defmodule Mix.Tasks.Aoc do
|
||||
defp module(10), do: Aoc2023.Day10
|
||||
defp module(11), do: Aoc2023.Day11
|
||||
defp module(12), do: Aoc2023.Day12
|
||||
defp module(13), do: Aoc2023.Day13
|
||||
# [MODULE INSERTION POINT]
|
||||
|
||||
defp base_dir(), do: System.get_env("AOC_BASE")
|
||||
|
||||
Reference in New Issue
Block a user