I have a daily puzzle calendar on my desk and I found a puzzle that I thought may be fun to solve using SQL.
Sample Data
WITH matrix AS
(
SELECT 'A' AS ID, 1 AS A,1 AS B,1 AS C,0 AS D,0 AS E,0 AS F,1 AS G,0 AS H,1 AS I,0 AS J FROM DUAL UNION ALL
SELECT 'B' AS ID, 0,0,0,0,1,1,1,0,1,0 FROM DUAL UNION ALL
SELECT 'C' AS ID, 0,1,1,1,1,0,0,0,0,1 FROM DUAL UNION ALL
SELECT 'D' AS ID, 1,0,0,0,1,0,1,0,0,0 FROM DUAL UNION ALL
SELECT 'E' AS ID, 1,1,0,0,0,0,0,0,0,1 FROM DUAL UNION ALL
SELECT 'F' AS ID, 0,1,1,0,1,1,0,0,0,0 FROM DUAL UNION ALL
SELECT 'G' AS ID, 0,1,0,1,1,1,0,1,0,1 FROM DUAL UNION ALL
SELECT 'H' AS ID, 1,1,0,1,0,0,0,1,0,0 FROM DUAL UNION ALL
SELECT 'I' AS ID, 0,1,1,1,0,1,1,1,0,1 FROM DUAL UNION ALL
SELECT 'J' AS ID, 1,0,0,0,0,0,0,1,0,1 FROM DUAL UNION ALL
SELECT 'K' AS ID, 1,0,0,1,1,1,0,1,1,0 FROM DUAL UNION ALL
SELECT 'L' AS ID, 0,0,1,0,1,1,0,0,1,0 FROM DUAL UNION ALL
SELECT 'M' AS ID, 0,0,1,0,0,0,0,1,1,1 FROM DUAL UNION ALL
SELECT 'N' AS ID, 1,0,1,0,0,0,1,1,1,0 FROM DUAL UNION ALL
SELECT 'O' AS ID, 1,0,0,1,1,0,1,0,0,0 FROM DUAL
)
SELECT *
FROM MATRIX
ORDER BY 1
Problem Statement
Suppose we have a matrix of one's and zero's like the following:
ID A B C D E F G H I J
-- -- -- -- -- -- -- -- -- -- --
A 1 1 1 0 0 0 1 0 1 0
B 0 0 0 0 1 1 1 0 1 0
C 0 1 1 1 1 0 0 0 0 1
D 1 0 0 0 1 0 1 0 0 0
E 1 1 0 0 0 0 0 0 0 1
F 0 1 1 0 1 1 0 0 0 0
G 0 1 0 1 1 1 0 1 0 1
H 1 1 0 1 0 0 0 1 0 0
I 0 1 1 1 0 1 1 1 0 1
J 1 0 0 0 0 0 0 1 0 1
K 1 0 0 1 1 1 0 1 1 0
L 0 0 1 0 1 1 0 0 1 0
M 0 0 1 0 0 0 0 1 1 1
N 1 0 1 0 0 0 1 1 1 0
O 1 0 0 1 1 0 1 0 0 0
Positions in this matrix are defined using (ROW,COLUMN) throughout the problem statement.
Challenge
Our goal is from a start position identified as (A,E), First Row, Fifth column, traverse DOWN the matrix to reach a valid point on row "O."
Restrictions
1. You can only move UP, DOWN, LEFT, or RIGHT (not diagonally) by one unit.
2. The path must be a repeating pattern of 0 1 0 1 0 1 ... etc For example a move from (A,E) to (B,E) is valid while a move from (A,E) to (A,F) is not.
Correct Solution
The correct solution has the following requirements:
1. Identifies the path from start to finish using an identifiable way to determine the ROW,COLUMN for each entry point in the path while abiding by the restrictions above.
2. PL/SQL and SQL are acceptable.
3. Solution must be perform well and be elegant as judged (loosely, since we don't have any voting functionality) by your peers. This solution will be marked as "Correct." Helpful points will be given to runner ups as determined by your peers.
Enjoy :D