Day 2
[advent-of-code-2021.git] / day02 / sub.sh
diff --git a/day02/sub.sh b/day02/sub.sh
new file mode 100644 (file)
index 0000000..b8040aa
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+exec 3<input.txt
+depth=0
+horizontal=0
+aim=0
+aim_depth=0
+aim_horizontal=0
+
+while read -a things -u 3; do
+    case ${things[0]} in
+        up)
+            ((depth-=${things[1]}))
+            ((aim-=${things[1]}))
+            ;;
+        down)
+            ((depth+=${things[1]}))
+            ((aim+=${things[1]}))
+            ;;
+        forward)
+            ((horizontal+=${things[1]}))
+            depth_calc=$((things[1]*aim))
+            ((aim_depth+=$depth_calc))
+            ((aim_horizontal+=${things[1]}))
+            ;;
+    esac
+done
+
+echo "horz * depth = $((depth*$horizontal))"
+echo "aimed version = $((aim_depth*$aim_horizontal))"