First Post!

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

Most Liked

hauleth

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

rvnash

rvnash

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

Last Post!

stevensonmt

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!

Where Next?

Popular in Challenges Top

Aetherus
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
Aetherus
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
woolfred
It is that time of the year again: Advent of Code 2022 :christmas_tree: Day 1 Leaderboard:
New
groovyda
Today’s challenge for me was about using reduce: defmodule Prob5 do def move([[h1 | rest] = _list1, list2]) do [rest, [h1 | list2]...
New
Aetherus
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
igorb
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 Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
dokuzbir
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
greenz1
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
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
jason.o
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

We're in Beta

About us Mission Statement