26 lines
529 B
Bash
Executable file
26 lines
529 B
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
file="$1"
|
|
|
|
horizontal=0
|
|
depth=0
|
|
aim=0
|
|
|
|
while read line; do
|
|
#direction="$(echo "$line" | cut -d ' ' -f 1)"
|
|
direction="${line%% *}"
|
|
#amount="$(echo "$line" | cut -d ' ' -f 2)"
|
|
amount="${line##* }"
|
|
case "$direction" in
|
|
forward)
|
|
horizontal=$((horizontal + amount))
|
|
depth="$((depth + (aim * amount) ))"
|
|
;;
|
|
down) aim="$((aim + amount))" ;;
|
|
up) aim="$((aim - amount))" ;;
|
|
esac
|
|
done < <(cat "$file")
|
|
|
|
echo "Horizontal: $horizontal"
|
|
echo "Depth: $depth"
|
|
echo "Product: $(( depth * horizontal ))"
|