Improve day 2
This commit is contained in:
23
02/01.sh
23
02/01.sh
@@ -3,16 +3,19 @@
|
||||
depth=0
|
||||
horizontal=0
|
||||
while read current; do
|
||||
if [[ "$current" == "down "* ]]; then
|
||||
current_tmp=${current/down /}
|
||||
depth=$((depth+current_tmp))
|
||||
elif [[ "$current" == "up "* ]]; then
|
||||
current_tmp=${current/up /}
|
||||
depth=$((depth-current_tmp))
|
||||
elif [[ "$current" == "forward "* ]]; then
|
||||
current_tmp=${current/forward /}
|
||||
horizontal=$((horizontal+current_tmp))
|
||||
fi
|
||||
case "$current" in
|
||||
"down "*)
|
||||
current_tmp=${current/down /}
|
||||
depth=$((depth+current_tmp))
|
||||
;;
|
||||
"up "*)
|
||||
current_tmp=${current/up /}
|
||||
depth=$((depth-current_tmp))
|
||||
;;
|
||||
"forward "*)
|
||||
current_tmp=${current/forward /}
|
||||
horizontal=$((horizontal+current_tmp))
|
||||
esac
|
||||
done
|
||||
|
||||
echo $((depth*horizontal))
|
||||
|
||||
26
02/02.sh
26
02/02.sh
@@ -4,17 +4,21 @@ aim=0
|
||||
depth=0
|
||||
horizontal=0
|
||||
while read current; do
|
||||
if [[ "$current" == "down "* ]]; then
|
||||
current_tmp=${current/down /}
|
||||
aim=$((aim+current_tmp))
|
||||
elif [[ "$current" == "up "* ]]; then
|
||||
current_tmp=${current/up /}
|
||||
aim=$((aim-current_tmp))
|
||||
elif [[ "$current" == "forward "* ]]; then
|
||||
current_tmp=${current/forward /}
|
||||
horizontal=$((horizontal+current_tmp))
|
||||
depth=$((depth+aim*current_tmp))
|
||||
fi
|
||||
case "$current" in
|
||||
"down "*)
|
||||
current_tmp=${current/down /}
|
||||
aim=$((aim+current_tmp))
|
||||
;;
|
||||
"up "*)
|
||||
current_tmp=${current/up /}
|
||||
aim=$((aim-current_tmp))
|
||||
;;
|
||||
"forward "*)
|
||||
current_tmp=${current/forward /}
|
||||
horizontal=$((horizontal+current_tmp))
|
||||
depth=$((depth+aim*current_tmp))
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
echo $((depth*horizontal))
|
||||
|
||||
Reference in New Issue
Block a user