Aaron Powell - Shadows of the Knight Ep. 1¶
This solves the Shadows of the Knight Ep.1 puzzle.
Problem¶
From the Shadows of the Knight Ep.1 puzzle, the problem statement is below:
You (Batman) will look for the hostages on a given building by jumping from one window to another using your grapnel gun. Your goal is to jump to the window where the hostages are located in order to disarm the bombs. Unfortunately, you have a limited number of jumps before the bombs go off… Before each jump, the heat-signature device will provide you with the direction of the bombs based on your current position:
U(Up)
UR (Up Right)
R (Right)
DR (Down Right)
DL (Down Left)
L (Left)
UL (Up Left)
Your mission is to program the device so that it indicates the location of the next window you should jump to in order to reach the bombs’ room as soon as possible.
Solution Method¶
The following section demonstrate the solution used to solve the Shadows of the Knight Ep.1 Problem
Background¶
The Shadows of the Knight Ep.1 problem can be onsidered as a two dimensional binary search problem, with the width array being one binary search and the height array being another binary search. I chose to return the next spot for batman as the midpoint of the width and height indices. Below is a diagram of the problem solution
As depictied in the image above, using binary search can help window the target bomb for Batman. If one were to use the min and max boundaries in the width and height axis as a guide to either increment or decrement (on each axis respectfully) depending on the provided input direction of the relative bomb location, the bomb could be find in O(log(n)) time instead of iterating through each position in the respected axis. In order for Batman to find the bomb without going past the target area, it is necessary to have a small step size during the binary search algorithm. As a result, the algorithm should only update max_h/w
and min_h/w
by 1 depending on the relative direction of the bomb to batman.
Code¶
Provided Code:
To begin the game, the width, height, and number of turns before the game is over must be read in from the user (as shown below).
w
: width of the board game Batman can access
h
: height of the board game Batman can access
n
: maximum number of turns before game over.
x0
: x axis positiony0
: y axis positionw
: width of the buildingh
: height of the building
Below are 4 list mappings with the directions in them: upwards, downwards, left, and right
In order to keep track of the boundaries of the binary search algorithm, the minimum width min_w
and minimum height min_h
are set to 0. The maximum width max_w
and maximum height max_h
are intialized to the given input length minus 1 (zero based)
Game loop¶
bomb_dir
: the direction of the bombs from batman’s current location (U, UR, R, DR, D, DL, L or UL)
Write an action using print To debug: print(“Debug messages…”, file=sys.stderr, flush=True)
Variables
min_w
- minimum width indexmin_h
- minimum height indexmax_w
- maximum width indexmax_h
- maximum height indexx0
- X axis positiony0
- Y axis position
Update position of Batman
Print out the location of the next window Batman should jump to.