Advent-Of-Code-2024/day/02/part1.sh
2024-12-05 11:51:57 +00:00

66 lines
1.2 KiB
Bash
Executable file

#! /bin/bash
#set -x
FILE_IN="${1}"
safe_count=0
while read -r l; do
echo "reading new line [$l]"
IFS=" "
read -ra line_split <<< "${l}"
unset IFS
# start with the first val
prev_val="${line_split[0]}"
is_safe=1
# if positive, going up, if negative, going down!
is_up_down="$((line_split[1] - line_split[0]))"
if [[ "${is_up_down}" -ge 0 ]]; then
echo "Initially going up"
else
echo "Initially going down"
fi
for v in "${line_split[@]:1}"; do
echo "checking [${v}] against previous [${prev_val}]"
prev_diff=$((v - prev_val))
echo "diff ${prev_diff}"
if [[ "${prev_diff}" -gt 0 ]]; then
echo "going up"
if [[ "${is_up_down}" -le 0 ]]; then
echo "wrong direction"
is_safe=0
fi
else
echo "going down"
if [[ "${is_up_down}" -ge 0 ]]; then
echo "wrong direction"
is_safe=0
fi
fi
if [[ "${prev_diff#-}" -le 0 ]] || [[ "${prev_diff#-}" -ge 4 ]]; then
echo "outside range"
is_safe=0
fi
if [[ "${is_safe}" -ne 0 ]]; then
echo "safe"
else
echo "unsafe"
fi
prev_val=$v
done
safe_count=$((safe_count + is_safe))
done <"$FILE_IN"
echo "final safe count ${safe_count}"