#!/bin/bash

filename="${1:-input.txt}"

exec 3<"$filename"
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))"
