diff --git a/d01/d1p2.sed b/d01/d1p2.sed new file mode 100755 index 0000000..b24a0cb --- /dev/null +++ b/d01/d1p2.sed @@ -0,0 +1,141 @@ +#!/usr/bin/sed -nEf + +x; s/^$/bbbbb/; x # put "50/0" into the hold buffer + +:line +#p # print current line for debug + +# we reached the end of input; print out the number of zeroes we've seen +/^$/ { + s//answer: /; p + g + s/[^-]//g + s/-/a/g + s/aaaaaaaaaa/b/g + s/bbbbbbbbbb/c/g + s/cccccccccc/d/g + s/dddddddddd/e/g + p + + s/e([^e]|$)/1%\1/ + s/d([^d]|$)/1%\1/ + s/c([^c]|$)/1%\1/ + s/b([^b]|$)/1%\1/ + s/a([^a]|$)/1%\1/ + + s/a1/2/; s/a2/3/; s/a3/4/; s/a4/5/; s/a5/6/; s/a6/7/; s/a7/8/; s/a8/9/ + s/b1/2/; s/b2/3/; s/b3/4/; s/b4/5/; s/b5/6/; s/b6/7/; s/b7/8/; s/b8/9/ + s/c1/2/; s/c2/3/; s/c3/4/; s/c4/5/; s/c5/6/; s/c6/7/; s/c7/8/; s/c8/9/ + s/d1/2/; s/d2/3/; s/d3/4/; s/d4/5/; s/d5/6/; s/d6/7/; s/d7/8/; s/d8/9/ + s/e1/2/; s/e2/3/; s/e3/4/; s/e4/5/; s/e5/6/; s/e6/7/; s/e7/8/; s/e8/9/ + s/%//g + + p +} + +# convert L/R to to save letters for digits +s/^R/>/ +s/^L// { + # strip the L|R + s/.(.*)/\1/ + + # append the content of hold buffer to pattern buffer + G; s/\n//g + + # sort the characters + s/([^-]*)(-*)([^-]*)(-+)([^-]*)/\2\4\1\3\5/g + s/([^a]*)(a*)([^a]*)(a+)([^a]*)/\2\4\1\3\5/g + s/([^b]*)(b*)([^a]*)(b+)([^b]*)/\2\4\1\3\5/g + + # add & carry + s/aaaaaaaaaa/b/g + /bbbbbbbbbb/ { # we passed 0 + s/$/-/ # increment accumulator + s/bbbbbbbbbb//g # 100 overflows to 0 + } + h + # s/^([^-]*)(-*)$/"\1"\n\2\n/; p # debug print +} + +# counter-clockwise rotation case +/^