DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 126

Challenge, My solutions

TASK #1 › Count Numbers


You are given a positive integer $N.

Write a script to print count of numbers from 1 to $N that don’t contain digit 1.

My solution

This seems pretty straight forward. Have a loop from 2 to $N and add one if that number does not contain a one index( $number, '1' ) == -1.


$ ./ 15

$ ./ 25
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Minesweeper Game


You are given a rectangle with points marked with either x or *. Please consider the x as a land mine.

Write a script to print a rectangle with numbers and x as in the Minesweeper game.

My solution

My solution is basically two sub-tasks. The first is processing the input, and the second is populating the board.

In the real world, you generally know what the input to a program is be it a file, URI or an HTTP request. For Team TWC challenges, this is up to the author to decide. For most tasks, it is usually a series of values than can be read from @ARGV. For this task, I read lines from STDIN. This allows a file to be piped in.

I turn the input into an array of arrays of x (mines) and * (non mines). I skip any blank lines, and then check all rows have the same number of columns.

The next part is to work through each cell. If it doesn't have a mine, I count the number of mines in the surrounding cells, making sure that we don't outside the bounds of the board. I then replace the cell value with the number of mines.

The last step is to print the output.


$ ./ < input.txt 
x 1 0 1 x 2 x x x x
1 1 0 2 2 4 3 5 5 x
0 0 1 3 x 3 x 2 x 2
1 1 1 x x 4 1 2 2 2
x 1 1 3 x 2 0 0 1 x
Enter fullscreen mode Exit fullscreen mode

Discussion (0)