This commit is contained in:
2025-12-04 17:30:01 -08:00
parent cf72f8d6a2
commit e634b816f8

141
d01/d1p2.sed Executable file
View File

@@ -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/</
# any 100 rotations is a noop, so remove the hundreds
# (but add 1 to the accumulator for each 100 clicks)
/.[0-9][0-9][0-9]/ {
s/^(.)1([0-9][0-9])/\1\2-/
s/^(.)2([0-9][0-9])/\1\2--/
s/^(.)3([0-9][0-9])/\1\2---/
s/^(.)4([0-9][0-9])/\1\2----/
s/^(.)5([0-9][0-9])/\1\2-----/
s/^(.)6([0-9][0-9])/\1\2------/
s/^(.)7([0-9][0-9])/\1\2-------/
s/^(.)8([0-9][0-9])/\1\2--------/
s/^(.)9([0-9][0-9])/\1\2---------/
}
s/(.).*([0-9][0-9])$/\1\2/
# Convert numbers to roman numerals
#s/$/\n/; :lrevloop; s/^(L|R)(.)(.*\n)/\1\3\2/; t lrevloop; s/\n// # reverse
s/[0-9]/%&/g; t dummy1
:dummy1
s/^(.*)%0([^%]*)$/\1\2/g; t num_fwd_tens
s/^(.*)%1([^%]*)$/\1a\2/g; t num_fwd_tens
s/^(.*)%2([^%]*)$/\1aa\2/g; t num_fwd_tens
s/^(.*)%3([^%]*)$/\1aaa\2/g; t num_fwd_tens
s/^(.*)%4([^%]*)$/\1aaaa\2/g; t num_fwd_tens
s/^(.*)%5([^%]*)$/\1aaaaa\2/g; t num_fwd_tens
s/^(.*)%6([^%]*)$/\1aaaaaa\2/g; t num_fwd_tens
s/^(.*)%7([^%]*)$/\1aaaaaaa\2/g; t num_fwd_tens
s/^(.*)%8([^%]*)$/\1aaaaaaaa\2/g; t num_fwd_tens
s/^(.*)%9([^%]*)$/\1aaaaaaaaa\2/g; t num_fwd_tens
:num_fwd_tens
s/^(.*)%0([^%]*)$/\1\2/g; t num_fwd_hundreds
s/^(.*)%1([^%]*)$/\1b\2/g; t num_fwd_hundreds
s/^(.*)%2([^%]*)$/\1bb\2/g; t num_fwd_hundreds
s/^(.*)%3([^%]*)$/\1bbb\2/g; t num_fwd_hundreds
s/^(.*)%4([^%]*)$/\1bbbb\2/g; t num_fwd_hundreds
s/^(.*)%5([^%]*)$/\1bbbbb\2/g; t num_fwd_hundreds
s/^(.*)%6([^%]*)$/\1bbbbbb\2/g; t num_fwd_hundreds
s/^(.*)%7([^%]*)$/\1bbbbbbb\2/g; t num_fwd_hundreds
s/^(.*)%8([^%]*)$/\1bbbbbbbb\2/g; t num_fwd_hundreds
s/^(.*)%9([^%]*)$/\1bbbbbbbbb\2/g; t num_fwd_hundreds
:num_fwd_hundreds
# clockwise rotation case
/^>/ {
# 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
/^</ {
# strip the L|R
s/.(.*)/\1/
# convert to negatives
s/a/A/g
s/b/B/g
# If holding buf currently points at 0, make it 100 so we don't double count
x; s/^(-*)$/bbbbbbbbbb\1/; x
# 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/([^a]*)(a*)([^a]*)(a+)([^a]*)/\2\4\1\3\5/g
s/([^B]*)(B*)([^B]*)(B+)([^B]*)/\2\4\1\3\5/g
s/([^b]*)(b*)([^b]*)(b+)([^b]*)/\2\4\1\3\5/g
# subtract & carry
:unit_sub; s/aA//g; t unit_sub
s/bA/aaaaaaaaa/g; t unit_sub
:tens_sub; s/bB//g; t tens_sub
# we passed 0
/^[AB-]|^$/ s/$/-/; t dummy2; :dummy2; # increment accumulator
s/^B/bbbbbbbbb/g; t tens_sub; # -10 overflows to 90
s/^A/bbbbbbbbbaaaaaaaaa/g; t unit_sub; # -10 overflows to 90
/[AB]/ b unit_sub
h
# s/^([^-]*)(-*)$/"\1"\n\2\n/; p # debug print
}
n
b line