-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03a.pl
37 lines (35 loc) · 891 Bytes
/
03a.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
# Read input file into %grid
for (<>) {
chomp;
$line++;
@chars = split //;
for ($i = 0; $i < @chars; $i++) {
$grid{"$line,$i"} = $chars[$i];
}
}
# Read part numbers
for ($i = 1; $i <= $line; $i++) {
for ($j = 0; $j <= @chars; $j++) {
$in_number = 1 if $grid{"$i,$j"} =~ /\d/;
if ($in_number) {
# Process next digit
if ($grid{"$i,$j"} =~ /\d/) {
$current = $current * 10 + $grid{"$i,$j"};
# Check adjacent squares for a symbol.
for ($x = -1; $x <= 1; $x++) {
for ($y = -1; $y <= 1; $y++) {
$adjacent = 1 if $grid{($i + $x) . ',' . ($j + $y)} =~ /[^\d.]/;
}
}
}
# Past the end of the number, add it to the sum and reset.
else {
$sum += $current if $adjacent;
$in_number = 0;
$current = 0;
$adjacent = 0;
}
}
}
}
print $sum, "\n";