/
11b.pl
executable file
·44 lines (37 loc) · 950 Bytes
/
11b.pl
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
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use enum qw( UP RIGHT DOWN LEFT );
use lib '.';
use Intcode;
my %panel = ('0:0' => 1);
my ($x, $y) = (0, 0);
my $direction = UP;
my $intcode = 'Intcode'->new;
$intcode->load(split /,/, <>);
$intcode->pause(1);
until ($intcode->finished) {
$intcode->input($panel{"$x:$y"} // 0);
$intcode->run;
my $paint = $intcode->output->[-1];
$intcode->run;
my $turn = $intcode->output->[-1];
$panel{"$x:$y"} = $paint;
$direction += (-1, 1)[$turn];
$direction %= 4;
my $step = { (UP) => [ 0, -1],
(RIGHT) => [ 1, 0],
(DOWN) => [ 0, 1],
(LEFT) => [-1, 0],
}->{$direction};
$x += $step->[0];
$y += $step->[1];
}
my @grid;
for (map [split /:/], grep $panel{$_}, keys %panel) {
$grid[ $_->[1] ][ $_->[0] ] = 1;
}
for my $line (@grid) {
say map $_ ? '#' : ' ', @$line;
}