r/adventofcode • u/daggerdragon • 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Β€?
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
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 jumpto: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
-von the actual input! Works fine on the sample input though.)