#!/usr/bin/perl use v5.35.0; sub safe { return 1 if @_ < 2; # Reports with only one level are deemed safe my $dir = $_[0] <=> $_[1]; # Levels can be either increasing or decreasing return 0 if !$dir; # but not flat for (;;) { return 0 if abs($_[0] - $_[1]) > 3; # Change can't be too fast shift; return 1 if @_ < 2; return 0 if ($_[0] <=> $_[1]) != $dir; # and must be consistent } } sub problem_dampener { return 1 if safe(@_); for (0..$#_) { return 1 if safe(@_[0..$_-1, $_+1..$#_]); } return 0; } my $safe_count = 0; while (<>) { $safe_count++ if problem_dampener(split); } say $safe_count;