Compare commits

...

2 Commits

Author SHA1 Message Date
ida schmidt 27a0fb8ca1 complete day 2 part 2! 2023-12-02 15:43:37 -07:00
ida schmidt 6b677b4009 finish day 2 part 1 2023-12-01 23:58:02 -07:00
1 changed files with 44 additions and 0 deletions

44
day2.sh Executable file
View File

@ -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