What more I've learned from the Advent of Code (days 6-8)
Tags: adventofcode, javascript
Published: 2020-12-13 10:55:16

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. :sweat_smile: Current total: 15:star2:

# 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:
```javascrit
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 :laughing:). I started by separating the operation and argument.
```javascript
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.
```javascript
let [_, sign, number] = argument.match(/(\+|\-)(\d+)/);
```
Using the underscore is the influence of @caiangums. :blush: 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.
```javascript
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:
```javascript
argument = Number.parseInt(argument);
(...)
accumulator += argument;
index += argument;
```
Much cleaner! Can't believe I didn't think of it right away. :facepalm:

# 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:
```javascript
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 `...`.
```javascript
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:
```javascript
switch(operation) {
  case 'acc':
    accumulator += argument;
    index++;
    break;
  case 'jmp':
    index += argument;
    break;
  case 'nop':
    index++;
    break;
}
```

That's all this time! Plodding on. :boot::boot:

<small>Cover photo by <a href="https://unsplash.com/@oo7ab?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Okwaeze Otusi</a> on <a href="https://unsplash.com/s/photos/bags?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></small>