Coding dojo

Code golf results

Some time ago I’ve tried my skills in coding golf competition, and had a second place, at least for a while. I’ve originally written it in JavaScript as this language is fresher in my mind than Python. But then I’ve rewritten it in Python because I’ve got stucked. Even though it’s been a while, I’ve coded more lines in Python and read more about it, so I was able to find the bug and proceed.

It was a great fun. I’ve learnt a lot. And it tickled my ego a little bit as I still consider myself a noob, so it was nice to prove myself. And this wasn’t typical golf challenge where they want you to code fizz buzz or Fibonacci numbers. This was a bit more challenging.

There was yet another success on my side. When I was writing it I’ve found out that example was wrong. I’ve checked my code multiple times and even did a dry run on piece of paper. And was sure my code was correct and example wasn’t. So I’ve informed the author of this competition, he confirmed I was right and updated the examples.

Here’s original example:

$ ./task 2 25H0,32S0,46R1,63H1,76S1
........
........
........
....S.H.
...H....
........
....R..S
........
$ ./task 3 25H0,32S0,46R1,63H1,76S1
........
........
....S...
......H.
..H.....
........
....R..S
........

After second iteration there should be only one healthy individual, but as you can see above there are two.

Another thing I’ve noticed, however I’ve not informed author directly this time, was “R - Recovered person, can be infected”. If person who recovered from sickness can be infected why according to the algorithm it never does. It's been changed to “cannot be infected”.

I’ve also learnt that I know nothing about coding golf. And treated it as any other programming challenge. But here, the shorter the code the better, not the cleaner one, that’s why me second JS entry was scored higher. Of course I could've read the rules of code golf first and minify my code upon the first entry, but oh well. This is what happens when you’re so eager to start coding you don’t consume full specification. Another lesson learnt;) It sounds like I want to justify the fact that I’ve eventually lost this competition, but really, I was only focused on solving the problem which I’ve found interesting.

And when it comes to JavaScript I’ve learnt (or was reminded of) two things. First, when you’re doing lots of bitwise shifting then it’s better to use BigInt instead of Number in JS. Number can be overflown. Second, if you’re iterating over the array with forEach, return won’t do anything. These things aren’t super advanced knowledge, and I bet I’ve read about them somewhere at some point (especially about forEach). But it’s one thing to read something in the book and quite another to apply it in the code after a while.

Probably the best thing from this experience was that I could cooperate with other programmer, whom I value a lot. And that I could reach out and exchange my findings with the author. As I work alone it’s really great that I had opportunity to communicate with humans, especially such smart ones.

I also have a confession to make, I haven’t solved it fully. My code for finding the neighbours isn’t complete. Because people move like Pacman (through the walls) when a person is in top left corner, for instance, it can be infected by the person who is in bottom right corner. My code doesn’t cover such possibility. So I was able to complete the competition by brute forcing it. Not very proud of it, but I didn’t want to invest more time in it, especially that I’ve known I won’t win. You can check the code here.