And we’re back! The interview club was graciously down to hack on Advent of Code 2023 Day 3 🙏🏼
Here’s the input for the puzzle:
The task is to add up all numbers that are touching a “symbol”, even if it’s diagonally. Below is the raw initial planning and psuedocode we came up with as a group:
After another 20 minutes:
Major Gotchas
- newline characters (
\n
)! These threw all my indexes off, but by 1 after every additional line. So that meant that the first character indexes were correct - The digit loop: this one is more obvious, and it didn’t occur to me and I even have a false memory of writing it but at one point I realized I left it out.
matchAll
helps me out by finding the starting index of every contiguous group of digits, but it of course only provides the first index. I need to check each digit of it. This isn’t very hard to do, but of course I need to be remembered
Things I Learned
Array.from(someString.matchAll(/[]/g))
is really useful for a case like this- Calculating
x,y
neighbor coordinates using a single array really isn’t harder than using a double nested loop. Actually, it honestly feels simpler after doing it. Bonus points, it’s a bit more performant. - Named loops - kinda cool! Much simpler to use than I thought, and they respect not only
break
but alsocontinue
- I am a bit liable to lose the play - Krishna and I sort of co-created a cool idea to have these sister data structures where
- a) was a list of numbers with the index of each of their digits
- b) was a list of indexes of digits that mapped back to the numbers
- I thought this was pretty cool, but totally forgot about it and didn’t end up using it.
- If we create an array out of the iterators that regex.matchAll returns, like from
'123'.matchAll(/\d+/g)
, it returns an object that sort of appears to have properties of box an object and an array! It has anindex
property, but it also can be indexed…seemingly. It can be indexed from[0]
anyways. But uh, that’s cause it has a0
property:- Not really sure what this is about.
BUT I SPEAK TOO SOON! I still didn’t get it
My solution worked for my test case but not for the full case.
So…Krishna came up with some good edge cases at the start of our collab. One was a symbol having multiple numbers - this isn’t an issue with the current algo, since each digit is potentially checked, and the symbols aren’t removed.
The other was a number having multiple symbols, or being repeated. Unless I misunderstood the instructions, I don’t think this is the case either. However it’s pretty likely I am missing something about the instructions.
Also, Advent said my answer was too low.
Welp. It turns out I WASN’T cleaning out the newlines after all somehow.
Didn’t do it…but I’m not sure why. Because it seemed to have done the trick on the test data. Does anyone have any ideas on why this is?
This also doesn’t work by the way:
Why not?
Looks like I misunderstood what this does. This only removes whitespace from the beginning and the end of javascript strings.
THIS however, does work:
And voilè! That submission worked. Hm. Still can’t explain why the data.replace('\n', '')
seemed to work for the test input and didn’t for the final input. Does anyone else know?
I learned that String.replace()
will only replace the first occurrence of a value. What I was reaching for was String.replaceAll()
.
The reason the regex here does work is because it is using the g
, or global
flag. Without this flag, replace used with a regex expression would do the same thing.
I actually realized one-off JS function is a bit of a weakness of mine, so I wanted to list them out.
These will only return a single item by default:
Whereas here are their multi-element counterparts:
Situations where a multi-element function can be modified to be a single element function: