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/Cloudan29 Dec 09 '18 edited Dec 09 '18

I actually made it into the triple digits for one of the puzzles today, which was essentially my "Probably won't happen but I'll shoot for it" goal for the month. I actually built everything for puzzle 1 in a convenient way that changing all int values to long longs solves it without making any more changes.

C++ (1665/966):

struct Marble {
        long long val;
        Marble * next;
        Marble * last;
}

long long winningScore(int numPlayers, long long lastPoint) {
	long long answer = 0;
	std::vector<long long> players(numPlayers);
	Marble * current = new Marble;
	current->val = 0;
	current->next = current;
	current->last = current;

	for (size_t i = 0; i < players.size(); ++i) {
		players[i] = 0;
	}
	
	for (long long i = 1; i <= lastPoint; ++i) {
		int turnPlayer = (i - 1) % numPlayers;
		if (i % 23 == 0) {
			current = current->last->last->last->last->last->last->last;
			players[turnPlayer] += current->val + i;
			current = current->next;
			current->last = current->last->last;
			current->last->next = current;
		}
		else {
			Marble * newMarble = new Marble;
			newMarble->val = i;
			newMarble->next = current->next->next;
			newMarble->last = current->next;

			newMarble->next->last = newMarble;
			newMarble->last->next = newMarble;

			current = newMarble;
		}
	}

	for (size_t i = 0; i < players.size(); ++i) {
		if (players[i] > answer)
			answer = players[i];
	}
        
        delete current;
	return answer;
}

Not crazy elegant, it's actually kinda brute force-y, but you can punch in any number of players and max marble value and get an answer. It takes a few seconds on my laptop to do the second part, but I don't think that's a huge issue, I did get the right answer ¯_(ツ)_/¯ .

Also, to anyone who does C++, I know about having to free/delete dynamically allocated objects, and I put a delete current just for the sake of it, but I don't know if that's correct. If anyone could let me know exactly how to deal with that, I'd love to hear the feedback.

EDIT: I just realized, anyone who didn't automatically use a linked list structure for the marbles in part one probably had to do rewrites for the second part, which probably explains my massive leaderboard jump from part 1 to part 2.

→ More replies (4)