Your task here will be to write a very dumb AI capable of fetching all treasures on a map.
Map
The map is defined as follow: (artist-mode
™-powered)
Basic case:
+-+-+-+-+-+-+-+-+-+-+-+-+-+
|P| | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | |T| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | |M|M|M|T| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | |T| | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
Locked up:
+-+-+-+-+-+-+-+-+-+-+-+-+-+
|P| | | | |M| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | |M| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | |T| | |M| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
|M|M|M|M|M|M| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
No treasures
+-+-+-+-+-+-+-+-+-+-+-+-+-+
|P| | |M|M|M| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | |M|M|M|M|M|M|M| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | |M| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | |M|M|M|M| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+
No way to move
+-+-+-+-+-+-+-+-+-+-+-+-+-+
|M|M|M|M|M|M|M|M|M|M|M|M|M|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cells are mountains and cannot be walked on.T
cells are treasures that need to be collected.
Goal
- You will generate a "path string" describing the movements needed to obtain all the treasures. No extraneous moves after treasures were all collected are allowed.
- Moves can be: "Left" "Right" "Up" "Down".
- Generating a move that'd result in an attempt to clib a mountain (M) or hit a wall (Out Of Bounds) is an error (it's not simply ignored).
General Rules
- Map must be read. (this is not a compression challenge, there are an infinite possible number of maps).
- There will always be one and only one player, P.
- There will always be a path to go get all the treasures, T.
- Map size may vary, but it'll always be at least
1x1
. - The AI starts at P, the player.
- There might not be a treasure on the map. In this case, you're allowed to exit any falsy value (that isn't a valid path)
- Your algorithm pick -- it needn't be the most efficient.
- You must solve it before the heat death of the universe
- It is not guaranteed you'll be able to access every non-mountain cell on the map. Just enough to fetch all treasures.
- The AI may see the whole map.
- Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Format
Input
- You can decide to take inputs as a multi-line string with or without the ASCII separators I included (same for the space between cells)
- Passed via stdio or a function argument or on your program's stack(s)
- You can other identifiers instead of " ",M,P -- so long they are uniquely identifiable and not a separator (you need it specified in your answer)
- You can decide to take an 2-dimensional array, or matrix, or the equivalent in your language, with the cells, as
[['P', ' ', ' '...], [...], [' ', ' ', 'T'...]...]
. - You are also allowed to take an array of strings, such as
["P ...", ..., " T..."...]
. - You are allowed to take the length as an extra input (width/height).
Output
- The output must look like "LLRRUUDD" (Left/Left/Right/Right/Up/Up/Down/Down)
- You can decide to intersperse spaces, newlines, etc, so long they don't mix with your instructions.
- You can other identifiers instead of L,R,U,D -- so long they are uniquely identifiable and not a separator (you need it specified in your answer)
- There might be different solutions (i.e.
DR
vsRD
), it doesn't matter which you output in this case.
Scoring
This is a code-golf challenge.
parsing
tag really suitable if we're allowed to take input as a matrix? Also, allowing input as a matrix seems to conflict with Map must be parsed. \$\endgroup\$