106 lines
2.5 KiB
Bash
106 lines
2.5 KiB
Bash
is_increasing () {
|
|
# Tests if the arguments are monotonally increasing. Returns 1 if
|
|
# monotone increasing; 0 if not.
|
|
local output=1
|
|
local difference=0
|
|
local prev=""
|
|
|
|
for entry in $@; do
|
|
|
|
# Skip the first value since there is nothing to compare against.
|
|
if [ -z $prev ]; then
|
|
prev=$entry;
|
|
continue;
|
|
fi
|
|
|
|
# If an entry is less than or equal to the previous entry, the arguments
|
|
# are not monotonally increasing.
|
|
if [ $entry -le $prev ]; then
|
|
output=0
|
|
break
|
|
fi
|
|
|
|
prev=$entry
|
|
|
|
done
|
|
|
|
echo $output
|
|
|
|
}
|
|
|
|
is_monotone () {
|
|
# Uses is_increasing to check if the arguments are monotonally
|
|
# increasing OR decreasing. Returns 1 if the args are monotone,
|
|
# 0 if not.
|
|
|
|
# Look at the first two entries to see if we should test for
|
|
# increasing or decreasing.
|
|
if [ $2 -gt $1 ]; then
|
|
echo $(is_increasing $@)
|
|
elif [ $1 -gt $2 ]; then
|
|
|
|
# To test for decreasing, simply check if they increase backwards.
|
|
# Replace spaces with line breaks, cat the column in reverse order,
|
|
# then use xargs to parse back into a list of args.
|
|
echo $(is_increasing $(echo $@ | tr ' ' '\n' | tac | xargs))
|
|
else
|
|
|
|
# If the first two entries are equal, then the arguments are not monotone.
|
|
echo 0
|
|
fi
|
|
}
|
|
|
|
is_gradual () {
|
|
# Checks to see if a series of arguments change gradually. If the
|
|
# absolute differences between each entry are 3 or smaller, return 1.
|
|
# Otherwise, returns 0
|
|
|
|
local output=1
|
|
local prev=""
|
|
|
|
for entry in $@; do
|
|
|
|
# Skip the first value since there is nothing to compare against.
|
|
if [ -z $prev ]; then
|
|
prev=$entry;
|
|
continue;
|
|
fi
|
|
|
|
difference=$((entry - prev))
|
|
|
|
if [ $difference -ge 4 ] || [ $difference -le -4 ]; then
|
|
output=0
|
|
break
|
|
fi
|
|
|
|
prev=$entry
|
|
|
|
done
|
|
|
|
echo $output
|
|
|
|
}
|
|
|
|
is_gradual_monotone () {
|
|
# Echoes 1 if the arguments are a gradual monotone series; 0 if not.
|
|
|
|
if [ $(is_monotone $@) -eq 1 ] && [ $(is_gradual $@) -eq 1 ]; then
|
|
echo 1
|
|
else
|
|
echo 0
|
|
fi
|
|
|
|
}
|
|
|
|
main_2_1 () {
|
|
num_gradual_monotone=0
|
|
|
|
while read line; do
|
|
line_is_gradual_monotone=$(is_gradual_monotone $line)
|
|
num_gradual_monotone=$((num_gradual_monotone + line_is_gradual_monotone))
|
|
done <<< $(cat input.txt)
|
|
|
|
echo $num_gradual_monotone
|
|
}
|
|
|
|
# main_2_1 |