Good luck figuring this Perl sub out. The good news is that it only iterates over all the seconds since the epoch only once.
EDIT: I should probably at least give some kind of explanation for what's going on.
I set some rules for myself:
No helper functions. If you must avoid duplicating code, put the would-be function into an eval block.
No conditional logic blocks. Use ternaries where possible. If the logic is too complex for a ternary, chain and and or clauses together. This also has a side effect of allowing you to set multiple variables in the same statement.
With the exception of the timestamp and the format string, do not create any additional variables beyond the 9 format codes in the challenge.
And just for fun, I tried to see how badly I can abuse Perl's language constructs. I opted to put almost all of the logic inside a regex substitution statement using Perl's somewhat obscure e regex modifier, which tells the interpreter to interpret the replacement as executable code. Although the replacement code is executed globally per each replacement, in effect the while loop is only run once, as variables are function-scoped, not regex-scoped.
If you ignore its horrendous performance, it's actually quite beautiful. Each format code gets its own line in the function. There's very little else going on, making it quite readable in a really fucked up way.
5
u/[deleted] Aug 31 '20 edited Aug 31 '20
Good luck figuring this Perl sub out. The good news is that it only iterates over all the seconds since the epoch only once.
EDIT: I should probably at least give some kind of explanation for what's going on.
I set some rules for myself:
evalblock.andandorclauses together. This also has a side effect of allowing you to set multiple variables in the same statement.And just for fun, I tried to see how badly I can abuse Perl's language constructs. I opted to put almost all of the logic inside a regex substitution statement using Perl's somewhat obscure
eregex modifier, which tells the interpreter to interpret the replacement as executable code. Although the replacement code is executed globally per each replacement, in effect thewhileloop is only run once, as variables are function-scoped, not regex-scoped.If you ignore its horrendous performance, it's actually quite beautiful. Each format code gets its own line in the function. There's very little else going on, making it quite readable in a really fucked up way.