part2 wip
This commit is contained in:
parent
82c69dfff8
commit
f537bb87d6
1 changed files with 112 additions and 0 deletions
112
day/02/part2.sh
Executable file
112
day/02/part2.sh
Executable file
|
|
@ -0,0 +1,112 @@
|
||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
#set -x
|
||||||
|
|
||||||
|
FILE_IN="${1}"
|
||||||
|
|
||||||
|
safe_count=0
|
||||||
|
|
||||||
|
function debug_echo() {
|
||||||
|
echo "$@" 1>&2
|
||||||
|
}
|
||||||
|
|
||||||
|
function check_is_safe() {
|
||||||
|
local is_up_down="${1}"
|
||||||
|
local first_val="${2}"
|
||||||
|
local second_val="${3}"
|
||||||
|
|
||||||
|
local fun_is_safe=1
|
||||||
|
|
||||||
|
prev_diff=$((first_val - second_val))
|
||||||
|
debug_echo "diff ${prev_diff}"
|
||||||
|
|
||||||
|
|
||||||
|
if [[ "${prev_diff}" -gt 0 ]]; then
|
||||||
|
debug_echo "going up"
|
||||||
|
if [[ "${is_up_down}" -le 0 ]]; then
|
||||||
|
debug_echo "wrong direction"
|
||||||
|
fun_is_safe=0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
debug_echo "going down"
|
||||||
|
if [[ "${is_up_down}" -ge 0 ]]; then
|
||||||
|
debug_echo "wrong direction"
|
||||||
|
fun_is_safe=0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${prev_diff#-}" -le 0 ]] || [[ "${prev_diff#-}" -ge 4 ]]; then
|
||||||
|
debug_echo "outside range"
|
||||||
|
fun_is_safe=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${fun_is_safe}" -ne 0 ]]; then
|
||||||
|
debug_echo "safe"
|
||||||
|
else
|
||||||
|
debug_echo "unsafe"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "${fun_is_safe}"
|
||||||
|
}
|
||||||
|
|
||||||
|
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]))"
|
||||||
|
|
||||||
|
safety_trigger=1
|
||||||
|
|
||||||
|
if [[ "${is_up_down}" -ge 0 ]]; then
|
||||||
|
echo "Initially going up"
|
||||||
|
else
|
||||||
|
echo "Initially going down"
|
||||||
|
fi
|
||||||
|
|
||||||
|
for idx in "${!line_split[@]}"; do
|
||||||
|
[[ "${idx}" -eq 0 ]] && continue
|
||||||
|
echo "index $idx"
|
||||||
|
v="${line_split[${idx}]}"
|
||||||
|
echo "checking [${v}] against previous [${prev_val}]"
|
||||||
|
test_is_safe=$(check_is_safe "${is_up_down}" "${v}" "${prev_val}")
|
||||||
|
|
||||||
|
debug_echo "safe val ${test_is_safe}"
|
||||||
|
|
||||||
|
if [[ "${test_is_safe}" -eq 0 ]]; then
|
||||||
|
# normally we fail this set
|
||||||
|
is_safe=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${test_is_safe}" -eq 0 ]] && [[ "${safety_trigger}" -eq 1 ]]; then
|
||||||
|
# but we let 1 happen if the trigger hasnt... triggered
|
||||||
|
is_safe=1
|
||||||
|
safety_trigger=0
|
||||||
|
if [[ "${idx}" -eq 1 ]]; then
|
||||||
|
# we may have to re-calculate is_up_down!
|
||||||
|
is_up_down="$((line_split[2] - line_split[0]))"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# we skip the re-assign once to 'skip' a number
|
||||||
|
prev_val=$v
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "${is_safe}" -eq 0 ]]; then
|
||||||
|
echo "final check unsafe"
|
||||||
|
else
|
||||||
|
echo "final check safe"
|
||||||
|
fi
|
||||||
|
|
||||||
|
safe_count=$((safe_count + is_safe))
|
||||||
|
done <"$FILE_IN"
|
||||||
|
|
||||||
|
echo "final safe count ${safe_count}"
|
||||||
Loading…
Add table
Reference in a new issue