Archive

fun

Today I started my first kata at CodeWars as a sensei. The kata is based on an mobile game called Strata (android, iOS).

The kata in CodeWars

The following sections corresponds to the kata I wrote for CodeWars. Now I’m working on the solution. When all the required stuff get finished I will write another post.

Introduction

Cross-stitch is a popular form of counted-thread embroidery in which X-shaped stitches in a tiled, raster-like pattern are used to form a picture. Cross-stitch is the oldest form of embroidery and can be found all over the world. Many folk museums show examples of clothing decorated with cross-stitch, especially from continental Europe and Asia.

This kata tries to emulate a certain cross-stitch embroidery. We start from a squared canvas where certain tiles are colored, a pattern. The pattern is composed by colored-tiles and empty-tiles (gray in the following example). We need to embroider the canvas and cover each tile with a vertical strand and an horizontal strand. The tile will be colored with the color of the last strand that covers it. The empty tiles can be embroidered with any color.

Take the next picture as reference:

2x2 Canvas

We start with a 2×2 canvas with a total of 4 tiles (1). We can embroider the first row with a blue strand (2). We can do the same for the second row with a purple strand (3). Embroidering the first column with a blue strand (4) we cover the blue tile in the first row with two strands. Since the strand on the top is blue, this tile is colored as blue. This makes the first tile be colored with the requested color.

Then, if we embroider the second column with a blue strand (5a) we fill the last tile (seconds row and second column) as blue, but the tile tells us that it must be colored as purple. So we not match the pattern. For hence, the strand on the second column needs to be purple (5b).

Request

What is requested in this kata is to return a solution given a patterned-canvas. For hence, you are requested to implement the function find_solution that has a two arguments:

  • canvas: This is an array of arrays representing the corss-stitch canvas (an array of rows).
  • colors: This is an array of the colors of the pattern.

I must say that the board will always be squared. An example of canvas and colors is:

canvas = [['b', ' '], [' ', 'p']]
colors = ['b', 'p']

It must be understood as: The canvas has two rows and two columns. And it has two colors, blue (b) and purple (p).

The function find solution must return a valid solution to embroider the canvas and color the given pattern. This solution must follow a specific format detailed here:

  1. It must be an array of hashes
  2. Each hash can have only three keys: :type, :index and :color
  3. The :type key can be 'r' for rows and 'c' for columns (string)
  4. The :index key will be the index of the row or column to be colored (integer). It must be between 0 and board.size - 1
  5. The :color key will be used to fill the row or column (string)

Warning: The canvas will be colore following the array’s order

An example of a solution for a 2×2 canvas:

[{:type=>"r", :index=>0, :color=>"b"}, {:type=>"r", :index=>1, :color=>"b"}, {:type=>"c", :index=>"0", :color=>"b"}, {:type=>"c", :index=>"1", :color=>"b"}]

No programmer can be an entry week at home without typing something. The ruby script squarefy I’m introducing in this post is the result of this afternoon, the 8th day of my Christmas holidays.

  • The repository of the project can he found at github (link).

The idea behind the script is to pixelize a picture but by drawing squares on it. This squares add Gaussian-blur effect on the original picture and also tint effect. So given the following picture:

The squarefy script will add a layer of colored and blurred squares on it with the aim to add a pixelized effect to the picture: