Random behavior and embedded language in code golf

For some languages, I am just curious how there can be a big gap between the top players in code size.

I hope random behavior is not used. ex: instead of reading input, one can just generate a random number and run the program hundreds/thousands of times until one run passes.

And I don’t know if embedded program is allowed. I mean for example in Java we can call javascript which is much shorter.

As for me, I use magic numbers which is always based on the input. It would be nice if CG team can make it clear what skills are allowed in the code golf.

1 Like

If it works it’s allowed. Yes some people use random and yes some people call other language. You can even use special character (e.g. (%w([¡š #8[#M[) in ruby)…
Don’t put too much times in code golf it’s not really interesting.

1 Like

In my opinion, a solution that works must be a solution repeatable - which is also one of testing principles. In this sense, a solution with ‘random behavior’ can’t be really qualified as a solution works. And it’s rather easy to prevent this kind of solution, ex: re-run the tests every day - even a weekly re-run is enough.

Anyway, thanks for the info, at least I know in some cases maybe I should stop searching vainly to optimize with ‘normal’ means :slightly_smiling:

1 Like

As to code-golf itself, I do get fun out of it – usually I have to study the problem in different perspectives every time I want to optimize it. And, it’s amazing to realize finally there exists so many ways of solving a problem.

Surely code-golf is not supposed to be used in real serious projects but as a game I find it’s quite funny.

1 Like

I’m sorry for you, but firsts solutions just call bash most of the time. For example in javascript, you just have to wirte this:

system('bash solution')
1 Like

Quite a pity that by doing this, golfing of a language actually turns into a dummy of another language.

The real disturbing experience for me was that I did believe other people had a much better way of solving the problem (normally that’s an exciting thing for me). But after spending some time, I realized that some practices are allowed by CG and the searches turned out to be useless.

I believe the same thing may happen to other coders which will become actually an annoying waste of time instead of pleasure. I really hope CG will do something on that.

1 Like

@Magus: Up to now I have mainly worked on Thor. I come with 58 letters for javascript. I confirm that there is neither embedded language nor random behavior. I read the input and do in pure javascript (surely with some magic number :grin: )

I have pretty nice scores with thor in several languages. And I confirm that all of them are feasible with pure supposed language and no random numbers. Hope this info is useful.

1 Like

I’m not sure if any top solutions contains any random. And for languages like ruby, python, perl, python3 (and maybe some more languages), you don’t need to use bash. But you just can’t be shorter than a “bashed solution” in C and C++ for example.

1 Like

I can’t tell for other but my 53 character thor contains random.

You can do better without random :wink:

AFAICT, random is used for Thor in Java (the best I have without random is 146). Embedded program is used for several solutions of Temperatures, except for Javascript where system() is disabled. I don’t know for the other cases.

1 Like

Could someone explain what is meant by both ‘random’ and ‘magic number’ in the context of this discussion please? Thnx

For thor there is only 4 solutions to verify so you can easily harcode it.
This solution can be simplified for exemple instead of 4 output your code got only 2 differents output.
Random can be use to avoid read input and use one of this 2 solutions.
Magic number can be one of the number of the read input instead read and parse all the input.

1 Like

Take thor for example - let’s say we keep only the first two tests for this example:
test 1: input = “7 8 28 8” (thor will go west)
test 2: input = “5 17 5 4” (thor will go south)

In a “normal” implementation, usually we track the positions of thor and light, we compare them, we check the boundary conditions, we may even model the problem in a N-dimension manner for pleasure :slightly_smiling:

In a ‘magic number’ implementation, we may do:

flag = readline()[0] ==“7”
while 1: print(flag? “W” : “S”)

Note: this implementation is repeatable thus stable for given tests.

In a ‘random number’ implementation, we may do:

flag = rand_int() == 7
while 1: print(flag? “W” : “S”)

As CG only records the best run, it’s enough if one run is okay when we launch the above code hundreds of times.

1 Like

OK, thanks for the info, (never would of thought of randomising, all those boring submit clicks!)

I’ve got a ‘regular’ solution to Thor in javascript which is 86 (which looks pretty trim to me…). I just tried ‘magic number’-ing to see if I could reduce that but the best I could find is 88, and that was reducing the solutions to 3 cases (I don’t think it can be reduced to 2?)

Reverer your score is 58!! Are you constructing strings with RegEx or something? I can’t see many other ways to get the code shorter.

@russell_ryan_jones: there is no RegEx in my code. Actually, I only use old plain javascript that almost every javascripter knows. But I confirm that I have used magic numbers in the code. For me, the challenge/fun is really to try to consider the problem in different perspectives.

Besides, I believe the random guys are ‘clever’ enough to use robot-software to do the click/back stuff automatically instead of manually :slightly_smiling: