You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
Elixir
52 lines
1.3 KiB
Elixir
defmodule Day25 do
|
|
def run do
|
|
%{locks: locks, keys: keys} = parse_input("data.txt")
|
|
|
|
# compare every key against every lock
|
|
good_keys =
|
|
Enum.reduce(keys, 0, fn key, acc ->
|
|
acc +
|
|
Enum.reduce(locks, 0, fn lock, acc2 ->
|
|
acc2 + if fits(lock, key), do: 1, else: 0
|
|
end)
|
|
end)
|
|
|
|
dbg(good_keys)
|
|
end
|
|
|
|
defp fits(lock, key) do
|
|
Enum.reduce(0..4, true, fn idx, acc ->
|
|
acc and Enum.at(lock, idx) + Enum.at(key, idx) <= 5
|
|
end)
|
|
end
|
|
|
|
defp parse_input(file) do
|
|
File.read!(file)
|
|
|> String.trim()
|
|
|> String.split("\n\n")
|
|
|> Enum.reduce(%{locks: [], keys: []}, fn el, %{locks: locks, keys: keys} = acc ->
|
|
grid = String.split(el, "\n") |> Enum.map(&String.graphemes/1)
|
|
is_lock = grid |> hd() |> hd() |> String.graphemes() |> Enum.all?(fn el -> el == "#" end)
|
|
|
|
res =
|
|
Enum.map(0..4, fn col ->
|
|
Enum.reduce(0..6, 0, fn row, acc2 ->
|
|
el = Enum.at(grid, row) |> Enum.at(col)
|
|
|
|
case el do
|
|
"#" -> acc2 + 1
|
|
_ -> acc2
|
|
end
|
|
end) - 1
|
|
end)
|
|
|
|
case is_lock do
|
|
true -> %{acc | locks: [res | locks]}
|
|
false -> %{acc | keys: [res | keys]}
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
Day25.run()
|