Skip to content

Instantly share code, notes, and snippets.

@AndreasOM
Created December 5, 2015 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasOM/31248080d060dcbc32bb to your computer and use it in GitHub Desktop.
Save AndreasOM/31248080d060dcbc32bb to your computer and use it in GitHub Desktop.
#!/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