Full of Hot Air
Tags: adventofcode, algorithms, javascript, programming
Published: 2023-01-01 19:14:38

## [Advent of Code 2022 Day 25](https://adventofcode.com/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
```

I know that because I wrote this algorithm:
```js
new Array(10).fill(null).map((_,i) => 5 ** i)
```

### Walking through the small sample
The SNAFU is:
```
2=-01
```

There are five digit places.

I'll need an array with the first five powers of five, in reverse order from above:
```js
let SNAFU = '2=-01'
let powers = new Array(SNAFU.length).fill(null).map((_,i) => 5 ** i).reverse()
```

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

My algorithm in JavaScript:
```js
SNAFU = SNAFU.split('').map(char => !isNaN(char) 
  ? +char 
  : ['=','-'].indexOf(char) - ['=','-'].length
)
```

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:
```js
SNAFU.reduce(
  (sum, digit, index) => sum += digit * powers[index]
  , 0
)
```

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:
```js
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')
```

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

Five to the nineteenth power is:
```
19073486328125
```

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

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

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](https://aocsimulators.rmion.repl.co/)
- Tons of GIFs created

![Map of 2022](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm3kc96asydo3of8odpz.png)

## Yearly star counts
- 312/400: 78% - C (Passing grade!)

![Yearly star counts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dmgmz9252pjvdhbcbzh.jpg)

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