:date: 2023-12-06 12:00
Part A is actually harder than part B. I wrote a quick Python script just to decode what they were asking for. But once I could see it, I could pull out all the scaffolding and there was relatively little there — suitable for a command line. But most real problems are not so convoluted and baffling. So needing to untangle it in Python first is usually not necessary.
Here is my unix one-liner solution. Set I=input6
to set input file.
$ paste -d' ' <(sed -ne 's/^T[^0-9]*//p' $I|sed 's/ */ /g'|tr ' ' '\n') <(sed -ne 's/^D[^0-9]*//p' $I|sed 's/ */ /g'|tr ' ' '\n')|while read S;do awk '{for(h=0;h<$1;h++){if(h*($1-h)>$2)b+=1} }END{print b}'<<<$S;done|awk 'BEGIN{X=1}{X*=$1}END{print X}'
1159152
This ran in 38.1ms.
And here's the Unix one-liner for part two.
$ paste -d' ' <(sed -ne 's/^T[^0-9]*//p' $I|sed 's/ *//g') <(sed -ne 's/^D[^0-9]*//p' $I|sed 's/ *//g')|awk '{for(h=0;h<$1;h++){if(h*($1-h)>$2)b+=1} }END{print b}'
41513103
Ran in 4.6ms! Like I say, easier than part one.