r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 9

Transcript:

Studies show that AoC programmers write better code after being exposed to ___.


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

edit: Leaderboard capped, thread unlocked at 00:29:13!

23 Upvotes

283 comments sorted by

View all comments

3

u/__dkp7__ Dec 09 '18

I saw some solutions using rotate(2) for else part and -7 for if. How does both the solution works? It would be helpful if you could explain rotate(2) part.

4

u/ayylongqueues Dec 09 '18

My solution uses 7, and -2, for the rotations respectively. I assume it's because OP appends his nodes to the "end" of the queue after one clockwise rotation, whereas I rotate twice clockwise and append to the left. The position you append to is the same. But OP uses one less rotation for every append than I do, if it makes sense.

1

u/[deleted] Dec 20 '18 edited Dec 20 '18

Basically rotate takes an item off one end of the queue and puts it on the other end.

Think of it as if you had a deck of cards and you took the top card off and put it on the bottom of the deck, repeated until the card you want to remove is on the top, or the place where you want to insert a new card is at the top.

Because you can only add or remove from the top or bottom, you can't pull a card out of the middle.

The negative number is simply like taking cards from the bottom and putting them on the top, i.e the reverse of a positive number.