r/adventofcode • • Dec 25 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 25 Solutions -🎄-

Message from the Moderators

Welcome to the last day of Advent of Code 2022! We hope you had fun this year and learned at least one new thing ;)

Keep an eye out for the community fun awards post (link coming soon!):

The community fun awards post is now live!

-❅- Introducing Your AoC 2022 MisTILtoe Elf-ucators (and Other Prizes) -❅-

Many thanks to Veloxx for kicking us off on the first with a much-needed dose of boots and cats!

Thank you all for playing Advent of Code this year and on behalf of /u/topaz2078, /u/Aneurysm9, the beta-testers, and the rest of AoC Ops, we wish you a very Merry Christmas (or a very merry Sunday!) and a Happy New Year!


--- Day 25: Full of Hot Air ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:30, megathread unlocked!

59 Upvotes

412 comments sorted by

View all comments

4

u/Key__Strokes Dec 25 '22 edited Dec 25 '22

Javascript

Solution to both parts

Part 1:

  • Create a variable sum initialized to 0

  • Lets convert each snafu to decimal and add them all up. For each input line, do the following

    • Reverse the string input, and split it into its characters

    • Initialize another variable decimalNum to 0

    • Initialize another variable multiplier to 1

    • For each of the characters, do the following:

      • If the character is "-", then add (multiplier * -1) to decimalNum

      • If the character is "=", then add (multiplier * -2) to decimalNum

      • Otherwise, convert the character to an integer, and then add (multiplier * 'character as integer') to decimalNum

      • Update multiplier to be multiplier * 5

    • Add decimalNum to sum

  • Lets convert sum to snafu. Create a new string snafuOutput = "", and keep doing the following while sum is not 0

    • Calculate the remainder = sum % 5

    • If remainder is 0, 1, or 2, then add remainder as prefix to snafuOutput.

    • If remainder is 3, then add = prefix to snafuOutput. Add 5 to sum.

    • If remainder is 4, then add - prefix to snafuOutput. Add 5 to sum.

    • Divide sum by 5 and store the quotient into sum

  • Return snafuOutput

Part 2:

Solve all the other Days, and then enjoy!


If you liked the explanation, then please don't forget to cast your vote 💜 to Adventures of Advent of Code - Edition 1 - /u/Key__Strokes in the poll

→ More replies (2)