PPA-8
Question
Consider the following image of a chess-board (image borrowed from chess.com). The piece on the board is a bishop.
Accept two positions as input: start
and end
. Print YES if a bishop at start
can move to end
in exactly one move. Print NO otherwise. Note that a bishop can only move along diagonals.
Hint
The bishop is capable of only diagonal moves. In the problem statement, you have to figure out if the bishop can move from start
to end
in exactly one move. So you have to see if this movement is along a diagonal or not. What are the characteristics of a diagonal? In terms of coordinate geometry, what is the slope of a diagonal?
As far as the inputs are concerned, each position on the chessboard is given by two symbols: a letter and a number. The letter denotes the column and the number denotes the row. For example, the bishop in the image given above is at C4
. In the test cases, we use capital letters for the columns.
Now consider the following strings:
And the following snippet of code:
Can you come up with the solution using these hints?
Solution
start = input()
end = input()
cols = 'ABCDEFGH'
rows = '12345678'
start_col, start_row = start[0], start[1]
end_col, end_row = end[0], end[1]
col_diff = cols.index(start_col) - cols.index(end_col)
row_diff = rows.index(start_row) - rows.index(end_row)
# abs(x) returns the absolute value of x
if abs(col_diff) == abs(row_diff):
print('YES')
else:
print('NO')