DEV Community

Cover image for Advent of Code 2024 - Day7: Bridge Repair
Grant Riordan
Grant Riordan

Posted on • Edited on

5

Advent of Code 2024 - Day7: Bridge Repair

Day 7: Bridge Repair

For this solution, I found the best way was using recursion.

What is Recursion?

Recursion is when a function calls itself to solve smaller parts of a problem. It's like solving a big puzzle by breaking it into smaller, identical puzzles. Each time the function calls itself, it works on a smaller piece of the original problem.

How Does Recursion Work?

Recursion has two main parts:

Base Case:

This is when the recursion stops. It's the simplest possible version of the problem. Think of it as the "goalpost." Once reached, the function stops calling itself and starts returning results.

In our case this is when the index == length of the equation parameters.

Recursive Case:

This is when the function calls itself to work on a smaller piece of the problem.

It's like taking a step closer to the base case each time.

How Does the Puzzle Use Recursion?

In the puzzle, the goal is to check if a target number can be made by applying operators (+, *, ||) between a series of numbers.

Here’s the challenge:

Try applying + between the current number and the next.
Try applying * between the current number and the next.
Try combining the two numbers using || (concatenation).

Continue this process until either:

All numbers are used, and the result equals the target (Base Case)
or
All possibilities are explored without finding a match (Recursive Case)

You can find the solution in both Python & C# here

As always feel free to follow me on twitter for more tips, solutions, articles & blog posts across multiple media.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay