3 kyu

The treasure in the Toroid [Code Golf]

Description:

Welcome to the world of toroids!

You find yourself in a toroid maze with a treasure and three lives. This maze is given as an array of strings of the same length:

maze = [
    '#### #####',
    '#    #E#T#',
    '###### # #',
    ' S## H E #',
    '#### #####'
]    

Here you can see that mazes can have different tiles:

  • ' ' => Space, you can walk through here freely
  • '#' => Wall, you cannot pass through here
  • 'E' => Enemy, you can pass through here at the cost of one life
  • 'H' => Health, you can pass through here and gain a life
  • 'S' => Spring, once you are over it, the next movement will be a jump over a tile (any tile, even a wall or an empty space). The effect of the tile that you jumped over will not be activated.
  • 'T' => Treasure, this is what you are looking for!

Given a starting row and column index (i and j, respectively and starting from 0), can you get the treasure by moving straight up, down, left and right (not diagonally)? Note that there are no restrictions on the starting coordinates (you might be asked to start from a wall!).

You have to take note of three things:

  • Once you fight an enemy or pass through a health tile, it dissapears.
  • If you run out of lives it's game over.
  • If you are asked to start from an invalid position (i.e. a wall), then it's an automatic game over too. You will never be asked to start from an invalid row or column index.

But, what about the toroid part?

The maze behaves as usual, but the coordinates wrap on both the x and y axis. This means that this treasure is obtainable from any empty tile in this particular maze:

maze = [
    '## ## ####',
    '   ##    #',
    '######## #',
    '##    ##T#',
    '## ## ####'
]    

To be more clear, imagine an empty 3x3 maze like this one:

maze = [
    '   ',
    '   ',
    '   ',
]    

Here, you can go from the '1' tile to the '2' tile by walking upwards and from the '3' tile to the '4' tile by walking left:

maze = [
    ' 1 ',
    '3 4',
    ' 2 ',
]    

Of course, this also works in the opposite directions.

Your task

Write a function treasure(maze, i, j, n) that returns True only when it is possible to take the treasure when you start walking from the tile with coordinates (i, j) and take a maximum of n steps.

Your function will be tested against many random 6x6 mazes with a maximum of 10 steps, so performance is important, but not essential :)

Do u even golf?

The task is not very difficul to program by itself, the real challenge is that this has to be solved in 240 characters or fewer. The best I managed to program was 229 characters long.

IMPORTANT: this kata has been created before the walrus operator was a thing, so you aren't allowed to use it in your solution.

Some examples

maze1 = [
    '##########',
    '    #   T#', # Not obtainable from (1, 0)
    '##########',
]    

maze2 = [
    '##########',
    '   S#   T#', # Obtainable from (1, 0)
    '##########',
]    

maze3 = [
    '##########',
    '    #   T ', # Obtainable from (1, 0)
    '##########',
]    

maze4 = [
    '##########',
    '     EEET#', # Not obtainable from (1, 0)
    '##########',
]    

maze5 = [
    '##########',
    '    EHEET#', # Obtainable from (1, 0)
    '##########',
]    
Restricted
Mathematics
Puzzles

Similar Kata:

More By Author:

Check out these other kata created by Dr Gabo

Stats:

CreatedMar 2, 2020
PublishedMar 2, 2020
Warriors Trained400
Total Skips198
Total Code Submissions1029
Total Times Completed29
Python Completions29
Total Stars20
% of votes with a positive feedback rating100% of 9
Total "Very Satisfied" Votes9
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
3 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • Dr Gabo Avatar
  • Blind4Basics Avatar
Ad