## Advent of Code 2022 Day 25

## Part 1

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

### And finally, another math problem

- Decipher each powers-of-five code into a power-of-10 decimal
- Find the sum of all decimals
- 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:

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

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

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

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

```
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
- Tons of GIFs created

## Yearly star counts

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

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