rugyoga

rugyoga

Advent of Code 2024 - Day 13

Most Liked

hauleth

hauleth

Simple Cramer’s rule for solving linear equations, and some small writeup about it.

seeplusplus

seeplusplus

rvnash

rvnash

Solving a 2x2 is a nice little demo of using Nx as an introduction.

sevenseacat

sevenseacat

Author of Ash Framework

You need to check your reached_target? function, the logic there looks wrong.

At the moment your only “don’t win” case is if you reach 100 presses without winning.

dpreston

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

Where Next?

Popular in Challenges Top

DmitriyChernyavskiy
Hello everyone, I’m a new in elexir and functional language. I’m trying to implement Websocket interraction with server. On first layer...
New
shritesh
This was way too easy after the last few days. Simple map, filter and count.
New
Aetherus
This topic is about Day 5 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
New
maennchen
Ok, that was a rough one today. I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
bjorng
This topic is about Day 14 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
bjorng
This topic is about Day 1 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
Qqwy
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New
seeplusplus
Hello all, hopefully I post this before someone else does and I don’t dupe. IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement