Created
December 5, 2015 09:17
-
-
Save AndreasOM/31248080d060dcbc32bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# expects data as first parameter | |
# http://adventofcode.com/ | |
# day 1 (1 & 2) | |
# a bit slow, but I wanted to to this in bash ;) | |
up=0 | |
down=0 | |
f=0 | |
p=0 | |
DATA=$1 | |
firstBasementPos=-1 | |
for (( i=0; i<${#DATA}; i++ )) | |
do | |
a=${DATA:$i:1} | |
if [ "$a" == "(" ] | |
then | |
let f=f+1 | |
let up=up+1 | |
else | |
if [ "$a" == ")" ] | |
then | |
let f=f-1 | |
let down=down-1 | |
fi | |
fi | |
let p=p+1 | |
if [[ f -lt 0 ]] && [[ firstBasementPos -eq -1 ]] | |
then | |
let firstBasementPos=p | |
fi | |
done | |
echo UP:$up DOWN:$down FLOOR:$f POS:$p FIRSTBASEMENT:$firstBasementPos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment