From f4adf8f479419e5671bbbd318043d332f0fa9e5e Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Thu, 2 Dec 2021 11:03:16 +0100 Subject: [PATCH] Improve day 2 --- 02/01.sh | 23 +++++++++++++---------- 02/02.sh | 26 +++++++++++++++----------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/02/01.sh b/02/01.sh index 1f8ee3f..bcdb548 100755 --- a/02/01.sh +++ b/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)) diff --git a/02/02.sh b/02/02.sh index 69e76d3..0ce1c77 100755 --- a/02/02.sh +++ b/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))