Improve day 2

This commit is contained in:
2021-12-02 11:03:16 +01:00
parent 084205f6d7
commit f4adf8f479
2 changed files with 28 additions and 21 deletions

View File

@@ -3,16 +3,19 @@
depth=0
horizontal=0
while read current; do
if [[ "$current" == "down "* ]]; then
case "$current" in
"down "*)
current_tmp=${current/down /}
depth=$((depth+current_tmp))
elif [[ "$current" == "up "* ]]; then
;;
"up "*)
current_tmp=${current/up /}
depth=$((depth-current_tmp))
elif [[ "$current" == "forward "* ]]; then
;;
"forward "*)
current_tmp=${current/forward /}
horizontal=$((horizontal+current_tmp))
fi
esac
done
echo $((depth*horizontal))

View File

@@ -4,17 +4,21 @@ aim=0
depth=0
horizontal=0
while read current; do
if [[ "$current" == "down "* ]]; then
case "$current" in
"down "*)
current_tmp=${current/down /}
aim=$((aim+current_tmp))
elif [[ "$current" == "up "* ]]; then
;;
"up "*)
current_tmp=${current/up /}
aim=$((aim-current_tmp))
elif [[ "$current" == "forward "* ]]; then
;;
"forward "*)
current_tmp=${current/forward /}
horizontal=$((horizontal+current_tmp))
depth=$((depth+aim*current_tmp))
fi
;;
esac
done
echo $((depth*horizontal))