Power of Thor - Codesize - Puzzle discussion

By the way, I appreciate the fact that developers are trying new things, I just didn’t like the direction they took, because coding using the least possible amount of characters encourages bad programming practices, like variable reuse, single letter definitions and cryptic code. I don’t know if this is a good idea, since there are many students here and a lot of people use these puzzles to learn how to program. Some habits die hard…

Maybe an optimization challenge related to the time an algorithm takes to run would be more fun. Also, it could encourage good programming practices, like writing eficient codes, avoiding to performe the same task multiple times, using heuristics to find a solution faster etc. Furthermore, it could encourage students to learn faster languages, like C. Well, just my opinion about this new optimization puzzles. Overall, I like CodinGame very much (:

51 Likes

Code golf (because this is how this kind of puzzle is called) also has benefits when learning. You have to find the limits of your language like what happens if you use an uninitialized variable, an undefined constant, the tiny differences between two similar functions or such things. Sure the final code is full of bad practices but it’s not as if you’ll code the same way everyday (except for physicists, as lots of them still think than naming variables i, ii, iii, and so on is perfectly fine).


Anyway, I find weird you’re saying that time optimization would lead to better practices. I mean, fast algorithms are also often cryptic because they’re based on how a CPU handles operations. Just take this famous example from Quake III Arena (with original comments):

float Q_rsqrt( float number )
{
	long i;
	float x2, y;
	const float threehalfs = 1.5F;
 
	x2 = number * 0.5F;
	y  = number;
	i  = * ( long * ) &y;                       // evil floating point bit level hacking
	i  = 0x5f3759df - ( i >> 1 );               // what the fuck? 
	y  = * ( float * ) &i;
	y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//      y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
 
	return y;
} 

This function is an optimized approximation of inversed square root, or to say it differently x^-0.5. Will you learn good practices from it? Certainly not. :wink:

28 Likes

The only bad practice I can see here is the magical number (which could be given a more useful name) and the scarce documentation. Both of these can be fixed without impairing the performances.

(Side note: this piece of code is considered a masterpiece by some people)

What you say about the limits of the language is true to some point, but then whitespace and comments (excluding preproc macros) could be excluded from the score.

2 Likes

I think CodinGame should take care of the spaces and comments removal. Else it just makes it unreadable and forces you to keep a copy of your code sideways to work on it.

26 Likes

NewboO, I said a performance challenge related to time would be more fun to me and could lead to better programming practice in general in most cases. But trying to code using as few characters as possible, in my opinion, will necessarily lead to bad programming practices. For example, nothing good can come from removing white spaces from your code. In most languages it will remove identation, cripple readability and decrease clarity. And this is why I think this kind of puzzle can be harmful for beginners, since they could develop bad habits that are hard to get rid from. I think a performance challenge related to time would be more fun (to me) and more instructive in general to students.

3 Likes

Code size optimization doesn’t lead to improve coding practices but I think it’s a good way to hack programming languages. In my own case I learnt few things in javascript like:

  • better understanding of typecasting
  • [a, b, c, d] = readline().split(’ ') is possible, yeah!
10 Likes

Same thing for me, I’ve learned a few things in C

2 Likes

Hi!
Is there a limiit to the submissions?
When I submit after writting my new code in the “Code Editor”, it doesn’t use the new code when computing but a previous code… Which results in my code size and ranking being associated with my previous code and not my newly edited code…
When I click on “View Report”, I see my old code in the “Answer” box, but when I go and “Try Again” it’s my new code I find in the “Code Editor”.

2 Likes

There is absolutely no submission limit.
If you still see your old code on the report page after publishing a new solution, it may be because of two reasons:

  • Your new code is longer than the previous one, so the best solution is kept
  • Your new code did not pass all the test cases and so is not accepted

From what you say, I think this is the second reason.

Actually, the report page is intended to display our best submission and not the last one.

2 Likes

Is it possible to have the exact rules used for the calculation of the code length?
Are the spaces taken into account?
Does the number of line count?
Does the length of a variable name count?
Does the number of variables count?

But concerning the question “is optimizing code length interesting?”, I think that yes. I usually don’t do that when I write a program, but in can be interesting challenge. If any of you know the games “Spacechem” and “Infinifactory”, you know that trying to acheive a solution using the least number of command can be very satisfying.

2 Likes

I have the same problem: my code is shorter now and it’s still the first one that is shown and used.

2 Likes

Me too. There should be a way to know exactly how well does the new code with the final tests. Otherwise we have no way to correct it…

1 Like

Code length only counts the number of characters. All characters count, including those in variable names, line feeds, spaces and tabulations.
Formerly, spaces and such did not count, but someone found an abuse by cyphering all his code into only spaces and such, scoring 42 for the Clones Code Length. So, the rules where changed.
Actually, tabulations count as 4 spaces, so you’d better replace them by 1 space if you’re using python. Or put multiple commands in 1 line using ‘;’ :wink:

7 Likes

Thanks for the answer. If the spaces count, then maybe some languages are better than other. On the orther hand, in Python, we need to indent with at least one space, but we don’t need “;” at the end of each line or horribles brackets every 2 lines…

1 Like

I think it would be great if you should show the character count on the page somewhere so we wouldn’t have to submit or use an external tool when we’re trying to optimize. Fun problem though!

2 Likes

This small hack can do the job (open the puzzle, open the js console and paste it):

var iframe=$("#ideFrame")[0].contentWindow;var ide=iframe.ace.edit("ace_edit"); ide.on("change", function(e){iframe.$(".squareTitle").find("h2").text(ide.getValue().length + " chars")});
7 Likes

When the CEO gives you a hack :slight_smile:

4 Likes

Hey!
Do you know a better way to shorten your code than by using lambda functions? I have more than the double caracteres than the first one in python 3, and I do not see any other way to improve what I have (exepted the solution which consist to find a better algo, but I do not have not found it yet).

To a better understanding of my issue, my lambda function is currently necessary to me as I am doing something twice.
Thanks.

Then, I think another way of improvment is that we can shorten the initial line by doing something else than “input().slice()” but I do not have any hint to do so yet.

1 Like

Hi Yandross, in my solution I only read the input, do the while loop, do a print and increment a value. No need to use a lambda function nor to rename a function.

3 Likes

Thanks. Actually, I should review my algo. Without my lambda function, I only have one character less since I’m doing identical things twice. I do not see how you can increment only one value thougth. There are two axes, and I do not know how to update data about these two axes easily.
Anyhow, I will pass to something else for now.

1 Like