solve 2024 day 13 pt. 1
This commit is contained in:
parent
154e278fb3
commit
1866e3aed5
3 changed files with 61 additions and 0 deletions
57
2024/lib/2024/13.ex
Normal file
57
2024/lib/2024/13.ex
Normal file
|
@ -0,0 +1,57 @@
|
|||
import AOC
|
||||
|
||||
aoc 2024, 13 do
|
||||
def p1(input) do
|
||||
input
|
||||
|> read_machines()
|
||||
|> Enum.map(&solve/1)
|
||||
|> Enum.filter(&match?({:solution, a, b} when a <= 100 and b <= 100, &1))
|
||||
|> Enum.map(&tokens_for_solution/1)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
def p2(_input) do
|
||||
end
|
||||
|
||||
defp solve(%{a: {xa, ya}, b: {xb, yb}, goal: {x, y}}) do
|
||||
[a, b] =
|
||||
[[xa, xb], [ya, yb]]
|
||||
|> Nx.tensor()
|
||||
|> Nx.LinAlg.solve(Nx.tensor([x, y]))
|
||||
|> Nx.round()
|
||||
|> Nx.to_list()
|
||||
|> Enum.map(&trunc/1)
|
||||
|
||||
if xa * a + xb * b == x and ya * a + yb * b == y do
|
||||
{:solution, a, b}
|
||||
else
|
||||
:impossible
|
||||
end
|
||||
end
|
||||
|
||||
defp tokens_for_solution({:solution, a, b}) do
|
||||
3 * a + b
|
||||
end
|
||||
|
||||
# defp valid_solution?()
|
||||
|
||||
## input
|
||||
|
||||
defp read_machines(str) do
|
||||
~r/Button A: X\+(\d+), Y\+(\d+)\nButton B: X\+(\d+), Y\+(\d+)\nPrize: X=(\d+), Y=(\d+)/m
|
||||
|> Regex.scan(
|
||||
str,
|
||||
capture: :all_but_first
|
||||
)
|
||||
|> Enum.map(fn strings ->
|
||||
[xa, ya, xb, yb, x, y] =
|
||||
Enum.map(strings, &String.to_integer/1)
|
||||
|
||||
%{
|
||||
a: {xa, ya},
|
||||
b: {xb, yb},
|
||||
goal: {x, y}
|
||||
}
|
||||
end)
|
||||
end
|
||||
end
|
|
@ -23,6 +23,7 @@ defmodule Aoc2024.MixProject do
|
|||
[
|
||||
{:advent_of_code_utils, "~> 4.0"},
|
||||
{:nimble_parsec, "~> 1.4"},
|
||||
{:nx, "~> 0.9.2"},
|
||||
{:styler, "~> 1.2", only: [:dev, :test], runtime: false}
|
||||
]
|
||||
end
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
%{
|
||||
"advent_of_code_utils": {:hex, :advent_of_code_utils, "4.0.1", "591073a49600cbceae6d92e0fb2d2c56b59f6d383851e2a7c00c3bf0e4f33f4f", [:mix], [{:floki, "~> 0.34", [hex: :floki, repo: "hexpm", optional: false]}, {:tz, "~> 0.26", [hex: :tz, repo: "hexpm", optional: false]}], "hexpm", "684d016883d7b5443d9d22abc34013fb6f1d5d9ff114859a40890545a570eec8"},
|
||||
"complex": {:hex, :complex, "0.5.0", "af2d2331ff6170b61bb738695e481b27a66780e18763e066ee2cd863d0b1dd92", [:mix], [], "hexpm", "2683bd3c184466cfb94fad74cbfddfaa94b860e27ad4ca1bffe3bff169d91ef1"},
|
||||
"floki": {:hex, :floki, "0.36.3", "1102f93b16a55bc5383b85ae3ec470f82dee056eaeff9195e8afdf0ef2a43c30", [:mix], [], "hexpm", "fe0158bff509e407735f6d40b3ee0d7deb47f3f3ee7c6c182ad28599f9f6b27a"},
|
||||
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
|
||||
"nx": {:hex, :nx, "0.9.2", "17563029c01bf749aad3c31234326d7665abd0acc33ee2acbe531a4759f29a8a", [:mix], [{:complex, "~> 0.5", [hex: :complex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "914d74741617d8103de8ab1f8c880353e555263e1c397b8a1109f79a3716557f"},
|
||||
"styler": {:hex, :styler, "1.2.1", "28f9e3d4b065c22575c56b8ae03d05188add1b21bec5ae664fc1551e2dfcc41b", [:mix], [], "hexpm", "71dc33980e530d21ca54db9c2075e646faa6e7b744a9d4a3dfb0ff01f56595f0"},
|
||||
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
|
||||
"tz": {:hex, :tz, "0.28.1", "717f5ffddfd1e475e2a233e221dc0b4b76c35c4b3650b060c8e3ba29dd6632e9", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:mint, "~> 1.6", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "bfdca1aa1902643c6c43b77c1fb0cb3d744fd2f09a8a98405468afdee0848c8a"},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue