Advent of Code 2015 Day 6
Why not Day 18?
- Day 18 immediately references Day 6
- Both are light-grid-themed puzzles
- I'm confident I will earn two stars out of four, and hopeful I can earn all four
- Let's get to it!
Part 1
- Light-grid puzzle? You know what that means!
- Double-checking the rules, in case of an unexpected rule variation
- The tools I'll use to solve this puzzle
- Writing a working algorithm
Light-grid puzzle? You know what that means!
- Nested arrays of binary values
- Nested
for
loops to toggle values - An adjacent-cell-checking algorithm
- A simulator to watch the light show
Double-checking the rules, in case of an unexpected rule variation
Nothing unexpected:
- 1000 rows, each with 1000 cells containing binary values that start as 0
- Instructions that indicate whether to make all values within a range 0, 1, or the opposite of the current value
- A checkpoint to determine whether my algorithm can follow the rules
The tools I'll use to solve this puzzle
Time to write this program, using the following tools:
-
Array
methods to determine the type of instruction -
regex
to extract the four coordinates defining each rectangular boundary -
new Array()
to create the 1000x1000 grid - nested
for
loops for each instruction to manipulate the lights
Writing a working algorithm
Generating the 1000x1000 grid:
let grid = new Array(1000) .fill(null) .map( el => new Array(1000) .fill(null) .map(el => 0) )
Within each iteration through the list of instructions, extracting the four corners of each region:
let [minX, minY, maxX, maxY] = [ ...instruction.matchAll(/\d+/g) ].map(el => +el[0])
Control flow based on the type of instruction:
switch (instruction.split(' ')[1]) { case 'on': // turn on break; case 'off': // turn off break; default: // toggle }
Changing each cell's value:
for (let row = minY; row <= maxY; row++) { for (let col = minX; col <= maxX; col++) { // turn on grid[row][col] = 1 // turn off grid[row][col] = 0 // toggle grid[row][col] = 1 - grid[row][col] } }
Counting all the lights that are on:
return [ ...grid.map(row => row.join('')).join('').matchAll(/1/g) ].length
When run altogether, it generated the correct answer!
Part 2
- Feeling a bit disappointed
- Updating four lines of code
Feeling a bit disappointed
- I was hoping the lights would reveal a message or picture
- Instead, the lights just get slightly more complicated
- And it's still a counting game
Updating four lines of code
Changing each cell's value:
for (let row = minY; row <= maxY; row++) { for (let col = minX; col <= maxX; col++) { // turn on grid[row][col]++ // turn off grid[row][col] = grid[row][col] == 0 ? 0 : grid[row][col] - 1 // toggle grid[row][col] += 2 } }
Counting all the lights that are on:
return grid.reduce( (total, row) => total += row.reduce( (sum, brightness) => sum + brightness, 0 ), 0 )
As expected, it generated the correct answer!
I did it!!
- I solved both parts!
- I used several algorithmic tools that I've become more familiar with throughout this journey!
- I opted not to make a simulator, since nothing about the puzzle indicates that seeing the lights reveals anything interesting!
I already know Day 18 is a bit more challenging.
I'm still hopeful I can earn all four stars from these two Days!
Top comments (0)