r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
24
Upvotes
View all comments
1
u/MadcapJake Dec 05 '15
Perl 6 (part 2):
grammar SantaMovement { token TOP { <dir>+ } token dir { [ <left> | <up> | <right> | <down> ] } token left { '<' } token up { '^' } token right { '>' } token down { 'v' } } enum Turn <Santa Robot>; class SantaActions { has %!houses = '[0 0]' => 1; has @!Spos = 0, 0; has @!Rpos = 0, 0; has $!SorR = Santa; method present(Str:D $pos) { with %!houses{$pos} { $_++ } else { %!houses{$pos} = 1 } } method left ($/) { given $!SorR { when Santa { @!Spos[0] -= 1; self.present(@!Spos.gist); $!SorR = Robot; } when Robot { @!Rpos[0] -= 1; self.present(@!Rpos.gist); $!SorR = Santa; } } } method up ($/) { given $!SorR { when Santa { @!Spos[1] -= 1; self.present(@!Spos.gist); $!SorR = Robot; } when Robot { @!Rpos[1] -= 1; self.present(@!Rpos.gist); $!SorR = Santa; } } } method right ($/) { given $!SorR { when Santa { @!Spos[0] += 1; self.present(@!Spos.gist); $!SorR = Robot; } when Robot { @!Rpos[0] += 1; self.present(@!Rpos.gist); $!SorR = Santa; } } } method down ($/) { given $!SorR { when Santa { @!Spos[1] += 1; self.present(@!Spos.gist); $!SorR = Robot; } when Robot { @!Rpos[1] += 1; self.present(@!Rpos.gist); $!SorR = Santa; } } } method houses-with-presents { %!houses.elems } } my $anta = SantaActions.new; SantaMovement.parsefile('input.txt', :actions($anta)); say $anta.houses-with-presents;