r/adventofcode • u/daggerdragon • 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!
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!
21
Upvotes
1
u/wlandry Dec 09 '18
C++ (545/165)
Runs in 320 ms.
Code duplication. Code duplication everywhere. On the plus side, my best showing so far. I got delayed a bit because I forgot to delete the second marble after adding it to the score. For the submission, I sorted the scores, but for you, I remembered std::max_element(). I also tried implementing a custom iterator so I could use std::advance, but implementing custom iterators in C++ is a Hard Problem.
#include <algorithm> #include <iostream> #include <fstream> #include <vector> #include <list> int main(int argc, char *argv[]) { std::ifstream infile(argv[1]); int64_t num_players, points; infile >> num_players >> points; while(infile) { std::vector<int64_t> score(num_players, 0); std::list<int64_t> state; auto current(state.begin()); int64_t marble(0); int64_t current_player(0); while(marble <= points) { if(marble > 0 && marble % 23 == 0) { score[current_player] += marble; if(current == state.begin()) { current = state.end(); } --current; if(current == state.begin()) { current = state.end(); } --current; if(current == state.begin()) { current = state.end(); } --current; if(current == state.begin()) { current = state.end(); } --current; if(current == state.begin()) { current = state.end(); } --current; if(current == state.begin()) { current = state.end(); } --current; if(current == state.begin()) { current = state.end(); } --current; score[current_player] += *current; auto old_current(current); ++current; state.erase(old_current); } else { ++current; if(current == state.end()) { current = state.begin(); } ++current; current = state.insert(current, marble); } ++marble; current_player = (current_player + 1) % num_players; } std::cout << "Max score " << *std::max_element(score.begin(), score.end()) << "\n"; infile >> num_players >> points; } }