Compare commits
2 Commits
065b24248e
...
27a0fb8ca1
Author | SHA1 | Date |
---|---|---|
ida schmidt | 27a0fb8ca1 | |
ida schmidt | 6b677b4009 |
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/env bash
|
||||
declare -a possiblegames # create array for possible games
|
||||
declare -a power # and for game's powers
|
||||
cat $1 | while read l; do # read input
|
||||
((i++))
|
||||
possible=1 # assume game can be won
|
||||
echo game $i
|
||||
echo green
|
||||
green=$(echo $l | grep -Po '([0-9]?[0-9](?= green))+' | while read g; do # find all the green cubes
|
||||
echo $g
|
||||
done | sort -n | tail -1)
|
||||
if [[ $green -gt 13 ]]; then # test if the largest cube is bigger than 13
|
||||
possible=0
|
||||
fi
|
||||
|
||||
echo blue # the same sort of structure is used above
|
||||
blue=$(echo $l | grep -Po '([0-9]?[0-9](?= blue))+' | while read b; do # find the blues
|
||||
echo $b
|
||||
done | sort -n | tail -1)
|
||||
if [[ $blue -gt 14 ]]; then # test for above-14ers
|
||||
possible=0
|
||||
fi
|
||||
|
||||
echo red # et cetera
|
||||
red=$(echo $l | grep -Po '([0-9]?[0-9](?= red))+' | while read r; do # find reds
|
||||
echo $r
|
||||
done | sort -n | tail -1)
|
||||
if [[ $red -gt 12 ]]; then
|
||||
possible=0
|
||||
fi
|
||||
|
||||
power[$i]=`echo $green*$blue*$red | bc`
|
||||
|
||||
if [[ possible -eq 1 ]]; then # if game is winnable, mark down the index into the array
|
||||
echo possible
|
||||
possiblegames[$i]=$i
|
||||
else
|
||||
echo impossible
|
||||
fi
|
||||
echo ${possiblegames[@]} # print all the games that are valid
|
||||
echo ${possiblegames[@]} | sed 's/ /+/g' | bc # .. and add all the array elements into the answer!
|
||||
echo ${power[@]} | sed 's/ /+/g' | bc # .. and for game's powers!
|
||||
done
|
||||
|
Loading…
Reference in New Issue