What more I've learned from the Advent of Code (days 6-8)
Minna N.
Posted on December 13, 2020
After a pretty good start, I got stuck on day 7 for a long time and haven't wanted to spend all my precious little free time with AoC. My day 7 recursion was looking pretty good but produced some false positives which I eventually weeded out manually and subtracted from the total. Part 2 is in shambles and I don't have a clear picture of how it should be solved. I heard people are figuring out the puzzles with pen&paper and I might try that, too.
But! I've managed to solve days 6 and 8 with two stars. 😅 Current total: 15🌟
Nifty datatype: Sets
One way to remove duplicate values from an array is to create a set out of it. Values in sets can only occur once. I used this feature on day 6 part 1: I collected all 'yes' answers from a group in an array first and then created the set:
let groupAsSet = [...new Set(groupArray)];
It was then easy to find the number of unique 'yes' answers from the group by set.length
.
Destructuring vol.2
I enjoyed solving day 8 (at least part 1 😆). I started by separating the operation and argument.
let [operation, argument] = command.split(' ');
For my original solution, this time I used a regular expression with match
method to also separate the sign and digits.
let [_, sign, number] = argument.match(/(\+|\-)(\d+)/);
Using the underscore is the influence of @caiangums. 😊 I saw this usage in his code: the first element in the array that match
returns is the whole matched string for the regex between /.../
, which I have no use for, and using an underscore nicely depicts that.
Next I used the sign and number to calculate changes in either accumulator or program position (index). I wanted to use the ternary operator here.
sign === '+' ? accumulator += Number.parseInt(number) : accumulator -= Number.parseInt(number);
(...)
sign === '+' ? index += Number.parseInt(number) : index -= Number.parseInt(number);
But... I ended up cleaning up my code and just converted the argument to number directly:
argument = Number.parseInt(argument);
(...)
accumulator += argument;
index += argument;
Much cleaner! Can't believe I didn't think of it right away. 🤦
Reminder: Arrays are reference types
On day 8, my solution was to for
loop through the boot code changing one command at a time. First I didn't realize I never "reset" the array at the beginning so I ended up just changing it one line at a time. Then I understood I needed a temporary array to make the one change:
let modifiedCommandArray = commandArray;
The same thing happened again! Then it hit me: oh, right, arrays are reference types so I'm actually modifying the same array but just using a different name. Fixed the code by using the spread operator ...
.
let modifiedCommandArray = [...commandArray];
Reminder: Include break
in your switch
I didn't make this mistake – this time – but I'm quite sure I could and then wonder what's going on. For switch
-case
structure, you usually want to end your case
block with break
or all the remaining code will be executed as well. Whoops!
I'm diggin' what my day 8 switch
structure looks like:
switch(operation) {
case 'acc':
accumulator += argument;
index++;
break;
case 'jmp':
index += argument;
break;
case 'nop':
index++;
break;
}
That's all this time! Plodding on. 👢👢
Cover photo by Okwaeze Otusi on Unsplash
Posted on December 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.