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