26
2
Given an input of four integers x1, y1, x2, and y2, output whether a white king in chess (with coordinates (x1, y1)) could catch a black pawn (with coordinates (x2, y2)) and capture it if the pawn is moving to promote to a queen as fast as possible.
The coordinates of the board are as follows:
first coordinate (x)
12345678
1 .#.#.#.#
2 #.#.#.#.
3 .#.#.#.#
second 4 #.#.#.#.
coordinate 5 .#.#.#.#
(y) 6 #.#.#.#.
7 .#.#.#.#
8 #.#.#.#.
Assume it is white to move (the king's turn) and that both players play optimally (the king will move as fast as possible to catch the pawn, and the pawn will move as fast as possible to promote). The input coordinates will always be distinct, and the pawn will never start with a y-coordinate of 8.
The king moves one square in any direction every turn (it can move diagonally), and the pawn can only move one space forwards (decrease its y-coordinate), unless it's at its initial position (with our coordinate system, y-coordinate of 7), in which case it can move two spaces forwards.
The input can be given as a whitespace-/comma-separated string, an array of strings/integers, or four function/command line/etc arguments. The coordinates can be given in whichever order is most convenient/golfy (so, accepting input as [y2, y1, x1, y2] is okay as long as it's consistent). The output must be a truthy or falsy value.
Since this is code-golf, the shortest code in bytes wins.
Truthy test cases:
5 3 3 2
6 1 1 7
3 3 3 2
4 1 4 7
7 7 1 7
1 8 1 7
Falsy test cases:
6 4 3 2
8 8 1 7
3 4 3 2
Can we chose the order of the coordinates or does it have to be
x1 y1 x2 y2
? – Dennis – 2016-01-07T03:04:25.183@Dennis I suppose whichever order is most convenient (i.e. golfiest) is okay. I'll edit the quesiton. – Doorknob – 2016-01-07T03:05:49.243
The write-up says that the pawn's starting position is in row 2, which suggests it's moving down the board, but the pictures have it moving up. Which is correct? (In other words, is the pawn's goal to reach 1 or 8?) – Jack Brounstein – 2016-01-07T03:08:14.683
@JackBrounstein That was an oversight on my part. Thanks, I'll fix that. – Doorknob – 2016-01-07T03:09:04.447
Can we assume that the king and pawn have distinct coordinates, and the the pawn does not start at row 8? – xnor – 2016-01-07T05:49:58.297
11I suggest the test case
1 8 1 7
, where the king captures before the pawn can move two spaces. I think all of the answers now get it wrong. This situation makes the problem a lot harder. – xnor – 2016-01-07T05:58:02.307@xnor Nice catch. Added that test case into the post. – Doorknob – 2016-01-07T12:10:52.037
@LegionMammal978 This problem only involves a single pawn, so en passant could not be relevant. – Doorknob – 2016-01-07T12:14:50.657
Why couldn't this have used actual chess notation e.g. c7, e6 etc. – Neil – 2016-01-07T13:06:27.107
1@Neil because the conversion to numbers is just boring and does not add nothing – edc65 – 2016-01-07T15:36:12.387
@edc65 well it could have at least used the Chess row numbers... – Neil – 2016-01-07T16:25:50.900
1@Neil There are already plenty of posts here involving algebraic notation, and I wanted to focus on the actual challenge. – Doorknob – 2016-01-07T17:36:58.570
Doesn't the king need to avoid capture by the pawn? (Not according to 7717) – kaine – 2016-01-07T19:29:35.703
2@kaine The pawn doesn't pose a significant threat to the king unless the king can't take it on or before the move it promotes. The king doesn't have to move into check because there is always an equally good move that it can make. – Neil – 2016-01-08T00:48:07.520