DEV Community

Cover image for Timing is Everything
Robert Mion
Robert Mion

Posted on

Timing is Everything

Advent of Code 2016 Day 15

Part 1

  1. A LCM puzzle?
  2. What better way than to just try counting up?

A LCM puzzle?

LCM: Least Common Multiple

  • The smallest number shared by several numbers as that which they evenly divide by

3,5 = 15
2,5 = 10

In this case, the numbers may be some combination of the time passed, the total positions, and the current position of each disc.

What better way than to just try counting up?

Create a variable, answer
Create a counter, starting at 0
Do as long as answer has no value
  Determine whether each disc would be at position 0 when reached
  If so
    Set answer to counter
  Else
    Increment counter
Enter fullscreen mode Exit fullscreen mode

What is the equation I'll use to determine whether a disc is at position 0?

(time + disc number + position) % positions == 0
Enter fullscreen mode Exit fullscreen mode

For example:
Animation of my planned algorithm

So, as long as each disc's equation becomes 0 for a time, then I found a winning time!

This algorithm seems easy enough to write.

Here I go!

...

It worked!

Part 2

Easiest Part 2 ever?

  • If I am reading this correctly, I just need to alter my puzzle input so it includes an extra disc
  • Then run my algorithm again...hoping that this extra disc doesn't cause the first time to press the button to become a number in the billions or trillions

After adding this line to my puzzle input:

Disc #7 has 11 positions; at time=0, it is at position 0.
Enter fullscreen mode Exit fullscreen mode

And running my algorithm again...

It almost immediately generated a number.

It is the correct answer!

I did it!!

  • I solved both parts!
  • Way faster - and with far less code and confusion - than I expected!
  • Perhaps because I deduced the equation so quickly!

I'm delighted that this Day 15 puzzle took me under 15 minutes to solve both parts!

Though, given the effort required of me to solve the last few puzzles, I'm a bit bummed that I probably won't remember this one among after moving on.

Top comments (0)