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))>=0
THEN
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])=0
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])+','
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:
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.