Files
aoc25/d01/d1p1.sed
2025-12-04 14:52:28 -08:00

122 lines
3.0 KiB
Sed
Executable File

#!/usr/bin/sed -nEf
x; s/^$/bbbbb/; x # put "50/0" into the hold buffer
:line
# = # 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/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/%//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
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/([^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
s/bbbbbbbbbb//g # 100 overflows to 0
h
#p # debug print
}
# counter-clockwise rotation case
/^</ {
# strip the L|R
s/.(.*)/\1/
# convert to negatives
s/a/A/g
s/b/B/g
# append the content of hold buffer to pattern buffer
G; s/\n//g
# sort the characters
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
s/^A/bbbbbbbbbaaaaaaaaa/g; t unit_sub
:tens_sub; s/bB//g; t tens_sub
s/^B/bbbbbbbbb/g; t tens_sub
/[AB]/ b unit_sub
h
#p # debug print
}
# check if we are at 0, and if so, add 1 to the accumulator
/a|b/! {
s/$/-/
h
}
n
b line