diff --git a/2023/lib/2023/13.ex b/2023/lib/2023/13.ex index 5fb2c3b..c223962 100644 --- a/2023/lib/2023/13.ex +++ b/2023/lib/2023/13.ex @@ -10,34 +10,40 @@ aoc 2023, 13 do |> Enum.sum() end - def p2(_input) do + def p2(input) do + input + |> String.split("\n\n") + |> Enum.map(&lines_of_chars/1) + |> Enum.map(&summarize(&1, 1)) + |> Enum.sum() end - def summarize(lines) do - vertical_line_of_reflection = find_reflection_index(lines) - horizontal_line_of_reflection = lines |> transpose() |> find_reflection_index() + def summarize(lines, smudges \\ 0) do + vertical_line_of_reflection = find_reflection_index(lines, smudges) + horizontal_line_of_reflection = lines |> transpose() |> find_reflection_index(smudges) summary = vertical_line_of_reflection + 100 * horizontal_line_of_reflection summary end - def find_reflection_index(lines) do + def find_reflection_index(lines, smudges) do len = lines |> List.first() |> length() idx = for i <- 1..(len - 1) do lines - |> Enum.all?(fn line -> + |> Enum.map(fn line -> line |> Enum.split(i) |> Tuple.to_list() |> map_list([&Enum.reverse/1, &id/1]) |> Enum.zip_with(&apply(Kernel, :==, &1)) - |> Enum.all?(is?(true)) + |> Enum.count(is?(false)) end) + |> Enum.sum() end - |> Enum.find_index(is?(true)) + |> Enum.find_index(is?(smudges)) if idx, do: idx + 1, else: 0 end diff --git a/README.md b/README.md index e2b4e09..4ed6b35 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **18/50** 🌟 +1. [2023] **19/50** 🌟 [2015]: ./2015 [2017]: ./2017