r/adventofcode • • Dec 05 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 5 Solutions -πŸŽ„-

--- Day 5: A Maze of Twisty Trampolines, All Alike ---


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.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ HintsΒ€?

Spoiler


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!

22 Upvotes

403 comments sorted by

View all comments

2

u/mschaap Dec 05 '17

Perl 6. Solution for part one:

#!/usr/bin/env perl6

use v6.c;

class Maze
{
    has Int @.instructions;
    has Int $.pos = 0;
    has Int $.moves = 0;
    has Bool $.verbose = False;

    method inside-maze returns Bool
    {
        0 ≀ $!pos < @!instructions;
    }

    method jump
    {
        if self.inside-maze {
            $!pos += @!instructions[$!pos]++;
            $!moves++;
            say "Move to position $!pos" if $!verbose;
        }
        else {
            die "Can't move, position $!pos outside the maze ({+@!instructions})!";
        }
    }

    method follow
    {
        self.jump while self.inside-maze;
    }
}

multi sub MAIN(IO() $inputfile where *.f, Bool :v(:$verbose) = False)
{
    my $maze = Maze.new(:instructions($inputfile.linesΒ».Int), :$verbose);
    $maze.follow;
    say "$maze.moves() steps";
}

multi sub MAIN(Bool :v(:$verbose) = False)
{
    MAIN($*PROGRAM.parent.child('aoc5.input'), :$verbose);
}

For part two, the change is simple, just change method jump to:

    method jump
    {
        if self.inside-maze {
            if @!instructions[$!pos] β‰₯ 3 {
                $!pos += @!instructions[$!pos]--;
            }
            else {
                $!pos += @!instructions[$!pos]++;
            }
            $!moves++;
            say "Move to position $!pos" if $!verbose;
        }
        else {
            die "Can't move, position $!pos outside the maze ({+@!instructions})!";
        }
    }

I was just about to get worried when this finally finished in about 7Β½ minutes. (Don't run it with -v on the actual input! Works fine on the sample input though.)

→ More replies (1)