// // redKnight.swift // codesprint 12 // // Created by Avery Jones on 2017-12-14. // Copyright © 2017 Avery Jones. All rights reserved. // import Foundation //MARK: Funcs func moveLeft(loc : (Int,Int), n : Int) -> (Int, Int) { let x = loc.0 - n return (x, loc.1) } func moveRight(loc : (Int,Int), n : Int) -> (Int, Int) { let x = loc.0 + n return (x, loc.1) } func moveUp(loc : (Int,Int)) -> (Int, Int) { let y = loc.1 - 2 return (loc.0, y) } func moveDown(loc : (Int,Int)) -> (Int, Int) { let y = loc.1 + 2 return (loc.0, y) } func moveUpRight(loc : (Int, Int)) -> (Int, Int) { return moveUp(loc: moveRight(loc: loc, n: 1)) } func moveUpLeft(loc : (Int, Int)) -> (Int, Int) { return moveUp(loc: moveLeft(loc: loc, n: 1)) } func moveDownRight(loc : (Int, Int)) -> (Int, Int) { return moveDown(loc: moveRight(loc: loc, n: 1)) } func moveDownLeft(loc : (Int, Int)) -> (Int, Int) { return (moveDown(loc: moveLeft(loc: loc,n: 1))) } func impossible(start : (Int, Int), end : (Int, Int)) -> Bool { let verticalDistance = abs(end.0-start.0) if verticalDistance % 2 != 0 { //print("Impossible") return false } let horizontalDistance = abs(end.1-start.1) return true } func possible(start : (Int, Int), end : (Int, Int)) -> Bool { if end.0 < start.0 - 2 || end.0 > start.0 + 2 || end.1 < start.1 - 2 || end.1 > start.1 + 2 { return true } if end.1 == start.1 { if end.0 == (start.0 - 2) || end.0 == (start.0 + 2) { return true } } if end.1 == (start.1 + 1) || end.1 == ( start.1 - 1){ return false } if end.1 == (start.1 + 2){ if end.0 == (start.0 + 1){ return true //return distance(from: (start.0+1, start.1+2), to: end) } if end.0 == ( start.0 - 1){ return true //return distance(from: (start.0-1, start.1+2), to: end) } return false } if end.1 == ( start.1 - 2){ if end.0 == (start.0 + 1){ return true //return distance(from: (start.0+1, start.1-2), to: end) } if end.0 == ( start.0 - 1){ return true //return distance(from: (start.0-1, start.1-2), to: end) } return false } if end.0 == (start.0 + 1) || end.0 == (start.0 - 1) { return false } return true } func closestMove(from: (Int, Int), to : (Int, Int)) -> Int { var shortestDistance = distance(from: moveUpLeft(loc: from), to: to) var shortestIndex = 0; if(distance(from : moveUpRight(loc: from), to: to) < shortestDistance) { shortestDistance = distance(from : moveUpRight(loc: from), to: to) shortestIndex = 1; } if(distance(from : moveRight(loc: from, n: 2), to: to) < shortestDistance) { shortestDistance = distance(from : moveRight(loc: from, n: 2), to: to) shortestIndex = 2; } if(distance(from : moveDownRight(loc: from), to: to) < shortestDistance) { shortestDistance = distance(from : moveDownRight(loc: from), to: to) shortestIndex = 3; } if(distance(from : moveDownLeft(loc: from), to: to) < shortestDistance) { shortestDistance = distance(from : moveDownLeft(loc: from), to: to) shortestIndex = 4; } if(distance(from : moveLeft(loc: from, n: 2), to: to) < shortestDistance) { shortestDistance = distance(from : moveLeft(loc: from, n: 2), to: to) shortestIndex = 5; } return shortestIndex } func makeMove(from : (Int, Int), moveNumber : Int) -> (Int, Int) { switch moveNumber { case 0 : return moveUpLeft(loc: from) case 1 : return moveUpRight(loc: from) case 2 : return moveRight(loc: from, n: 2) case 3 : return moveDownRight(loc: from) case 4 : return moveDownLeft(loc: from) case 5 : return moveLeft(loc: from, n: 2) default: return from } } func indexToDirection(index : Int) -> String { switch index{ case 0 : return "UL" case 1 : return "UR" case 2 : return "R" case 3 : return "LR" case 4 : return "LL" case 5 : return "L" default: return "pogChamp" } } func distance(from : (Int, Int), to : (Int, Int)) -> Float { let x = abs(Float(from.0) - Float(to.0)) let y = abs(Float(from.1) - Float(to.1)) return sqrt((x*x)+(y*y)) } // Mark: Initialization var pathExists = true let n = Int(readLine()!)! let importantPoints = readLine()!.components(separatedBy: " ").map { Int($0)! } var currentLocation = (importantPoints[1], importantPoints[0]) let destinationLocation = (importantPoints[3], importantPoints[2]) var moveIndex = 0; var numberOfMoves = 0; var indexVector = [Int]() var outputString = "" var stringVector = [String]() //if(impossible(start: currentLocation, end: destinationLocation) == false){ // pathExists = false //} while(currentLocation != destinationLocation && pathExists) { //print("current location is \(currentLocation)") pathExists = possible(start: currentLocation, end: destinationLocation) //print("path from \(currentLocation) to \(destinationLocation) = \(pathExists)") moveIndex = closestMove(from: currentLocation, to: destinationLocation) indexVector.append(moveIndex) currentLocation = makeMove(from: currentLocation, moveNumber: moveIndex) numberOfMoves += 1 //if(possible(currentLocation, destinationLocation) == 1) } //print(pathExists) if(pathExists || currentLocation == destinationLocation) { indexVector = indexVector.sorted() for a in indexVector { stringVector.append(indexToDirection(index: a)) } print(stringVector.count) print(stringVector.joined(separator: " ")) } else { print ("Impossible") }