Home Backend Development Python Tutorial Advent of Code &#- Day Claw Contraption

Advent of Code &#- Day Claw Contraption

Dec 30, 2024 am 06:30 AM

Advent of Code

Day 13: Claw Contraption (Maths, Maths, and more Maths).

Link to Solution

Today's Challenge was done in Python for a change. This choice was made to:
a) test my python / learn more python
b) it looked like a very heavy mathematical puzzle today, so felt Python would be perfect, and I was NOT wrong - it was lightning fast.

Welcome to today's puzzle, a lesson in Maths sad face, I had no idea how to solve this one (Part2), at first I brute forced it, looping over (max of 100 times) until found the right "route".

Which worked fine for part 1 as expected. However, for Part 2 I knew this wasn't going to be the case, so went back and looked for a Mathematical approach, I had a hunch this would have to be what the team were pushing us towards. Googling around, and after a lot of hunting I came across Cramers Rule (first time I'd heard of it tbh).

The task was to:

Calculate the minimum cost to reach the prize using button presses.

For Part 1, determine if it's possible to reach the target with pressing buttons, and if it is what is the most amount of prizes you can successfully win within 100 presses and the cost to do so.

For Part 2, optimise performance by handling large coordinate offsets in essence removing the 100 button press limit, and pushing the prize location into the abyss.

Solution

Cramer's Rule seems to be an excellent approach for solving this problem because it allows you to efficiently solve linear equations that describe how to move the claw to reach the prize in each machine. Let's break down why and how Cramer's Rule applies:

Problem Breakdown

For each claw machine, we have two equations:

Equation 1 (from Button A):
a1 * A b1 * B = c1

Equation 2 (from Button B):
a2 * A b2 * B = c2

Where a1 and b1 are the movements along the X and Y axes for Button A, A is the number of times the A button is pressed, and c1 is the target position on the X axis (prize location).

where a2 and b2 are the movements along the X and Y axes for Button B, B is the number of times the B button is pressed, and c2 is the target position on the Y axis (prize location).

For each claw machine, we want to solve for the number of button presses A and B (the number of times buttons A and B need to be pressed) that will align the claw with the prize at coordinates (c1, c2) on the X and Y axes.

Why Cramer's Rule is Useful

Cramer's Rule is specifically designed to solve systems of linear equations. A system of linear equations is simply a set of two or more equations that share common variables, and the goal is to find values for those variables that satisfy all the equations at once.

In simpler terms:

Imagine you have multiple equations that describe how things are related.

Each equation uses the same variables (e.g., x and y), and you're trying to find values for these variables that make all of the equations true at the same time.

In this case, each machine's button configuration can be represented as a 2x2 system of linear equations, where we are solving for two unknowns, A (button A presses) and B (button B presses).

How would a developer know for the future to use Cramer's Rule ?

System of Equations: The first thing a developer does is identify that the problem requires solving a system of linear equations.

Pattern Recognition: Developers recognise that this is a 2x2 system and that Cramer's Rule is a straightforward way to solve it.

*Determinants and Matrices: * They recall that determinants can be used to solve for the unknowns in linear equations, and if the determinant is zero, it indicates a problem (no or infinite solutions).

Simplicity and Clarity: Cramer's Rule provides a simple, direct method to find the values of A and B without requiring iterative methods or complex algebra.

Example: First Claw Machine

The button movements and prize locations are as follows:

Button A moves the claw X+94, Y+34.
Button B moves the claw X+22, Y+67.
Prize location is at X=8400, Y=5400.
Copy after login
Copy after login

We have the system of equations:

94 * A + 22 * B = 8400   (Equation for X-axis)
34 * A + 67 * B = 5400   (Equation for Y-axis)
Copy after login

Step 1: Calculate Determinants
Main Determinant D:
The determinant D is calculated using the formula:

D = a1 * b2 - a2 * b1
Copy after login

Substituting the values:

D = (94 * 67) - (34 * 22)
D = 6298 - 748
D = 5550
Copy after login

Determinant for A, D_x:
Next, we calculate the determinant D_x using the formula:

D_x = c1 * b2 - c2 * b1
Copy after login

Substituting the values:

D_x = (8400 * 67) - (5400 * 22)
D_x = 562800 - 118800
D_x = 444000
Copy after login

Determinant for B, D_y:
Now, calculate the determinant D_y using the formula:

D_y = a1 * c2 - a2 * c1
Copy after login

Substituting the values:

D_y = (94 * 5400) - (34 * 8400)
D_y = 507600 - 285600
D_y = 222000
Copy after login

Step 2: Solve for A and B
Using Cramer's Rule, we now solve A and B:

A = D_x / D
B = D_y / D
Copy after login

Solve for A:

A = 444000 / 5550
A = 80
Copy after login

Solve for B:

B = 222000 / 5550
B = 40
Copy after login

Step 3: Check for Valid Integers
Both A and B are integers, which means that it is possible to win the prize for this claw machine.

Step 4: Calculate Total Cost
The cost to press Button A is 3 tokens, and the cost to press Button B is 1 token. So, the total cost to win the prize is:

Button A moves the claw X+94, Y+34.
Button B moves the claw X+22, Y+67.
Prize location is at X=8400, Y=5400.
Copy after login
Copy after login

Part2 - uses the same logic the only difference is we add the 10^13 offset to both the X and Y axis of the Prize co-ordinates.

I know that is a lot, and believe it took me a lot to understand / get my head around it too. Happy to have a chat , you can reach me at Twitter.

The above is the detailed content of Advent of Code &#- Day Claw Contraption. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles