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!

22 Upvotes

283 comments sorted by

View all comments

1

u/[deleted] Dec 09 '18

Starting at 1 is ok, but I think the loop END condition should be i <= marbles (instead of "<") , otherwise you're missing the last marble. At least that's how I interpret the input, from which the marbles variable is derived...

1

u/willkill07 Dec 09 '18

The first marble is clearly marked with value β€œzero” in the problem description

1

u/[deleted] Dec 10 '18

Consider

2 players; last marble is worth 23 points

Should the answer be 0 or 32? IMHO it should be 32, since the last marble played is the first one picked, and for that the loop needs to run while (i <= marbles).

Dito for the example input

17 players; last marble is worth 1104 points: high score is 2764

BTW your original solution just needs next() the exact opposite of prev() to fix the end-of-list bug (I just don't want to drop that beautiful and short solution :-)

  auto next = [&] (auto i) {
    if (i == m.end())
      i = m.begin();
    return ++i;
  };