sed 4.2.2 -r
, 291 287 276 260 255 251 248 240 233 228 226 220 219 212 209 208 207 196 193 191 184 181 177 175 173 170 169 165 164 163 bytes
h;G;G
:;s/.//;x;s/\s.*//
:a;s/#*/& &/;T;x;s/^##/#/;x;ta
x;s/#.#//;H;g;s/ //g;/\n##+\n/t;T
s/(#+)\n#.#(#*)/\2\n\2\n\2 \1/;h;t
s/.#?\n+/#/;h;T;s/.* #//;x;s/ #*$//;ta
Try it online! (cases 0-4; slow)
Try it online! (cases 0-5; fast, +1 byte)
Try it online! (cases 0-7; calculate the powers but don't multiply them together)
takes input and output as unary #
s. TIO links include conversion to/from decimal in header/footer.
after two months and 100 bytes of golfing, I think it's high time to actually write out an explanation. normally, I add the explanation in comments next to the code, but I think explaining the data format first will work better with this one. the PATTERN and HOLD spaces are switched around a lot, but let's call this the initial PATTERN space:
add
mult
pow prev
each 'variable' is a unary number of #
s which keeps track of how many more times we need to preform each operation. the first one used is add
, where the a
loop adds the HOLD space (which is currently pow
) to itself and decrements add
. once add
is 0 (ie HOLD now equals add * pow
), mult
gets decremented by one and add
is set to HOLD. once mult
is 1 (ie HOLD now equals pow ^ pow
), pow
is decremented, add
and mult
are set to pow
, and prev
gets set to HOLD\tprev
(where \t
is a tab). once pow
is 0, we have calculated n^n
for every n
, starting at the input and going all the way down to 0, with the results separated by tabs. finally, the last time we loop through is to multiply these results together, which we do by first removing all newlines to let it pass through the first checks, and loading the final result into the HOLD space while deleting it from the PATTERN space until there is only one result left.
description of code by line:
1: sets the format up like how I described as 'initial PATTERN space' where the HOLD space is the input and the PATTERN space is the input repeated three times
2: a 'prefix' which takes care of an off-by-one error and discards junk that accumulates in the HOLD space
3: the `add` loop, which ends with the HOLD space equaling HOLD space * first line of PATTERN space
4: the `mult` loop, which ends with the HOLD equaling HOLD space ^ second line of PATTERN space
5: the `pow` loop, which saves the result of the `mult` loop after a tab and restarts everything with a decremented input
6: the `final` loop, which removes all the newlines to bypass the `mult` and `pow` loops, and multiplies the first result to the last result until there is only one result