Skip to content

Commit 0711091

Browse files
committed
2024 day 13 part 2 - remove some duplicate code
The logic for part 1 and part 2 is identical, the differences happen in the parsing layer
1 parent e6e0906 commit 0711091

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

lib/y2024/day13.ex

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,19 @@ defmodule Y2024.Day13 do
22
use Advent.Day, no: 13
33

44
@doc """
5-
iex> Day13.part1([
5+
iex> Day13.parts([
66
...> %{buttons: %{a: %{x: 94, y: 34, cost: 3}, b: %{x: 22, y: 67, cost: 1}},
77
...> prize: %{x: 8400, y: 5400}}
88
...> ])
99
280
10-
"""
11-
def part1(machines) do
12-
machines
13-
|> Enum.map(&find_solution/1)
14-
|> Enum.reduce(0, fn
15-
%{a: a, b: b}, acc -> acc + a.tokens + b.tokens
16-
nil, acc -> acc
17-
end)
18-
end
1910
20-
@doc """
21-
iex> Day13.part2([
11+
iex> Day13.parts([
2212
...> %{buttons: %{a: %{x: 26, y: 66, cost: 3}, b: %{x: 67, y: 21, cost: 1}},
2313
...> prize: %{x: 10000000012748, y: 10000000012176}}
2414
...> ])
2515
459236326669
2616
"""
27-
def part2(machines) do
17+
def parts(machines) do
2818
machines
2919
|> Enum.map(&find_solution/1)
3020
|> Enum.reduce(0, fn
@@ -87,6 +77,6 @@ defmodule Y2024.Day13 do
8777
%{x: String.to_integer(x) + offset, y: String.to_integer(y) + offset}
8878
end
8979

90-
def part1_verify, do: input() |> parse_input(0) |> part1()
91-
def part2_verify, do: input() |> parse_input(10_000_000_000_000) |> part2()
80+
def part1_verify, do: input() |> parse_input(0) |> parts()
81+
def part2_verify, do: input() |> parse_input(10_000_000_000_000) |> parts()
9282
end

0 commit comments

Comments
 (0)