Winter Challenge 2024 by Codingame

“It’s the season to be jolly …” a famous Christmas carol starts, and this year CodinGame.com brought us programmers a present in the form of their Winter Challenge 2024. Being a nerd and a game enthusiast, tackling these kinds of problems always brings out a smile on my face πŸ˜‰

The overall premise of the challenge is that you get a problem to solve programmatically, while competing against other people doing the same. The best your algorithm is, the higher you’ll rank. The competitors are divided into leagues, and as you move up the leagues the challenge becomes more complex. I’ve never considered myself an expert on these things, but nothing like a learning opportunity, so I went at it again this year.

So what the winter challenge 2024?

This year you’ll be playing the part of an organism, living on a game board. The goal is to outgrow a competing organism over 100 turns. You can grow new organs to an adjacent field. You consume proteins while growing, but can gain more by growing organs onto protein caches found on the board. If you grow more organs than you opponent, you win. Oh yeah, and the board changes between games.

Here’s an image of what the board looks like in league “Wood 3” after the first turn has elapsed.

Seems simple enough, so game on!

To bold grow where no one has grown before …

Having thought about how to become the biggest organism on the board, I divided the problem it into a set of sub problems. I want my organism to:

  1. Find the distance to all protein caches
  2. Pick the closest cache
  3. Calculate the shortest path to the protein cache
  4. Grow an organ on the shortest path
  5. End turn, by going back to 1.

I can treat the board as a Cartesian coordinate system and use a two dimensional table as the underlying data structure for each tile. So if this was just a flat board, calculating the distance would be fairly simple math. Unfortunately the world is full of obstacles, and unfortunately our tiny simulation of it is no exception. Someone decided to put up walls! Alas simple math will not get us there.

Back in my student years I came across something called graph theory. The short version is that we can see the board as a set of nodes (each tile). Nodes can be connected to one another by edges. In our case edges exist between two adjacent tiles, as we cannot grow diagonally. So going from one node to another is done by traveling along the edges. Having a wall represented in the graph would basically be a node with no edges connected to it, thereby preventing us from going there. Ok, this is all fine an dandy, but where does this get us you may ask. The good thing about entering into a know domain is that you get to use known ways of solving the problem. Enter graph solving algorithms πŸ˜€

The shortest path between two points

There are a number of suitable algorithms one can use, but I went for a simple approach – Djikstra’s algorithm. We wanted to know the distance to each protein cache, but Djikstra can give us the distance to each point on the board. And I foresee us needing at a later point…

My board representation would look something like this, with asterisks being walls, ‘X’ being root nodes and ‘A’ being protein caches.

* * * * * * * * * * * * * * * * * *
*                                 *
* X                               *
*         A                       *
* * * * * * * * * * * * * * * *   *
*                                 *
* X                               *
*         A                       *
* * * * * * * * * * * * * * * * * *

After having implemented the algorithm I could then run it to calculate the distance, giving me this.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 --
--  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 --
--  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 17 --
-- 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 --
-- 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 --
-- 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Sweet, now I just need to read the values in the table for each protein cache and pick the lowest value.

Backtracking isn’t so bad …

Djikstra’s algorithm has a really simple way of finding the shortest path. Beginning at the destination, check the distance to every connected node and pick the one with the lowest value and remember that. Repeat this for the node you just found until you reach a zero distance coordinate and voila all the nodes you remember forms the shortest path.

Time to grow

So from here I took the coordinates from the node with distance = 1 in the shortest path and grew an organ there. And with that I grew big enough to enter the next league πŸ™‚

Book recommendation “Clean Architecture”

Hi all,

This time I’d been fortunate enough to have the book “Clean Architecture” in my hands. Why fortunate, you may ask. Well, I’d say the the book is a must read if you want to improve your skills when it come to building maintainable software.

Programming principles

The author Robert C. Martin (or “Uncle Bob” as he known as in the industry( goes through a set of software architecture principles coined the SOLID principles. I believe that following these principles you will write better software, as you’ll architect and structure it much better. And I try to do I when I’m programming.

I think the book does a good job of explaining these and through practice I believe you should be able to incorporate these whenever you program. Or at least that is my hypothesis πŸ˜‰

Writing maintainable code

The book also goes to some lengths to point out what a good software architect should strive to accomplish, namely making code easy to maintain. If you look at online review there are diverging opinions on how many pages should be spent on this, but what I can say is that from my professional experience I have never seen any piece of code, component or system that over time became harder and harder to maintain, as features where added. So, in my opinion, you always need to be diligent about reducing your technical debt or maintenance overhead if you will.

You can go have a look at the book at Amazon

Problems with Wacom Touch Ring not working

I’ve had a Wacom Cintiq QHD27 for some time now. Awesome tool and way too flashy for my skill level in Photoshop. None the less I occasionally have some fun drawing on it (I actually made the graphics for my games using it).

One thing that has aggravated me from time to time is that the touch ring, on the EK remote I got with it, was not responding in Photoshop. I’d like to be using it for controlling the size of the brush, canvas rotation and such, basically speeding up my work flow, but it was just dead somehow. Trawling my way through various support sites I came across a solution that worked for me; resetting the darn thing. I’m not talking one of those software resets here, no no, that would have been too easy. No, what I had to do was pull off the back and push the reset button under the cover, doing a hard reset. But it fixed the problem in the end πŸ˜€

I got the guide from here in case you need it (thank you, Frank Doorhof for making the guide)

Hope it works for you too πŸ™‚