Advent of Code 2024 - Day 7

https://adventofcode.com

This is a thread to discuss Day 7 :slight_smile:

https://adventofcode.com/2024/day/7

REMEMBER!
It is VERY important NOT to write any public spoilers :slight_smile:
ALWAYS keep spoiler stuff hidden inside one of these

Summary

This text will be hidden

which you can obtain by clicking on the gear icon

image

Good luck :slight_smile:
FYI one of the features of Advent of Code is that we all get given different inputs so our answers will be different.
The examples are all the same but each of us gets different inputs for us to solve

My Part 1 effort ran for 12 hours and then crashed out at about 20% of the way through it so going to need a different method :frowning:

Finally managed to do Part 1 but had to do it in sections as it crashes about every 40 - 90 iterations :frowning:

Because it took so long I replaced one line with some JavaScript - this sped it up but did nothing to prevent the crashes :frowning:

So I did it in sections and totalled all the sections up in a spreadsheet and I got the right answer :slight_smile:

I re-ran the first section (1-80) without the JS and it gave the same answer as the JS code so I'm treating that as "proof" that my Snap! code would do the same for all the other sections

But it would be very nice if someone else had a go at this one and see if they can come up with a method that
A. Doesn't crash
B Takes < 12 hours to run :slight_smile:
https://snap.berkeley.edu/snap/snap.html#present:Username=cymplecy&ProjectName=aoc2024_07&editMode

I decided to go and look at solutions in other languages and I came across this short one in python

Spoiler

(Day 7 of Advent of Code 2024 (Python) ยท GitHub)

I was intrigued by the line
possible = product("*+", repeat=len(num) - 1)
and searched for what product did with the repeat parameter and worked out it would produce all the combinations of + and *

So, I worked out how to get same effect in Snap!

and then re-did my solution

Which now runs much, much quicker and doesn't crash my browser :slight_smile:

https://snap.berkeley.edu/snap/snap.html#present:Username=cymplecy&ProjectName=aoc2024_07e&editMode

This one is pretty fast, too: 130 millisecond processing time (with 850 lines of "real" puzzle input).

Solution to part 1

Solution to part 2

Replaced the append block from part 1's definition of generate candidates with:

AoC 2024 script pic (2)
(different from + and *, join is not a hyperblock)

Run time: about 30 sec. on my system (a rather basic windows PC ) ... No wonder it takes more run time than part 1: the longest input line has 20 numbers now to be combined in 319 = 1,162,261,467 fashions, versus 219 = 524,888: larger by a factor 2217.

... no JavaScript required, nor Python: just plain old recursion within Snap! :smirk:

Having looked at your elegant code for Part 1 (and working out how it worked using single-stepping) I realised that my 1st solution (that took forever and crashed) was just overly complicated

So I simplified it and it runs in <10 seconds and doesn't crash :slight_smile:

https://snap.berkeley.edu/snap/snap.html#present:Username=cymplecy&ProjectName=aoc2024_07f&editMode

Nice!