r/adventofcode • • Dec 15 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 15 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 7 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 15: Rambunctious Recitation ---


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:09:24, megathread unlocked!

39 Upvotes

771 comments sorted by

View all comments

1

u/whatadewitt Dec 15 '20 edited Dec 15 '20

With the understanding that 30000000 iterations is going to take some time... I really thought I had this pretty optimized, but it took much longer than I thought it would... can someone help me out here with a way I could have done it better?

JavaScript solution.

https://github.com/whatadewitt/aoc2020/blob/master/day15/chal2.js

1

u/daggerdragon Dec 15 '20

Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

Also, you can always create your own Help thread (and make sure to title it properly!)

1

u/SirCinnamon Dec 15 '20

First thing I spot is that you're storing the starting seed numbers in the same loop you're iterating over the rest in. I did the same at first but it means you're comparing "it < data.length" an extra 29999994 times. You can populate those in their own loop.

But more than that, you're storing twice as much info as you need I think. You're storing the last two times a number has been called, but you can just store the previous time and overwrite it directly with the current. You need a swap variable to calculate the next number making it the new "current" number, but its faster than accessing your nested arrays by a lot I bet.

1

u/whatadewitt Dec 15 '20

Thanks, this all makes a ton of sense.

I generally try to get these done with my morning coffee before I start work, so I often rush a bit...

1

u/SirCinnamon Dec 15 '20

Yeah honestly facing people here posting really clean code and the leaderboards showing insanely fast solve times, its important to remember those arent the same people/programs! At least thats what I try and remember

1

u/ankouno Dec 15 '20

Javascript v8 seems to also have an optimization issue with this sort of code that ends up making adding keys O(n) rather than O(1) as would be expected. Using a pre-allocated array or Map object instead should fix that issue.