Day 1 part 2

This commit is contained in:
bdnugget committed 2025-12-07 22:21:26 +01:00
1 parent 377667078c
commit ec694c32c7
1 file changed
+11 -15
+11 -15
View File
@@ -9,17 +9,23 @@ main() {
local dial=50
local count_exact_zero=0
local count_passed_zero=0
local prev dir num line
local prev dir num line crossings
while IFS= read -r line; do
prev=$dial
dir="${line:0:1}"
num="${line:1}"
# apply rotation
# apply rotation and count zero crossings before modulo wrap
if [[ "$dir" == L ]]; then
if (( prev == 0 )); then
crossings=$(( num / 100 ))
else
crossings=$(( (num - prev + 100) / 100 ))
fi
dial=$(( dial - num ))
else
crossings=$(( (prev + num) / 100 ))
dial=$(( dial + num ))
fi
@@ -28,21 +34,11 @@ main() {
# count zero hits (for part 1)
if (( dial == 0 )); then
((count_exact_zero+=1))
count_exact_zero=$(( count_exact_zero + 1 ))
fi
# check if we passed 0 (for part 2)
if [[ "$dir" == L ]]; then
# moving left: crossed 0 if new dial < prev
if (( dial > prev )); then
((count_passed_zero+=1))
fi
else
# moving right: crossed 0 if new dial < prev
if (( dial < prev )); then
((count_passed_zero+=1))
fi
fi
# count how many times we passed 0 (for part 2)
count_passed_zero=$(( count_passed_zero + crossings ))
#done < example.txt
done < input.txt