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