aboutsummaryrefslogtreecommitdiff
path: root/2015/puzzle-1.sh
blob: 1a5dbc088c171d68b56fcd8d58931ffd9ecca038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env sh
text=$(< 1-input)
open=$(echo $text | tr -d ')' | wc -c)
close=$(echo $text | tr -d '(' | wc -c)

echo "Round 1:" $(($open - $close))

current=0
pos=1
while read -n1 char
do
    case $char in
        "(" )
            current=$(($current + 1))
            ;;
        ")" )
            current=$(($current - 1))
            ;;
    esac
    if [ $current -lt 0 ]
    then
       break;
    fi
    pos=$(($pos + 1))
done < <(printf "%s" $text | tr -d '\n')

echo "Round 2:" $pos

# Local Variables:
# compile-command: "sh puzzle-1.sh"
# End: