Make all scripts able to take a file name, merge parts 1 and 2 of lanternfish in...
[advent-of-code-2021.git] / day02 / sub.sh
1 #!/bin/bash
2
3 filename="${1:-input.txt}"
4
5 exec 3<"$filename"
6 depth=0
7 horizontal=0
8 aim=0
9 aim_depth=0
10 aim_horizontal=0
11
12 while read -a things -u 3; do
13     case ${things[0]} in
14         up)
15             ((depth-=${things[1]}))
16             ((aim-=${things[1]}))
17             ;;
18         down)
19             ((depth+=${things[1]}))
20             ((aim+=${things[1]}))
21             ;;
22         forward)
23             ((horizontal+=${things[1]}))
24             depth_calc=$((things[1]*aim))
25             ((aim_depth+=$depth_calc))
26             ((aim_horizontal+=${things[1]}))
27             ;;
28     esac
29 done
30
31 echo "horz * depth = $((depth*$horizontal))"
32 echo "aimed version = $((aim_depth*$aim_horizontal))"