Discussion thread for day 7 of the Advent of Code - https://adventofcode.com/2024/day/7
A bit relaxing day😁
Part1
Part2
REGEX_CountMatches([Bin_String],"2")<Length([Target])​
And, sum up the result of part1 and part2!
That was a nice morale boost after Day 6.I'm not normally in the do it all in a formula tool camp but this one very much lent itself to that.
Assorted hints and observations
This one took me a bit to think through, but I'm happy with where I ended up. It was one of those rare days for me where Part 2 came easily after Part 1.
I continue to be SO THANKFUL for dark mode!! You're the best, @NicoleJ!
Channeling @grossal energy today for a formula-style macro-free solution.
Here are the basic building blocks of the expression:
First, make sure there's an additional valid number to check.If that condition is met, then evaluate whether the cumulative result is divisible by the current operand or ends with (||) the current operand. Regardless of whether either of those cases are met, I also add an option to subtract the current operand. This expression turns a single number input into 1-3 output numbers: row-1 divided by current operand, row-1 chopped off current operand, and row-1 subtracted out current operand.IF regex_countmatches([Row-1:Cum result], ',')>1 and tonumber(getpart([Row-1:Cum result],',',1))>=0THEN
IF Mod(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1))),[2])=0 and EndsWith(tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))),tostring([2]))THEN tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))/[2])+','+ tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))-[2])+','+ left(tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))),length(tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))))-length(tostring([2])))+','
ELSEIF Mod(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1))),[2])=0THEN tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))/[2])+','+ tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))-[2])+','
ELSEIF EndsWith(tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))),tostring([2]))THEN left(tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))),length(tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))))-length(tostring([2])))+','+ tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))-[2])+','
ELSE tostring(IIF(isempty([Row-1:Order]),[1],tonumber(getpart([Row-1:Cum result],',',1)))-[2])+','ENDIF
Also, Visual Studio Code is by far my favorite IDE that can also be dumbed down and used as a great text editor. It has handy dandy color coding by language and powerful find/replace features. Using it makes writing complex formulas like this significantly easier!
Solved!
My approach is simple iterative macro solution.
All I did was look for something that met the conditions by taking the right hand side vertically and executing each operator simultaneously for each record. Both Part 1 and Part 2 were completed in about 12 seconds. Since the number of records wasn't that huge, no special ingenuity was required.
However, I wandered around for an hour without realizing that the maching should be made on the last record on the right hand side...
Part2 macro:
This problem is an excellent exercise in algorithms:
Done. WF run in 17 sec for P1 and P2. I need to go back to Day6P2.
Took awhile to find my issue mistake (Tip: Use regex to be more restrict on what to replace, be careful replacing more than intended)
Part 2 is easy for me, adding one more case in IF ELSE solved it.
Day 7 in the bag! An absolutely insane amount of time troubleshooting my part 2 macro, just to find one of my Filter tools wasn't configured properly...
Whole thing runs in 5.8 seconds. Was expecting a lot worse!
Once I realized I should add a Record ID at the beginning to account for duplicate test values this wasn't too bad... was a nightmare trying to sort that problem out to begin with though. Note to self: always add a Record ID...
Day 7. More in spoiler
Still struggling with Day6 Part2 and skipped it for now...
Here is my solution for Day7.
Finally completed.
Update the Text Input tool to change the parameters for Part 1 and 2.
Done.
I'm looking forward to looking at everyone's solutions since I could not figure out a more efficient way to solve this. My part 2 took almost 30 minutes to run
Edit: @ScottLewis did what I was trying to do. But instead of my nightmarish mess, theirs is so nice and beautiful and simple
Part 1:
Part 2:
At first I didn't fully understand the question and was stopping once the target was reached even if there were numbers remaining. Led to a long time spent debugging.
Went down a Mod rabbit hole (mainly because that's how I'd do it in VBA) before deciding the iterative approach would be easier. One of my neater ones, which is nice!
Dare I say I enjoyed this one?
Took tips from @gawa and went the binary route for Part 1.
And thank you Andrew Merrill for helping me understand how Part 2 could work. Solving was then easy once I figured out a way to create ternary numbers (sans macro!)
I went down a couple different paths before landing on this one.
My solution: