AoC - Day 2

:date: 2023-12-02 12:00

Part One

I had a sanguine desire to use unix command lines for solutions and today it was still possible. Don't ask why I was using test instead of [ expr ]. Let's just say it's a good opportunity to learn not to name your files test!

Set Bash variable ${I} to be the input file name.

$ while read G;do for N in $( sed 's/[0-9][0-9]* \(blue\|green\)//g' <<<"$G"|cut -d':' -f2|tr -cd '[0-9 \n]' );do if test $N -gt 12;then G='';break;fi;done;test "$G" && echo $G;done <$I|while read G;do for N in $( sed 's/[0-9][0-9]* \(red\|blue\)//g' <<<"$G"|cut -d':' -f2|tr -cd '[0-9 \n]' );do if test $N -gt 13;then G='';break;fi;done;test "$G" && echo $G;done|while read G;do for N in $( sed 's/[0-9][0-9]* \(red\|green\)//g' <<<"$G"|cut -d':' -f2|tr -cd '[0-9 \n]' );do if test $N -gt 14;then G='';break;fi;done;test "$G" && echo $G;done|sed 's/^Game \(.*\):.*$/\1/'|awk '{X+=$1}END{print X}'

2265

That ran in 362ms.

Part Two

Also tractable. This one ran even quicker in 336ms!

$ paste <(while read G;do for N in $( sed 's/[0-9][0-9]* \(blue\|green\)//g' <<<"$G"|cut -d':' -f2|tr -cd '[0-9 \n]' );do echo $N;done|sort -rn|head -n1 ;done <$I) <(while read G;do for N in $( sed 's/[0-9][0-9]* \(blue\|red\)//g' <<<"$G"|cut -d':' -f2|tr -cd '[0-9 \n]' );do echo $N;done|sort -rn|head -n1 ;done <$I) <(while read G;do for N in $( sed 's/[0-9][0-9]* \(red\|green\)//g' <<<"$G"|cut -d':' -f2|tr -cd '[0-9 \n]' );do echo $N;done|sort -rn|head -n1;done <$I)|awk '{X+=$1*$2*$3}END{print X}'

64097