Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.