-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4_1.pl
73 lines (65 loc) · 2.44 KB
/
day4_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
#!/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}) {
# XMAS forwards
$found++ if (search_row($column_number, $row, "XMAS"));
# XMAS backwards
$found++ if (search_row($column_number, $row, "SAMX"));
# XMAS straight down
$found++ if (search_column($column_number, $row_number, \@lines, "X", "XMAS"));
# XMAS straight upwards
$found++ if (search_column($column_number, $row_number, \@lines, "S", "SAMX"));
# XMAS downwards \
$found++ if (search_diag($column_number, $row_number, \@lines, "X", "XMAS", 1));
# XMAS downwards /
$found++ if (search_diag($column_number, $row_number, \@lines, "X", "XMAS", -1));
# XMAS upwards \
$found++ if (search_diag($column_number, $row_number, \@lines, "S", "SAMX", 1));
# XMAS upwards /
$found++ if (search_diag($column_number, $row_number, \@lines, "S", "SAMX", -1));
}
}
say "Found $found";
exit 0;
sub search_row ($column_number, $row, $string) {
return 1 if ($column_number <= $#{$row} - 3 && join('', @$row[$column_number..$column_number + 3]) eq $string);
}
sub search_column ($column_number, $row_number, $lines, $key, $string) {
if ($row_number <= $#{$lines} - 3 && $lines->[$row_number]->[$column_number] eq $key) {
my $text = $key;
for my $n (1..3) {
$text .= $lines->[$row_number + $n]->[$column_number];
}
return 1 if ($text eq $string);
}
}
# direction is +1 for \, -1 for /
sub search_diag ($column_number, $row_number, $lines, $key, $string, $direction) {
if ($direction == 1) {
if ($column_number <= $#{$lines->[$row_number]} - 3 && $lines->[$row_number]->[$column_number] eq $key) {
my $text = $key;
for my $n (1..3) {
$text .= $lines->[$row_number + $n]->[$column_number + $n];
}
return 1 if ($text eq $string);
}
} else {
if ($column_number >= 3 && $lines->[$row_number]->[$column_number] eq $key) {
my $text = $key;
for my $n (1..3) {
$text .= $lines->[$row_number + $n]->[$column_number - $n];
}
return 1 if ($text eq $string);
}
}
}