Skip to content

Commit 74d0673

Browse files
committed
2024 Day 25
1 parent 90f58fc commit 74d0673

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

2024/lib/day25.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule Day25a do
2+
def run do
3+
File.read!(".input.txt") |> run
4+
end
5+
6+
def fits?(a, b) do
7+
Enum.zip(a, b)
8+
|> Enum.all?(fn {a, b} -> a + b <= 5 end)
9+
end
10+
11+
def run(input) do
12+
{locks, keys} =
13+
input
14+
|> String.split("\n\n")
15+
|> Enum.reduce({[], []}, fn pattern, {locks, keys} ->
16+
pattern =
17+
pattern
18+
|> String.split("\n")
19+
|> Enum.map(&(&1 |> String.to_charlist()))
20+
21+
heights =
22+
pattern
23+
|> tl()
24+
|> Enum.take(5)
25+
|> Enum.reduce([0, 0, 0, 0, 0], fn line, heights ->
26+
Enum.zip(heights, line)
27+
|> Enum.map(fn {h, cell} -> h + if cell == ?#, do: 1, else: 0 end)
28+
end)
29+
30+
if hd(pattern) == ~c"#####" do
31+
{[heights | locks], keys}
32+
else
33+
{locks, [heights | keys]}
34+
end
35+
end)
36+
37+
locks
38+
|> Enum.map(fn lock ->
39+
keys |> Enum.count(fn key -> fits?(lock, key) end)
40+
end)
41+
|> Enum.sum()
42+
end
43+
end

2024/test/day25_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule Day25Test do
2+
use ExUnit.Case
3+
4+
def example, do: ""
5+
6+
test "Part 1 Example" do
7+
assert Day25a.run(example()) == :todo
8+
end
9+
10+
# test "Part 2 Example" do
11+
# assert Day25b.run(example()) == :todo
12+
# end
13+
end

0 commit comments

Comments
 (0)