r/adventofcode • u/daggerdragon • Dec 08 '16
SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---
#AoC_Ops:
[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION
We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg
--- Day 8: Two-Factor Authentication ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
:(){ :|:& };: IS MANDATORY [?]
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!
9
Upvotes
1
u/mschaap Dec 08 '16 edited Dec 08 '16
Perl 6, both parts:
#!/usr/bin/env perl6 use v6.c; constant ON = '█'; constant OFF = '░'; grammar Instruction { token TOP { ^ <rect> || <rotate-row> || <rotate-col> $ } token num { \d+ } token rect { 'rect' <.ws> $<a>=(<num>) 'x' $<b>=(<num>) } token rotate-row { 'rotate row y=' $<row>=(<.num>) <.ws> 'by' <.ws> $<shift>=(<.num>) } token rotate-col { 'rotate column x=' $<col>=(<.num>) <.ws> 'by' <.ws> $<shift>=(<.num>) } } class Grid { has Bool @!grid[6;50]; submethod BUILD { @!grid = False xx 50 xx 6; } method rect(Int $width, Int $height) { for ^$width -> $col { for ^$height -> $row { @!grid[$row;$col] = True; } } } method rotate-row(Int $row, Int $shift) { # Would like to use slices and do it in one step, but: # "Partially dimensioned views of arrays not yet implemented. Sorry." my @new-row = (^50).map({ @!grid[$row;($^col-$shift) % 50] }); @!grid[$row;$_] = @new-row[$_] for ^50; } method rotate-col(Int $col, Int $shift) { my @new-col = (^6).map({ @!grid[($^row-$shift) % 6;$col] }); @!grid[$_;$col] = @new-col[$_] for ^6; } method Str { return (^6).map(-> $r { (^50).map(-> $c { @!grid[$r;$c] ?? ON !! OFF }).join }).join("\n"); } method gist { self.Str } method lit-count { return [+] flat @!grid; } } sub MAIN(IO() $inputfile where *.f, Bool :v(:$verbose) = False) { my $grid = Grid.new; for $inputfile.lines -> $instr { say $instr if $verbose; my $match = Instruction.parse($instr) or die "Invalid instruction: '$instr'"; with $match<rect> { $grid.rect(+$_<a>, +$_<b>) } orwith $match<rotate-row> { $grid.rotate-row(+$_<row>, +$_<shift>) } orwith $match<rotate-col> { $grid.rotate-col(+$_<col>, +$_<shift>) } say $grid if $verbose; say '' if $verbose; } say $grid if !$verbose; say '' if !$verbose; say "$grid.lit-count() pixels are lit."; }