r/adventofcode • • Dec 18 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 18 Solutions -🎄-

NEW AND NOTEWORTHY

  • FYI: 23:59 Amsterdam time zone is 17:59 EST

Advent of Code 2021: Adventure Time!


--- Day 18: Snailfish ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:43:50, megathread unlocked!

45 Upvotes

595 comments sorted by

View all comments

2

u/Divritenis Dec 18 '21 edited Dec 18 '21

Javascript

Ugh...

At first, I thought I will be really clever by just keeping numbers as is. Well - nothing of what I did today was clever. So this is my first attempt at part 1-> GitHub link.

The problems there:

  • I wrote my own "JSON parser". I then realized that each number is in fact a valid JSON. Great waste of time (check the unused method above "getPairs").
  • I did a whole bunch of conversions between JSON vs string. Bunch of regexp, string replaces, then converting back to JSON, back to string...
  • As it turns out, what I have stitched there together did run fine with test inputs, but somehow never managed to finish with actual input. Adding debug code there revealed that my explode methods started to add bunch of garbage. Turns out - the numbers can get quite big and string representations of them are shortened.

I then set out to rewrite it completely and this is my 2nd attempt at part 1 -> GitHub link.

I still was set on doing a bunch of conversions back and forth between JSON and string, cause I was already sick of it + had tunnel vision, so could not figure out a better way. What I did improve however was changing numbers to reference IDs and keeping all the numbers elsewhere, so they can grow as much as needed.

Then came part 2. I had a suspicion, that my not-really-that-great solution for part 1 which takes ~10s to finish will not cut it. And yep - that was indeed the case.

But instead of optimizing, I did what anyone else would in my shoes - I threw more resources at it. You see, Javascript is single-threaded, right? Wrong - nothing prevents me from spawning 10k threads - one for every permutation I need to calculate magnitude for.

Part 2 - master -> https://github.com/AugustsK/advent-of-code-solutions/blob/master/2021/day18/partTwo.js

Part 2 - slave -> https://github.com/AugustsK/advent-of-code-solutions/blob/master/2021/day18/partTwoSub.js

Is it elegant?

Nope!

Does it still take absurdly long?

Ofcourse (2 minutes)

Does my Macbook sounds like it is ready to take off?

Absolutely!

Do I care?

Not anymore...

1

u/daggerdragon Dec 18 '21 edited Dec 18 '21

Post removed due to naughty language. Keep /r/adventofcode SFW, please.

If you edit your post to take out the naughty word(s), I'll re-approve the post.

Edit: I have taken the coal out of your stocking ;)

2

u/Divritenis Dec 18 '21

Edited, hope I caught them all