day03.pl (1201B) - raw
1 #!/usr/local/bin/perl 2 # day 2015-03 3 4 use strict; 5 use warnings; 6 use v5.32; 7 8 @ARGV = "input" unless @ARGV; 9 10 chomp( my $input = do { local $/; <> } ); 11 12 my @directions = split //, $input; 13 my %houses; 14 my ($x, $y) = (0, 0); 15 $houses{ $x, $y } += 1; # starting house gets a present 16 17 foreach ( @directions ) { 18 $y += 1 if $_ eq '^'; 19 $x += 1 if $_ eq '>'; 20 $y -= 1 if $_ eq 'v'; 21 $x -= 1 if $_ eq '<'; 22 $houses{ $x, $y } += 1; 23 } 24 25 say "part 1: ", scalar grep { $_ > 0 } values %houses; 26 27 # Solution for Part 2 28 undef %houses; 29 my ($santa_x, $santa_y, $robot_x, $robot_y) = (0, 0, 0, 0); 30 $houses{ 0, 0 } = 2; 31 32 for ( my $i = 0; $i < (scalar @directions) - 1; $i += 2 ) { 33 $santa_y += 1 if $directions[ $i ] eq '^'; 34 $santa_x += 1 if $directions[ $i ] eq '>'; 35 $santa_y -= 1 if $directions[ $i ] eq 'v'; 36 $santa_x -= 1 if $directions[ $i ] eq '<'; 37 $houses{ $santa_x, $santa_y } += 1; 38 $robot_y += 1 if $directions[ $i + 1 ] eq '^'; 39 $robot_x += 1 if $directions[ $i + 1 ] eq '>'; 40 $robot_y -= 1 if $directions[ $i + 1 ] eq 'v'; 41 $robot_x -= 1 if $directions[ $i + 1 ] eq '<'; 42 $houses{ $robot_x, $robot_y } += 1; 43 } 44 45 say "part 2: ", scalar grep { $_ > 0 } values %houses;