-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday19.pl
More file actions
61 lines (52 loc) · 1.14 KB
/
day19.pl
File metadata and controls
61 lines (52 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use strict;
use Term::ANSIColor;
use Win32::Console::ANSI;
use Time::HiRes qw/time/;
use v5.14;
my $time = time;
my $filename = "${0}_input";
open(my $fh, $filename);
my @grid;
while(my $input = <$fh>){
my @line = split "", $input;
push @grid, [@line];
}
my $x = 0, my $y = 0;
while(1){
if($grid[0][$x] ne " "){
last;
}
$x++;
}
my @text;
my $direction = 3; # 0 = right, 1 = left, 2 = up, 3 = down
my $steps = 0;
while(1){
if($direction == 0){ # right
$x++;
} elsif($direction == 1){ # left
$x--;
} elsif($direction == 2){ # up
$y--;
} elsif($direction == 3){ # down
$y++;
}
$steps++;
my $location = $grid[$y][$x];
if($location eq "+"){
changeDirection();
} elsif($location eq " "){
last;
} elsif ($location =~ /[A-Z]/){
push @text, $location;
}
}
sub changeDirection {
if($direction < 2){ # currently going left or right
$direction = ($grid[$y+1][$x] ne " ") ? 3 : 2 ;
} else {
$direction = ($grid[$y][$x+1] ne " ") ? 0 : 1 ;
}
}
print "The path is ", colored( join("", @text) , "black on_red" ), ".\n";
print "The packet takes ", colored( $steps , "black on_red" ), " steps. ( ", sprintf ("%.3f",time - $time) ," s )\n";