rugyoga
Advent of Code 2024 - Day 13
First Post!
dpreston
I wrote the simple algebraic solution that Part 2 needed first, but assumed it was going to miss the cheapest solution if there were multiple, so I ran an exhaustive search to solve Part 1.
That was obviously not going to be suitable for Part 2 so I went back to see how much massageing it was going to need, and the answer was None!
def parse(input) do
input
|> String.split("\n", trim: true)
|> Enum.chunk_every(3)
|> Enum.map(fn [a, b, p] ->
[ax, ay] =
String.split(a, ["Button A: X+", ", Y+"], trim: true) |> Enum.map(&String.to_integer/1)
[bx, by] =
String.split(b, ["Button B: X+", ", Y+"], trim: true) |> Enum.map(&String.to_integer/1)
[px, py] =
String.split(p, ["Prize: X=", ", Y="], trim: true) |> Enum.map(&String.to_integer/1)
{{ax, ay}, {bx, by}, {px, py}}
end)
end
def part1(input) do
input
|> parse()
|> Enum.map(fn {{ax, ay}, {bx, by}, {px, py}} ->
na = div(by * px - bx * py, by * ax - bx * ay)
nb = div(ay * px - ax * py, ay * bx - ax * by)
x = na * ax + nb * bx
y = na * ay + nb * by
if x == px and y == py do
3 * na + nb
else
0
end
end)
|> Enum.sum()
end
@scale 10_000_000_000_000
def part2(input) do
input
|> parse()
|> Enum.map(fn {{ax, ay}, {bx, by}, {px, py}} ->
na = div(by * (px + @scale) - bx * (py + @scale), by * ax - bx * ay)
nb = div(ay * (px + @scale) - ax * (py + @scale), ay * bx - ax * by)
x = na * ax + nb * bx
y = na * ay + nb * by
if x == px + @scale and y == py + @scale do
3 * na + nb
else
0
end
end)
|> Enum.sum()
end
1
Most Liked
hauleth
Simple Cramer’s rule for solving linear equations, and some small writeup about it.
https://github.com/hauleth/advent-of-code/blob/master/2024/day13.livemd
4
seeplusplus
rvnash
Last Post!
stevensonmt
What is this pattern matching wizardry:
<<ax::2-binary>> <> ", Y+" <> <<ay::2-binary>>
Also your explanation of the linear algebra concepts is fantastic. Thanks for sharing!
1
Popular in Challenges
This topic is about Day 4 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 7 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
It is that time of the year again: Advent of Code 2022 :christmas_tree:
Day 1
Leaderboard:
New
Today’s challenge for me was about using reduce:
defmodule Prob5 do
def move([[h1 | rest] = _list1, list2]) do
[rest, [h1 | list2]...
New
Today’s challenge is quite interesting. I ended up using Zipper to solve this problem. Maybe I overengineered quite a bit.
The data stru...
New
New
I found today a bit tedious: advent-of-code-2024/lib/advent_of_code2024/day15.ex at main · ibarakaiev/advent-of-code-2024 · GitHub.
New
Other popular topics
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










