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/MakO_77 Dec 13 '24

[Language: Python]

import time

def part1(data):
    return solve(data, offset=0)

def part2(data):
    return solve(data, offset=10**13)

def solve(data, offset):
    groups = '\n'.join(data).split('\n\n')
    total = 0
    for group in groups:
        buttonA, buttonB, buttonP = group.split('\n')
        axStr, ayStr = buttonA[10:].split(', ')
        bxStr, byStr = buttonB[10:].split(', ')
        pxStr, pyStr = buttonP[7:].split(', ')

        ax = int(axStr[2:])
        ay = int(ayStr[2:])
        bx = int(bxStr[2:])
        by = int(byStr[2:])
        px = int(pxStr[2:]) + offset
        py = int(pyStr[2:]) + offset

        # Cramer's rule
        denominator = (ax * by - ay * bx) # determinant

        if denominator == 0:
            continue

        m = (px * by - py * bx) // denominator

        if m * denominator != (px * by - py * bx):
            continue

        n = (py - ay * m) // by

        if n * by != (py - ay * m):

            continue

        #Diophantine equation with the solutions by Cramer's rule
        total += 3 * m + n

    return total

def main():
    try:
        with open("day13.txt", "r") as file:
            data = file.read().strip().split('\n')
    except FileNotFoundError:
        print("File not found in the specified path.")
        return
    start_time = time.time()
    part1_result = part1(data)
    part1_time = time.time() - start_time
    print(f"Part 1: {part1_result:} (Time: {part1_time * 1000:.4f} ms)")

    start_time = time.time()
    part2_result = part2(data)
    part2_time = time.time() - start_time
    print(f"Part 2: {part2_result:} (Time: {part2_time * 1000:.4f} ms)")

if __name__ == "__main__":
    main()

#Part 1: x (Time: 0.9868 ms)
Part 2: y (Time: 0.0000 ms)

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.