DEV Community

Cover image for Full of Hot Air
Robert Mion
Robert Mion

Posted on

Full of Hot Air

Advent of Code 2022 Day 25

Part 1

  1. And finally, another math problem
  2. Creating an array of powers of five
  3. Walking through the small sample
  4. Validating with both sample lists
  5. Turning the SNAFU knobs to find my answer

And finally, another math problem

  1. Decipher each powers-of-five code into a power-of-10 decimal
  2. Find the sum of all decimals
  3. Cipher that decimal back into a powers-of-five code

My gut tells me:

  • I think I can write an algorithm to do #1
  • I know I can write an algorithm to do #2
  • I doubt I can write an algorithm to do #3

If I can't solve #3 algorithmically, maybe it will be doable using my trusty calculator and some process of elimination.

Creating an array of powers of five

The first 10 powers of five are:

1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125
Enter fullscreen mode Exit fullscreen mode

I know that because I wrote this algorithm:

new Array(10).fill(null).map((_,i) => 5 ** i)
Enter fullscreen mode Exit fullscreen mode

Walking through the small sample

The SNAFU is:

2=-01
Enter fullscreen mode Exit fullscreen mode

There are five digit places.

I'll need an array with the first five powers of five, in reverse order from above:

let SNAFU = '2=-01'
let powers = new Array(SNAFU.length).fill(null).map((_,i) => 5 ** i).reverse()
Enter fullscreen mode Exit fullscreen mode

Before I can perform the necessary math, I need to convert the = and - into their corresponding amounts:

2 1 0  -  =
2 1 0 -1 -2
Enter fullscreen mode Exit fullscreen mode

My algorithm in JavaScript:

SNAFU = SNAFU.split('').map(char => !isNaN(char) 
  ? +char 
  : ['=','-'].indexOf(char) - ['=','-'].length
)
Enter fullscreen mode Exit fullscreen mode

That last part is clever, I think:

  • I make use of the index of the character and the length of the two-item array to return the number I seek: 1 - 2 = -1 for - and 0 - 2 = -2 for =

Lastly, I need to calculate each digit place - by multiplying the digit by the corresponding power - and sum each product:

SNAFU.reduce(
  (sum, digit, index) => sum += digit * powers[index]
  , 0
)
Enter fullscreen mode Exit fullscreen mode

I successfully generated 976 for the SNAFU 2=-01!

Validating with both sample lists

Will my SNAFU-deciphering algorithm hold up to these lists?

I had to adjust my algorithm to work with an input.

My algorithm in JavaScript:

input
  .split('\n')
  .map(SNAFU => {
    SNAFU = SNAFU.split('').map(char => !isNaN(char) 
      ? +char 
      : ['=','-'].indexOf(char) - ['=','-'].length
    )
    let powers = new Array(SNAFU.length).fill(null).map((_,i) => 5 ** i).reverse()
    return SNAFU.reduce(
      (sum, digit, index) => sum += digit * powers[index]
      , 0
    )
  })
  .join('\n')
Enter fullscreen mode Exit fullscreen mode

It worked! I saw the expected decimals for each SNAFU in both lists!

Turning the SNAFU knobs to find my answer

The deciphered SNAFUs in my puzzle input summed to this final decimal:

30223327868980
Enter fullscreen mode Exit fullscreen mode

Five to the nineteenth power is:

19073486328125
Enter fullscreen mode Exit fullscreen mode

Thus, the SNAFU I need to find has 20 digit places:

????????????????????
Enter fullscreen mode Exit fullscreen mode

And so began the tedious guessing game that was manually finding the winning combination of 2, 1, 0, -, = to generate my large decimal number.

How it went:

  • Under
  • Over
  • Still over
  • Under
  • Still under
  • A little over

You get the picture.

Eventually, I found my winning SNAFU:

2=0=02-0----2-=02-10
Enter fullscreen mode Exit fullscreen mode

Submitted...

It was the correct answer!

I did it!!

  • I solved Part 1!
  • Using an algorithm and process of elimination!

I got my 34th gold star!

I tied my record for lowest star count!

Year in review

  • 34 stars earned (tie for lowest)
  • 14 two-star days
  • 6 one-star days
  • 5 zero-star days
  • 5 simulators built
  • Tons of GIFs created

Map of 2022

Yearly star counts

  • 312/400: 78% - C (Passing grade!)

Yearly star counts

200 'days' later

Countless hours of:

  • Practice
  • Struggle
  • Critical thinking
  • Trial and error
  • Debugging
  • Frustration
  • Confusion
  • Acceptance
  • Grit
  • Persistence
  • Patience
  • Adrenaline
  • Delight
  • Sheer glee

It's been a wonderful way to spend my free time throughout 2022.

I encourage you to do the same!

Top comments (0)