Coding Challenge

Today, was the start of the a coding challenge at Codility. At the challenge, you have two hours to solve the problem below. 

I gave my best at solving it, but, unfortunately I was not able to. So, I need to shake off the rustiness, and try again later.


(06.Dec.2022) Edited to add:

I finally cracked this little nut. I will post my solution after the challenge ends on December 17.

As a result I got a little certificate:


(19.Dec.2022) Edited to add:

Today, as the challenged ended last Saturday, I'm posting my solution.


Carol of the Code

Tom is decorating the pavement in his garden with N square tiles. Each tile is divided into four triangles of different colors (white - ‘W’, red - ‘R’, green - ‘G’ and blue - ‘B’). A tile is described as a string of four characters denoting respectively, the color of the upper, right, bottom and left triangle. For example, the tile in the figure below is described as “WRGB”.

Tom arranged the tiles in a row and decided to rotate some of them to obtain a pretty sequence. He considers a sequence of tiles pretty if each pair of adjacent tiles shares one side of the same color.

Write a function:

class Solution { public int solution(string[] A); }

that, given an array A of N strings, representing the sequence of tiles, returns the minimum number of 90-degree rotations (clockwise or counter-clockwise) that Tom has to perform.

Examples:

1. Given A = ["RGBW", "GBRW"], the function should return 1.

Tom can rotate the second tile counter-clockwise once to obtain a pretty sequence.

2. Given A = ["WBGR", "WBGR", "WRGB", "WRGB", "RBGW"], the function should return 4.

Tom can obtain a pretty sequence by rotating the first and third tiles counter-clockwise and the second and fourth tiles clockwise.

3. Given A = ["RBGW", "GBRW", "RWGB", "GBRW"], the function should return 2.

Tom can rotate the first tile clockwise twice to obtain a pretty sequence.

4. Given A = ["GBRW", "RBGW", "BWGR", "BRGW"], the function should return 2.

Tom can rotate the first two tiles clockwise to obtain a pretty sequence.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • string representing a tile has 4 letters (exactly one occurrence of 'R', 'G', 'B' and 'W').

Comments