-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2004701
commit b5b858b
Showing
5 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule Y2024.Day13 do | ||
use Advent.Day, no: 13 | ||
|
||
@doc """ | ||
iex> Day13.part1([%{buttons: %{a: %{x: 94, y: 34, cost: 3}, b: %{x: 22, y: 67, cost: 1}}, prize: %{x: 8400, y: 5400}}]) | ||
280 | ||
""" | ||
def part1(machines) do | ||
machines | ||
|> Enum.map(&find_solution/1) | ||
|> Enum.reduce(0, fn | ||
%{a: a, b: b}, acc -> acc + a.tokens + b.tokens | ||
nil, acc -> acc | ||
end) | ||
end | ||
|
||
# @doc """ | ||
# iex> Day13.part2("update or delete me") | ||
# "update or delete me" | ||
# """ | ||
# def part2(input) do | ||
# input | ||
# end | ||
|
||
@doc """ | ||
iex> Day13.find_solution(%{buttons: %{a: %{x: 94, y: 34, cost: 3}, b: %{x: 22, y: 67, cost: 1}}, prize: %{x: 8400, y: 5400}}) | ||
%{a: %{presses: 80, tokens: 240}, b: %{presses: 40, tokens: 40}} | ||
""" | ||
def find_solution(%{buttons: %{a: a, b: b}, prize: prize}) do | ||
a_presses = Enum.min([100, div(prize.x, a.x), div(prize.y, a.y)]) | ||
b_presses = Enum.min([100, div(prize.x, b.x), div(prize.y, b.y)]) | ||
|
||
result = | ||
for( | ||
a_press <- 0..a_presses, | ||
b_press <- 0..b_presses, | ||
do: {a_press, b_press, a.cost * a_press + b.cost * b_press} | ||
) | ||
|> Enum.sort_by(fn {_, _, cost} -> cost end) | ||
|> Enum.find(fn {a_press, b_press, _cost} -> | ||
a_press * a.x + b_press * b.x == prize.x && a_press * a.y + b_press * b.y == prize.y | ||
end) | ||
|
||
if result do | ||
%{ | ||
a: %{tokens: a.cost * elem(result, 0), presses: elem(result, 0)}, | ||
b: %{tokens: b.cost * elem(result, 1), presses: elem(result, 1)} | ||
} | ||
end | ||
end | ||
|
||
@doc """ | ||
iex> Day13.parse_input("Button A: X+94, Y+34\\nButton B: X+22, Y+67\\nPrize: X=8400, Y=5400") | ||
[%{buttons: %{a: %{x: 94, y: 34, cost: 3}, b: %{x: 22, y: 67, cost: 1}}, prize: %{x: 8400, y: 5400}}] | ||
""" | ||
def parse_input(input) do | ||
input | ||
|> String.split("\n\n", trim: true) | ||
|> Enum.map(fn machine -> | ||
[a, b, prize] = String.split(machine, "\n", trim: true) | ||
%{buttons: %{a: parse_button(a, 3), b: parse_button(b, 1)}, prize: parse_prize(prize)} | ||
end) | ||
end | ||
|
||
defp parse_button(string, cost) do | ||
<<"Button ", _::binary-1, ": X+", x::binary-2, ", Y+", y::binary-2>> = string | ||
%{cost: cost, x: String.to_integer(x), y: String.to_integer(y)} | ||
end | ||
|
||
defp parse_prize(string) do | ||
[[x], [y]] = Regex.scan(~r/\d+/, string) | ||
%{x: String.to_integer(x), y: String.to_integer(y)} | ||
end | ||
|
||
def part1_verify, do: input() |> parse_input() |> part1() | ||
# def part2_verify, do: input() |> parse_input() |> part2() | ||
end |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Y2024.Day13Test do | ||
use ExUnit.Case, async: true | ||
alias Y2024.Day13 | ||
doctest Day13 | ||
|
||
test "verification, part 1", do: assert(Day13.part1_verify() == 27157) | ||
# test "verification, part 2", do: assert(Day13.part2_verify() == "update or delete me") | ||
end |