-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-18-star-1.pl
executable file
·153 lines (126 loc) · 3.05 KB
/
day-18-star-1.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl
use warnings;
use 5.036;
use List::Util qw(min);
chomp(my @lines = <>);
my @digs = map { [ split(/ /) ] } @lines;
(my $minX, my $maxX, my $minY, my $maxY) = (0, 0, 0, 0);
(my $x, my $y) = (0, 0);
foreach my $digRef (@digs) {
(my $dir, my $len) = @{$digRef};
if ($dir eq 'R') {
$x += $len;
} elsif ($dir eq 'L') {
$x -= $len;
} elsif ($dir eq 'U') {
$y -= $len;
} elsif ($dir eq 'D') {
$y += $len;
} else {
die "Wrong dir: $dir";
}
if ($x > $maxX) { $maxX = $x }
if ($x < $minX) { $minX = $x }
if ($y > $maxY) { $maxY = $y }
if ($y < $minY) { $minY = $y }
}
# say "upper left: ($minX, $minY), lower right: ($maxX, $maxY)";
(my $width, my $height) = (($maxX - $minX), ($maxY - $minY));
(my $startX, my $startY) = (-$minX, -$minY);
say "Start ($startX, $startY)";
my @lagoonMap;
for my $y (0..$height) {
push(@lagoonMap, [map { '.' } (0..$width)]);
}
# dig the perimeter
($x, $y) = ($startX, $startY);
$lagoonMap[$y][$x] = '#';
foreach my $digRef (@digs) {
(my $dir, my $len) = @{$digRef};
if ($dir eq 'R') {
for my $i (1..$len) {
$x++;
$lagoonMap[$y][$x] = '#';
}
} elsif ($dir eq 'L') {
for my $i (1..$len) {
$x--;
$lagoonMap[$y][$x] = '#';
}
} elsif ($dir eq 'U') {
for my $i (1..$len) {
$y--;
$lagoonMap[$y][$x] = '#';
}
} elsif ($dir eq 'D') {
for my $i (1..$len) {
$y++;
$lagoonMap[$y][$x] = '#';
}
} else {
die "Wrong dir: $dir";
}
}
# foreach my $lineRef (@lagoonMap) {
# my $s;
# foreach my $c (@{$lineRef}) {
# print "$c";
# }
# print "\n";
# }
# dig out the interior
my @queue = &pack($startX + 1, $startY + 1);
my %mem;
while (@queue) {
my $p = shift @queue;
$mem{$p} = 1;
(my $x, my $y) = &unpack($p);
$lagoonMap[$y][$x] = '#';
# up
my $up = &pack($x, $y - 1);
if (!(defined $mem{$up}) && $lagoonMap[$y - 1][$x] eq '.') {
push(@queue, $up);
$mem{$up} = 1;
}
# down
my $down = &pack($x, $y + 1);
if (!(defined $mem{$down}) && $lagoonMap[$y + 1][$x] eq '.') {
push(@queue, $down);
$mem{$down} = 1;
}
# left
my $left = &pack($x - 1, $y);
if (!(defined $mem{$left}) && $lagoonMap[$y][$x - 1] eq '.') {
push(@queue, $left);
$mem{$left} = 1;
}
# right
my $right = &pack($x + 1, $y);
if (!(defined $mem{$right}) && $lagoonMap[$y][$x + 1] eq '.') {
push(@queue, $right);
$mem{$right} = 1;
}
}
# count what was digged
my $count = 0;
foreach my $lineRef (@lagoonMap) {
foreach my $c (@{$lineRef}) {
if ($c eq '#') {
$count += 1;
}
}
}
# foreach my $lineRef (@lagoonMap) {
# my $s;
# foreach my $c (@{$lineRef}) {
# print "$c";
# }
# print "\n";
# }
say $count;
sub pack($x, $y) {
return ($x << 16) | $y;
}
sub unpack($p) {
return (($p >> 16), $p & 0xffff);
}