DEV Community

Cover image for Chronal Calibration
Robert Mion
Robert Mion

Posted on

Chronal Calibration

Advent of Code 2018 Day 1

Task: Solve for X where...

Part 1

X = the resulting frequency - starting from 0 - after all of the changes in frequency have been applied
Enter fullscreen mode Exit fullscreen mode

Part 2

X = the first frequency my device reaches twice
Enter fullscreen mode Exit fullscreen mode

Example input

+1
-2
+3
+1
Enter fullscreen mode Exit fullscreen mode

It represents:

  • A series of frequency changes
  • + is an increase in frequency
  • - is a decrease in frequency

Part 1

The three amigos: split(), map(), reduce()

My proposed algorithm:

Generate an ordered list of numbers from the input
For each number
  Accumulate a sum - starting at 0
Return the sum
Enter fullscreen mode Exit fullscreen mode

I'll use split(), map() and reduce() to make this a very simple task:

input.split('\n').map(Number).reduce((a,c) => a + c)
Enter fullscreen mode Exit fullscreen mode

It worked as anticipated, generating a correct answer!

Part 2

Round and around, seemingly forever

Set a list of frequency history, starting with one element: 0
Set frequency as 0
Set index as 0
Do as long as the last item in the list is not found elsewhere in the list
  If index is equal to the length of the list
    Reset index to 0
  Increment frequency by the number change at the current index in the list of frequency changes
  Insert the new frequency at the end of history
  Increment index by 1
Return the last item in the history list
Enter fullscreen mode Exit fullscreen mode

This animation shows how my algorithm works:
Algorithm for Part 2

It took over 30 seconds to run, but it generated the correct answer!

I did it!!

  • I solved both parts!
  • I solved Part 1 using one concise line of code!

Bummers:

  • My algorithm for Part 2 takes a long time to run...longer than I expected
  • I'm not sure how I'd write a more performant algorithm to generate the correct answer in milliseconds instead of seconds

Year in review

2018 progress map

  • I got 35 gold stars!
  • I beat my low score by 1!
  • I made 8 simulators! That's my lowest amount in a year thus far, but each one was a delight to make.
  • This felt like the toughest year
  • I still am frustrated that I didn't solve Day 24. It would have been fun to build that simulator.

Four-year checkpoint

Broader progress map

  • 100 puzzles attempted thus far!
  • Out of 200 stars possible, I've earned 143!
  • That's nearly 75% of the stars!
  • Not bad for someone like me who is not a trained Software Engineer, Computer Scientist or Programmer

Onward, to 2017!

Top comments (0)