add part1 and data

This commit is contained in:
Tom Bloor 2024-12-05 11:51:57 +00:00
parent d86111e630
commit 82c69dfff8
No known key found for this signature in database
GPG key ID: 8775E856E2754827
4 changed files with 1076 additions and 0 deletions

4
day/02/demo_2_input.txt Normal file
View file

@ -0,0 +1,4 @@
1 3 5 6
1 2 3 99
1 3 2 3
9 8 9 10

6
day/02/demo_input.txt Normal file
View file

@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

66
day/02/part1.sh Executable file
View file

@ -0,0 +1,66 @@
#! /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}"

1000
day/02/part_1_input.txt Normal file

File diff suppressed because it is too large Load diff