diff --git a/day25/main.exs b/day25/main.exs new file mode 100644 index 0000000..e56f4b1 --- /dev/null +++ b/day25/main.exs @@ -0,0 +1,51 @@ +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()