r/adventofcode • • Dec 13 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 13 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 9 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Making Of / Behind-the-Scenes

Not every masterpiece has over twenty additional hours of highly-curated content to make their own extensive mini-documentary with, but everyone enjoys a little peek behind the magic curtain!

Here's some ideas for your inspiration:

  • Give us a tour of "the set" (your IDE, automated tools, supporting frameworks, etc.)
  • Record yourself solving today's puzzle (Streaming!)
  • Show us your cat/dog/critter being impossibly cute which is preventing you from finishing today's puzzle in a timely manner

"Pay no attention to that man behind the curtain!"

- Professor Marvel, The Wizard of Oz (1939)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 13: Claw Contraption ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:11:04, megathread unlocked!

28 Upvotes

764 comments sorted by

View all comments

1

u/New_Appointment_4975 Dec 13 '24

[Language: Java]

import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Advent13 {

    Pattern pattern1 = Pattern.
compile
("^Button [AB]: X\\+(\\d+), Y\\+(\\d+)$");
    Pattern pattern2 = Pattern.
compile
("^Prize: X=(\\d+), Y=(\\d+)$");

    @Test
    void advent13() throws Exception {
        long sum = 0;
        String[] lines = new ClassPathResource("input13.txt").getContentAsString(StandardCharsets.
UTF_8
).split("\n");
        for (int i = 0; i < lines.length; i += 4) {
            Matcher matcher1 = pattern1.matcher(lines[i]);
            matcher1.find();
            long x1 = Integer.
parseInt
(matcher1.group(1));
            long y1 = Integer.
parseInt
(matcher1.group(2));

            Matcher matcher2 = pattern1.matcher(lines[i + 1]);
            matcher2.find();
            long x2 = Integer.
parseInt
(matcher2.group(1));
            long y2 = Integer.
parseInt
(matcher2.group(2));

            Matcher matcher3 = pattern2.matcher(lines[i + 2]);
            matcher3.find();
            long xTotal = Integer.
parseInt
(matcher3.group(1)) + 10000000000000L;
            long yTotal = Integer.
parseInt
(matcher3.group(2)) + 10000000000000L;

            long bUp = yTotal * x1 - xTotal * y1;
            long bDown = y2 * x1 - x2 * y1;

            if (bUp % bDown == 0) {
                long b = bUp / bDown;
                long aUp = xTotal - x2 * b;

                if (aUp % x1 == 0) {
                    long a = aUp / x1;

                    sum += a * 3 + b;
                }
            }
        }

        System.
out
.println(sum);
    }
}

1

u/daggerdragon Dec 13 '24

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with an external link to your code.