Skip to content

Commit

Permalink
2024 day 13, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 13, 2024
1 parent 2004701 commit b5b858b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years).

<!-- stars start -->
<p><img src="https://img.shields.io/static/v1?label=Total&message=443%20stars&style=for-the-badge&color=green" alt="443 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=24%20stars&style=for-the-badge&color=orange" alt="24 stars" /></a><br />
<p><img src="https://img.shields.io/static/v1?label=Total&message=444%20stars&style=for-the-badge&color=green" alt="444 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=25%20stars&style=for-the-badge&color=yellow" alt="25 stars" /></a><br />
<a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=44%20stars&style=for-the-badge&color=green" alt="44 stars" /></a><br />
<a href="./lib/y2022/"><img src="https://img.shields.io/static/v1?label=2022&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2021/"><img src="https://img.shields.io/static/v1?label=2021&message=46%20stars&style=for-the-badge&color=green" alt="46 stars" /></a><br />
Expand Down
3 changes: 2 additions & 1 deletion lib/y2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

My Elixir solutions for [Advent of Code 2024](https://adventofcode.com/2024).

<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=24%20stars&style=for-the-badge&color=orange" alt="24 stars" /><!-- stars 2024 end -->
<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=25%20stars&style=for-the-badge&color=yellow" alt="25 stars" /><!-- stars 2024 end -->

## Benchmarks

Expand Down Expand Up @@ -38,4 +38,5 @@ day 11, part 1 838.21 1.19 ms ±4.86% 1.21 ms 1.
day 11, part 2 19.52 51.22 ms ±1.86% 51.26 ms 53.37 ms
day 12, part 1 7.16 139.70 ms ±1.82% 139.22 ms 144.95 ms
day 12, part 2 6.84 146.22 ms ±1.32% 145.82 ms 151.25 ms
day 13, part 1 4.91 203.63 ms ±0.46% 203.49 ms 206.75 ms
```
77 changes: 77 additions & 0 deletions lib/y2024/day13.ex
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 added lib/y2024/input/day13.txt
Binary file not shown.
8 changes: 8 additions & 0 deletions test/y2024/day13_test.exs
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

0 comments on commit b5b858b

Please sign in to comment.