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))