-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4_2.pl
53 lines (46 loc) · 1.42 KB
/
day4_2.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
#!/usr/bin/perl
use v5.34.0;
use feature 'signatures';
no warnings 'experimental::signatures';
my @lines;
my $found = 0;
while (<>) {
chomp;
push @lines, [ split //, $_ ];
}
for my $row_number (0..$#lines) {
my $row = $lines[$row_number];
for my $column_number (0..$#{$row}) {
# MAS left-arm cross
$found++ if (search_diag($column_number, $row_number, \@lines, "M", "MAS"));
# SAM left-arm cross
$found++ if (search_diag($column_number, $row_number, \@lines, "S", "SAM"));
}
}
say "Found $found";
exit 0;
sub search_diag ($column_number, $row_number, $lines, $key, $string) {
# Check for left-arm
if ($column_number <= $#{$lines->[$row_number]} - 2 && $lines->[$row_number]->[$column_number] eq $key) {
my $text = $key;
for my $n (1..2) {
$text .= $lines->[$row_number + $n]->[$column_number + $n];
}
return 0 if ($text ne $string);
} else {
return 0;
}
# Check for right-arm
my $invert = join('', reverse(split //, $string));
for my $target ($string, $invert) {
my $col = $column_number + 2;
my $key = substr($target, 0, 1);
if ($col >= 2 && $lines->[$row_number]->[$col] eq $key) {
my $text = $key;
for my $n (1..2) {
$text .= $lines->[$row_number + $n]->[$col - $n];
}
return 1 if ($text eq $target);
}
}
}