Advent-of-Code/day2/solution-part1.sh
Jonathan Hodgson 96e5cf31fa Day 2
2021-12-03 11:04:53 +00:00

20 lines
436 B
Bash
Executable file

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