Robert Mion

Posted on

# Chronal Calibration

## Task: Solve for X where...

### Part 1

``````X = the resulting frequency - starting from 0 - after all of the changes in frequency have been applied
``````

### Part 2

``````X = the first frequency my device reaches twice
``````

## Example input

``````+1
-2
+3
+1
``````

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
``````

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

``````input.split('\n').map(Number).reduce((a,c) => a + c)
``````

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
``````

This animation shows how my algorithm works:

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

• 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

• 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!