]> git.sommitrealweird.co.uk Git - advent-of-code-2021.git/blob - day01/count.sh
990433db1ae9f939fb8e4769f126cf6d19903435
[advent-of-code-2021.git] / day01 / count.sh
1 #!/bin/bash
2
3 check=0
4 cur_number=0
5 prev_number=0
6 count=0
7 cur_line=0
8 cur_window=1
9
10 declare -a windows
11
12 for num in $(<input.txt); do
13     cur_line=$((cur_line+1))
14     windows[$cur_window]=$num
15     if [ $cur_window -gt 1 ]; then
16         ((windows[$((cur_window - 1))]+=$num))
17     fi
18     if [ $cur_window -gt 2 ]; then
19         ((windows[$((cur_window - 2))]+=$num))
20     fi
21     ((cur_window+=1))
22
23     case $check in
24         0)
25             check=1
26             cur_number=$num
27             prev_number=$num
28             continue
29             ;;
30         1)
31             cur_number=$num
32             if [ $cur_number -gt $prev_number ]; then
33                 ((count+=1))
34             fi
35             prev_number=$num
36             ;;
37     esac
38 done
39
40 # remove last 2 entries for windows array
41 unset windows[$((cur_window+1))]
42 unset windows[$((cur_window+2))]
43
44 echo "$count is the answer"
45
46 # start from window 2, then we just compare to previous
47 window=2
48 count=0
49
50 while [ $window -le ${#windows[@]} ]; do
51     if [ ${windows[$((window-1))]} -lt ${windows[$window]} ]; then
52         ((count+=1))
53     fi
54     ((window+=1))
55 done
56
57 echo "$count is the 3 value sum answer"