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 depth=0
horizontal=0 horizontal=0
while read current; do while read current; do
if [[ "$current" == "down "* ]]; then case "$current" in
current_tmp=${current/down /} "down "*)
depth=$((depth+current_tmp)) current_tmp=${current/down /}
elif [[ "$current" == "up "* ]]; then depth=$((depth+current_tmp))
current_tmp=${current/up /} ;;
depth=$((depth-current_tmp)) "up "*)
elif [[ "$current" == "forward "* ]]; then current_tmp=${current/up /}
current_tmp=${current/forward /} depth=$((depth-current_tmp))
horizontal=$((horizontal+current_tmp)) ;;
fi "forward "*)
current_tmp=${current/forward /}
horizontal=$((horizontal+current_tmp))
esac
done done
echo $((depth*horizontal)) echo $((depth*horizontal))

View File

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