From f8ae840a056f490f41bdd48f16f18e75a9a08e06 Mon Sep 17 00:00:00 2001 From: Dominika Date: Thu, 2 Dec 2021 23:37:09 +0100 Subject: [PATCH] + day 2 --- 2_1.sh | 21 +++++++++++++++++++++ 2_2.sh | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 2_1.sh create mode 100755 2_2.sh diff --git a/2_1.sh b/2_1.sh new file mode 100755 index 0000000..0aeef8f --- /dev/null +++ b/2_1.sh @@ -0,0 +1,21 @@ +#!/bin/bash +a=0 +b=0 + +function num() { + echo $2 # could have used awk, but this works ;p +} + +while read line; do + x=$(num $line) + if [[ "$line" == "forward"* ]]; then + a=$((a+x)) + elif [[ "$line" == "up"* ]]; then + b=$((b-x)) + elif [[ "$line" == "down"* ]]; then + b=$((b+x)) + fi +done + + +echo $((b*a)) diff --git a/2_2.sh b/2_2.sh new file mode 100755 index 0000000..eb103bd --- /dev/null +++ b/2_2.sh @@ -0,0 +1,22 @@ +#!/bin/bash +a=0 +c=0 +aim=0 + +function num() { + echo $2 # could have used awk, but this works ;p +} + +while read line; do + x=$(num $line) + if [[ "$line" == "forward"* ]]; then + a=$((a+x)) + c=$((c+aim*x)) + elif [[ "$line" == "up"* ]]; then + aim=$((aim-x)) + elif [[ "$line" == "down"* ]]; then + aim=$((aim+x)) + fi +done + +echo $((c*a))