DEV Community

Cover image for Let It Snow
Robert Mion
Robert Mion

Posted on

Let It Snow

Advent of Code 2015 Day 25

Part 1

  1. First, I need to see more of the picture
  2. Row, then column, then calculate

First, I need to see more of the picture

Similarly to 2017 Day 3's spiral puzzle, I need to see more of this diagonal pattern so I can identify and confirm its underlying row-and-column equations.

Based on the slice in the diagram:

   | 1   2   3   4   5   6  
---+---+---+---+---+---+---+
 1 |  1   3   6  10  15  21
 2 |  2   5   9  14  20
 3 |  4   8  13  19
 4 |  7  12  18
 5 | 11  17
 6 | 16
Enter fullscreen mode Exit fullscreen mode
  • The down-and-to-the-right diagonal numbers grow by multiples of 4
  • Moving right in a row or down in a column increments numbers by an amount one greater than the previously incremented amount, starting from an amount based on the row and column

This animation shows me confirming and discovering the above patterns:
Math underlying the grid

Row, then column, then calculate

  • I need to determine which code will get stored in the row and column specified in my puzzle input
  • Not the actual code right now
  • But in the ordered list of codes that are generated, where mine falls

Thankfully, my animation helped me understand one way to calculate this.

To find the number in column 1 of my target row:

Set row as 1
For i from 1 up to but not including the target row
  Increment row by i
Enter fullscreen mode Exit fullscreen mode

Then, to find the number in my target column:

Set column to row
For i from row + 1 up to but not including column plus the target column
  Increment column by i
Enter fullscreen mode Exit fullscreen mode

Now that I have the number, I know how many times to perform the multiplication, division and remainder:

Set code to 20151125
For i from 1 up to but not including column
  Set code to the remainder after dividing the product of code and 252533 by 33554393
Enter fullscreen mode Exit fullscreen mode

After all three loops, I should have the correct answer!

And that I did!

  • At least, after some fiddling with the amounts
  • I made a few off-by-one errors
  • But having the diagram of initial answers helped me debug my algorithm

My working algorithm in JavaScript:

const part1 = (row, col) => {
  let x = 1
  for (let i = 1; i < row; i++) { x += i }
  for (let i = row + 1; i < row + col; i++) { x += i }
  let code = 20151125
  for (let i = 1; i < x; i++) {
    code = code * 252533 % 33554393
  }
  return code
}
Enter fullscreen mode Exit fullscreen mode

Part 2

Since I start at the last day of the year, Part 2 is never unlocked.

However, it requires I have 49 stars, which I never anticipate fully acquiring.

I did it!

  • I solved Part 1!
  • Using some mathematic patterns instead of brute-forcing a nested array of hundreds of thousands of values!

That was a delightful, diagonal puzzle!

A surprising new direction, much like the spiral one from 2017!

I hope I continue to find novel puzzles in this first year of Advent of Code.

Top comments (0)