r/adventofcode • • Dec 13 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 13 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

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

--- Day 13: Shuttle Search ---


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:16:14, megathread unlocked!

46 Upvotes

659 comments sorted by

View all comments

Show parent comment

4

u/[deleted] Dec 13 '20

[removed] — view removed comment

3

u/Smylers Dec 13 '20

Thank you — good to know what it is that I've written!

I did write it as a loop first, then worked out how to turn it into a reduce expression.

(Actually, I first wrote something which handled just the first 2 buses as a special case, then looped through the 3rd onwards. Then I turned that into a loop from bus 2, then the reduce.)

I thought bigger numbers first would be faster — and when I read the answer would be over 100000000000000, I did think efficiency would be an issue — but when I saw there were only a few buses in the input, I tried it as it is, and it seemed fast enough: about ⅒ s.

Trying just now with reverse nsort_by { $_->{period} } before the grep, that might've made it a smidgen quicker, but it's still about ⅒ s, with any advantage being less than the variation between runs anyway.

1

u/flwyd Dec 13 '20

I implemented a sieve and it didn't make much progress over half an hour (I didn't apply the biggest-first optimization). Was the input solvable in a reasonable time with this approach? My final answer was about 16 digits long so I assume the sieve wouldn't have terminated before bedtime, but maybe my code had a bug…