Yet another obfuscated Awk code

50 views
Skip to first unread message

Janis Papanagnou

unread,
Jan 22, 2022, 5:07:41 AMJan 22
to
Just for recreational purposes here's yet another obfuscated Awk code.

func ___(_) { return __(_,x^x,x^x^x) }
func __(_,_x,x_) { return --_?__(_,x_,_x+x_):_x+x_ }
{ print ___($_) }

Solutions, guesses, comments, improvements, fixes, requests for hints,
etc. are all welcome.

Janis
Message has been deleted

Kpop 2GM

unread,
Jan 22, 2022, 12:59:48 PMJan 22
to
hmmmmmm ……..i dunno ….. maybe full length of each row of input, determines which Lucas number it'll print out. if original input were greater than zero, and integer, but degenerate into an infinite loop if original input were less-than-or-equal-zero, or is a non-integer real number, or contains bytes other than 0-9, +/-, space, to the left of the first position with a number-like value (e.g. decrementing capital "A" makes it -1, thus degenerate into the infinite loop case) ??
Message has been deleted

Kpop 2GM

unread,
Jan 22, 2022, 1:11:05 PMJan 22
to
it's not even limited by 2^-1023 or 2^-1074. since once you're past -2^53, unitary pre-decrementing a number keeps it stuck there, since it lacks the ability to accumulate your non-representable odd numbers in double precision, and will go on for quite some time until the either input position 2 or 3 in the two-underscore function reaches 2^1024 infinity

just my random uneducated guess

as for function called by print, the 3-underscore one, all of those are pure distractions, since those reduce to calling two-underscore function with

__( original-input, 1, 0)

wait … then that's Fibonacci then not Lucas …. i'm confused

Janis Papanagnou

unread,
Jan 22, 2022, 8:35:00 PMJan 22
to
You've identified it!

A couple notes. Since numbers grow extremely there's some limit as
you noticed. I admit it was unfair that I didn't define the domain
of the function, but you got it anyway. I could of course have had
a condition added to make life easier, e.g. $_&&!/[^0-9]/ { ... }
or, to obfuscate it a bit, even _=int(sqrt(-($_*-$_))) { ... } .
The two constant parameters are indeed 1 and 0 (not the values you
assumed in an earlier post). I used a reduced pattern of what I saw
in your other thread's post (_^_^_^...^_) for these constants (with
my observation that the reduced subexpression toggles between 0 and
1). The function is the one you suspected, and (in obfuscated form)
simpler written as just

# defined for integer args>0
func __(_) { return _>2?__(--_)+__(--_):1 } { print __($_) }

I like this form even more (with those nasty --_ side effects) and
the underlying mathematical function is more obvious, but the time
complexity is extremely bad since we have a cascading recursion in
that simple form. There's a transformation of the function possible
to generate an equivalent linear recursion with good (linear) time
complexity (and no supporting arrays and the like), and that is

func ___(_) { return __(_,x^x,x^x^x) }
func __(_,_x,x_) { return --_?__(_,x_,_x+x_):_x+x_ }
$_&&!/[^0-9]/ { print ___($_) }

And where the first version is rarely usable for arguments >35 the
second one runs smoothly for large arguments in no time.

Janis

Kpop 2GM

unread,
Jan 24, 2022, 4:15:36 PMJan 24
to
it's not your fault whatsoever you haven't defined the domain - that's what a code challenge is all about anyway :

to identify both the core feature(s), and edge cases. you're entirely within your rights to only define what you want to define, and it's up to the participants to complete the puzzle. I'm not a mathematician whatsoever and still don't really understand what a Lucas number sequence is anyway.

i was only able to identify it as Fib cuz i already have 2 variants of the fib( ) function coded in a similar fashion (but both failing to capture some edge cases) :

function fib2(_,__) {
return (__=!-"")+_<+__?+__\
: fib2(_-__)+fib2(_-__-__) }

function fib3(__,_,___){
return !__?___:_""""""\
? fib3(__-(_~_),_+___,_)\
: fib3(__-(_=_~_),___+_,_) }

Kpop 2GM

unread,
Feb 3, 2022, 6:51:37 AMFeb 3
to
Hi Janis,

if you care for an even more challenging obfuscated one :what is contained within that exactly 1.00 GB being outputted from this function in 95 miliseconds , given the sample input of 7 in argument position 1, and 30 in position 2 ? This code shouldn't have any trouble executing on just about any awk variant one can imagine. It's also fully self-contained,.

% ( time ( gnice mawk2 'function __(_,___,____) { if (_!~"."||___<-____) { return "" } else if (((____+=++____)*++____+--____)<=+___) { _=__(_,____=int(___/=____)); gsub(".",_,_); return ___-____?(_)_:_ } else { return (""<"")<--___?(_=__(_,--___))(_)(_)_:___?_:(_)_ } } BEGIN { print length(__(7,30)) }' ) )

( gnice mawk2 ; ) 0.03s user 0.07s system 96% cpu 0.095 total
1073741824

The 4Chan Teller

Kpop 2GM

unread,
Apr 11, 2022, 8:38:20 PMApr 11
to
@Janus : This next one should be straight forward enough. For your convenience, I'm including the profiler output from gawk to show how it's being executed using gawk -P (posix) mode as well as mawk 1.3.4's debugger

what does this function that's non-recursive, non-gawk-extension-dependent, and fully self-encapsulated return ?

(self-encapsulating to a point it neither makes any calls to built-in functions, nor does it reference any system-wide variable)

# gawk profile, created Mon Apr 11 20:27:37 2022

# BEGIN rule(s)

BEGIN {
1 OFMT = CONVFMT = "%.50g"
1 print _____()
}


# Functions, listed alphabetically

1 function _____(_, __, ___, ____)
{
1 __ = ! (____ += ____ += ____ *= ____ *= ____ *= ____ = (___ = _ = _ ~ _) - +-++_ * _--)
1562499 while (--____) {
1562499 __ += (___ / _++) - (___ / _++)
}
1 return __
}

====================== below is what the debugger from mawk 1.3.4 prints out ===============

function _____
000 . l_pusha 1
002 . l_pusha 3
004 . l_pusha 3
006 . l_pusha 3
008 . l_pusha 3
010 . l_pusha 3
012 . l_pusha 3
014 . l_pusha 2
016 . l_pusha 0
018 . l_pushi 0 # frame-number
020 . l_pushi 0 # frame-number
022 . match2
023 . assign
024 . assign
025 . l_pusha 0
027 . pre_inc
028 . uminus
029 . uplus
030 . l_pusha 0
032 . post_dec
033 . mul
034 . sub
035 . assign
036 . mul_asg
037 . mul_asg
038 . mul_asg
039 . add_asg
040 . add_asg
041 . not
042 . assign
043 . pop
044 . jmp 063
046 . l_pusha 1
048 . l_pushi 2 # frame-number
050 . l_pusha 0
052 . post_inc
053 . div
054 . l_pushi 2 # frame-number
056 . l_pusha 0
058 . post_inc
059 . div
060 . sub
061 . add_asg
062 . pop
063 . l_pusha 3
065 . pre_dec
066 . jnz 046
068 . l_pushi 1 # frame-number
070 . ret
071 . ret0
BEGIN
000 . f_pusha OFMT
002 . f_pusha CONVFMT
004 . pushs "%.50g"
006 . f_assign
007 . f_assign
008 . pop
009 . call _____ 0
012 . pushint 1
014 . print
016 . exit0
Reply all
Reply to author
Forward
0 new messages