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

Show parent comment

2

u/sebastiannielsen Dec 09 '18 edited Dec 09 '18

Managed to optimize it GREATLY with some hints from https://www.reddit.com/user/ex_nihilo ... Now it runs under 10 seconds on a i3-4010U.... And also more elegant than using hashref-linked-lists.

#!/usr/bin/perl

$players = 493;
$lastmarble = 7186300;


$currentplayer = 0;

@marbles = ('0');
@playerscore = ();

for ($i = 1; $i < ($lastmarble + 1); $i++) {
$currentplayer++;
if ($currentplayer > $players) {
$currentplayer = 1;
}

if (($i / 23) == int($i / 23)) {
$playerscore[$currentplayer] = $playerscore[$currentplayer] + $i;

for ($z = 0; $z < 7; $z++) {
unshift(@marbles, pop(@marbles));
}
$removed = shift(@marbles);
$playerscore[$currentplayer] = $playerscore[$currentplayer] + $removed;
}
else
{
push(@marbles, shift(@marbles));
push(@marbles, shift(@marbles));
unshift(@marbles, $i);
}

#print "[".$currentplayer."] ";
#foreach $marb (@marbles){
#print $marb." ";
#}
#print "\n";

}


$hscore = 0;
foreach $score (@playerscore) {
if ($score > $hscore) {
$hscore = $score;
}
}

print "Score: $hscore\n";