Compare commits
No commits in common. "9735026df8ab810920b13259f8ce8f356017a877" and "82c69dfff8d9d9905f634857fd69e02ad56c0065" have entirely different histories.
9735026df8
...
82c69dfff8
8 changed files with 0 additions and 123 deletions
|
|
@ -2,9 +2,3 @@
|
||||||
1 2 3 99
|
1 2 3 99
|
||||||
1 3 2 3
|
1 3 2 3
|
||||||
9 8 9 10
|
9 8 9 10
|
||||||
9 10 11 11
|
|
||||||
9 10 10 11
|
|
||||||
9 8 2 7
|
|
||||||
9 8 7 1
|
|
||||||
9 4 10 11
|
|
||||||
9 4 10 11 10
|
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
7 6 4 2 1
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
1 2 7 8 9
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
9 7 6 2 1
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
1 3 2 4 5
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
8 6 4 4 1
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
1 3 6 7 9
|
|
||||||
111
day/02/part2.sh
111
day/02/part2.sh
|
|
@ -1,111 +0,0 @@
|
||||||
#! /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=2
|
|
||||||
|
|
||||||
# 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=$((is_safe - 1))
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "${test_is_safe}" -eq 0 ]] && [[ "${safety_trigger}" -eq 1 ]]; then
|
|
||||||
# but we let 1 happen if the trigger hasnt... triggered
|
|
||||||
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]))"
|
|
||||||
echo "allowing flip up/down"
|
|
||||||
fi
|
|
||||||
echo "-------------------"
|
|
||||||
echo "allowing 1 failure!"
|
|
||||||
echo "-------------------"
|
|
||||||
else
|
|
||||||
# we skip the re-assign once to 'skip' a number
|
|
||||||
prev_val=$v
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ "${is_safe}" -gt 0 ]]; then
|
|
||||||
safe_count=$((safe_count + 1))
|
|
||||||
fi
|
|
||||||
done <"$FILE_IN"
|
|
||||||
|
|
||||||
echo "final safe count ${safe_count}"
|
|
||||||
Loading…
Add table
Reference in a new issue