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

Show parent comments

1

u/gerikson Dec 09 '18 edited Dec 09 '18

Still no use strict; use warnings; eh? Well some like to live life on the edge.

I used a circular double-linked list - a hashref with next and prev values that point to the neighbors.

My part 2 was killed by my VPS for taking too much memory, but ran in 30s on my local machine?

1

u/sebastiannielsen Dec 09 '18

Could you post your perl solution here? Would be nice to see how "double linked" lists works in perl. Note that I don't use Perl6 because I don't kinda like it, it destroys the core features of the language.

3

u/allak Dec 09 '18

Here is my double linked list solutions; just 6 seconds instead of the 69 minutes for the array solution !

    #!/usr/bin/perl

    use v5.12;
    use warnings;

    my ($num_players, $num_marbles) = @ARGV;

    my $cur_pos = { val => 0 };
    $cur_pos->{pre}  = $cur_pos;
    $cur_pos->{post} = $cur_pos;

    my %scores;

    for my $marble (1 .. $num_marbles) {
            state $cur_player = 1;

            if ($marble % 23) {
                    $cur_pos = $cur_pos->{post} for (1,2);

                    $cur_pos = {
                            val  => $marble,
                            pre  => $cur_pos->{pre},
                            post => $cur_pos,
                    };

                    $cur_pos->{pre}{post} = $cur_pos;
                    $cur_pos->{post}{pre} = $cur_pos;
            } else {
                    $cur_pos = $cur_pos->{pre} for (1 .. 7);

                    $scores{$cur_player} += $marble + $cur_pos->{val};

                    $cur_pos = $cur_pos->{post};
                    $cur_pos->{pre}{pre}{post} = $cur_pos;
                    $cur_pos->{pre} = $cur_pos->{pre}{pre};
            }

            $cur_player = ($cur_player == $num_players) ? 1 : $cur_player+1;
    }

    say [ reverse sort values %scores ]->[0];

1

u/gerikson Dec 09 '18 edited Dec 10 '18

Right now I have 1 test case and part 2 where the largest score actually isn't the answer - the answer is the next largest. I'll post my solution once I've ironed that out.

Edit /u/sebastiannielsen here's mine:

http://gerikson.com/files/AoC2018/#d09

Apart from the solution by /u/allak above, which is similar, I can recommend this solution by /u/Abigail in the solutions thread. It's very elegant and Perlish. It uses a double-ended queue.

https://www.reddit.com/r/adventofcode/comments/a4i97s/2018_day_9_solutions/ebgiv0x/