The goal of this challenge is to write a program or function that returns the least amount of strikes needed to complete a given course.
Input
- The layout of the course can be passed in any suitable way and format you prefer. (read from the console, passed as an input parameter, read from a file or any other, multiline-string, string array, two-dimensional character/byte array).
- The start position of the ball and the hole can be passed as input too, it doesn't have to be parsed from the input. In the test-cases they are included in the course to make sure there is no confusion about the actual position.
- You can remap the input characters to something else, as long as they are still recognisable as distinct characters (e.g. printable ASCII characters).
Output
- The program must return the lowest possible score (least amount of strikes needed to reach the hole) for any course passed as input in a sensible format (string, integer, float or a haiku describing the result)
- If the course is impossible to beat, return
-1
(or any other falsy value of your choice that wouldn't be returned for a beatable course).
Example:
In this example positions are notated 0-based, X/Y, left-to-right, top-down - but you can use any format you like since the result is completely format-independent anyways.
Input:
###########
# ....#
# ...#
# ~ . #
# ~~~ . #
# ~~~~ #
# ~~~~ #
# ~~~~ o #
# ~~~~ #
#@~~~~ #
###########
Ball (Start-Position): 1/9
Hole (End-Position): 8/7
Output:
8
Rules and fields
The course can consist of the following fields:
'@'
Ball - The start of the course'o'
Hole - The goal of the course'#'
Wall - Ball will stop when it hits a wall'~'
Water - Must be avoided'.'
Sand - Ball will stop on sand immediately' '
Ice - Ball will continue to slide until it hits something
The basic rules and restrictions of the game:
- The ball can't move diagonally, only left, right, up and down.
- The ball will not stop in front of water, only in front of walls, on sand and in the hole.
- Shots into the water are invalid/impossible
- The ball will stay in the hole, not skip over it like it would on ice
- The course is always rectangular.
- The course is always bordered by water or walls (no boundary checks required).
- There is always exactly one ball and one hole.
- Not all courses are possible to beat.
- There might be multiple paths that result in the same (lowest) score.
Loopholes and Winning Condition
- Standard loopholes are forbidden
- Programs must terminate
- You can't make up additional rules (hitting the ball so hard it skips over water, rebounds off a wall, jumps over sand fields, curves around corners, etc.)
- This is code-golf, so the solution with the least amount of characters wins.
- Solutions must be able to handle all provided test-cases, if this is impossible due to restrictions of the used language please specify that in your answer.
Test cases
Course #1 (2 strikes)
####
# @#
#o~#
####
Course #2 (not possible)
#####
#@ #
# o #
# #
#####
Course #3 (3 strikes)
~~~
~@~
~.~
~ ~
~ ~
~ ~
~ ~
~.~
~o~
~~~
Course #4 (2 strikes)
#########
#~~~~~~~#
#~~~@~~~#
## . ##
#~ ~ ~ ~#
#~. o .~#
#~~~ ~~~#
#~~~~~~~#
#########
Course #5 (not possible)
~~~~~~~
~... ~
~.@.~.~
~... ~
~ ~ ~.~
~ . .o~
~~~~~~~