-- http://nabetani.sakura.ne.jp/hena/ord5railsontiles/
-- Input is 9 tiles
-- Arrange tiles and enter from Tile B, top
-- Trace the rail and print the tiles traversed until you corss 3 X 3 boundary
inputList = ["101221102", "000000000", "111111111", "222222222",
"000211112", "221011102", "201100112", "000111222",
"012012012", "201120111", "220111122", "221011022",
"111000112", "001211001", "111222012", "220111211",
"211212212", "002112210", "001010221", "100211002",
"201212121"]
numList = map show [0, 1..]
hashList = repeat "#"
alphaList = "ABCDEFGHI"
dTop = 0
dLft = 1
dBot = 2
dRgt = 3
main = testAll inputList where
testAll = putStr . unlines .
zipWith addWithoutSp hashList .
zipWith addWithSp numList .
zipWith addWithSp inputList . map findRail
addWithSp a b = a ++ " " ++ b
addWithoutSp a b = a ++ b
findRail = findRailInt 0 1 dTop
-- Outside 3 X 3
findRailInt (-1) _ _ _ = ""
findRailInt 3 _ _ _ = ""
findRailInt _ (-1) _ _ = ""
findRailInt _ 3 _ _ = ""
-- findRailInt i j dDir a = [ alphaList !! idx ] ++ " " ++ show newi ++ " " ++ show newj ++ " " ++ show newDir ++ " " ++ findRailInt newi newj newDir a where
findRailInt i j dDir a = [ alphaList !! idx ] ++ findRailInt newi newj newDir a where
idx = 3 * i + j
tileType = a !! idx
newDir = calcDir tileType dDir
calcDir '0' 0= dTop
calcDir '0' 2 = dBot
calcDir '0' 1 = dLft
calcDir '0' 3 = dRgt
calcDir '1' 0 = dLft
calcDir '1' 3 = dBot
calcDir '1' 1 = dTop
calcDir '1' 2 = dRgt
calcDir '2' 0 = dRgt
calcDir '2' 1 = dBot
calcDir '2' 2 = dLft
calcDir '2' 3 = dTop
newi = calcNewI newDir i
newj = calcNewJ newDir j
calcNewI 0 i = i + 1
calcNewI 2 i = i - 1
calcNewI _ i = i
calcNewJ 1 j = j + 1
calcNewJ 3 j = j - 1
calcNewJ _ j = j