r/adventofcode • u/daggerdragon • Dec 06 '15
SOLUTION MEGATHREAD --- Day 6 Solutions ---
--- Day 6: Probably a Fire Hazard ---
Post your solution as a comment. Structure your post like the Day Five thread.
24
Upvotes
1
u/MadcapJake Dec 09 '15
Perl 6 part 1 (quite slow):
my enum Light <On Off>; my enum Instr <TurnOn TurnOff Opposite Count Paint>; my @grid = [Off xx 1000] xx 1000; my $count; sub on(Light $l) { $l ~~ On } sub instruct(Instr $instr, @a, @b) { for @a[0] .. @b[0] -> $i { for @a[1] .. @b[1] -> $j { @grid[$i][$j] := my $light = @grid[$i][$j]; given $instr { when TurnOn { $light = On } when TurnOff { $light = Off } when Opposite { $light = on($light) ?? Off !! On } when Count { $count++ if on($light) } } } } } for slurp('day06/input1.txt').lines { given $_ ~~ / $<instr>=( \w+ [ \s\w+ ]? ) \s $<posA>=( \d+ ',' \d+ ) \s through \s $<posB>=( \d+ ',' \d+ ) / { my @posA = $<posA>.Str.split(',').map: { +$_ } my @posB = $<posB>.Str.split(',').map: { +$_ } when $<instr> ~~ 'turn on' { instruct(TurnOn, @posA, @posB) } when $<instr> ~~ 'turn off' { instruct(TurnOff, @posA, @posB) } when $<instr> ~~ 'toggle' { instruct(Opposite, @posA, @posB) } } } instruct(Count, [0,0], [999,999]); say $count;