-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2_1.pl
48 lines (39 loc) · 856 Bytes
/
day2_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
#!/usr/bin/perl
use v5.34.0;
my $safe = 0;
while (<>) {
chomp;
my @levels = split / /;
my $this_safe = 1;
my $dir = 0;
# Check size of gaps
for my $i (1..$#levels) {
last unless $this_safe;
if ($levels[$i - 1] == $levels[$i] ) {
$this_safe = 0;
next;
}
if (abs($levels[$i - 1] - $levels[$i]) > 3) {
$this_safe = 0;
next;
}
my $step_dir = 0;
if ($levels[$i - 1] > $levels[$i]) {
$step_dir = -1;
} else {
$step_dir = 1;
}
if ($dir == 0) {
$dir = $step_dir;
next;
} else {
if ($dir != $step_dir) {
$this_safe = 0;
next;
}
}
}
$safe++ if $this_safe;
}
say $safe;
exit 0;