47 lines
2.0 KiB
Haskell
47 lines
2.0 KiB
Haskell
|
import Data.List.Split
|
||
|
import Data.List
|
||
|
import qualified Data.Set as Set
|
||
|
import Data.Maybe
|
||
|
|
||
|
|
||
|
main = do
|
||
|
content <- getContents
|
||
|
let input = (lines content)
|
||
|
putStrLn ( show (input !! 0))
|
||
|
let input1 = Set.fromList (getCoordinates [] ( map getCommands (getList ( input !! 0))) (0,0))
|
||
|
let input2 = Set.fromList (getCoordinates [] ( map getCommands (getList ( input !! 1))) (0,0))
|
||
|
let listinput = getCoordinates [] ( map getCommands (getList ( input !! 0))) (0,0)
|
||
|
let listinput2 = getCoordinates [] ( map getCommands (getList ( input !! 1))) (0,0)
|
||
|
let intersects = input1 `Set.intersection` input2
|
||
|
let results = Set.map (manhattan (0,0)) intersects
|
||
|
let result = Set.findMin( results)
|
||
|
let coordinate = Set.filter(\x-> ((manhattan (0,0)) x) == result) intersects
|
||
|
let results2 = [(fromJust (elemIndex a listinput)) + (fromJust (elemIndex a $ listinput2)) | a<- (Set.toList intersects)]
|
||
|
let result2 = minimum results2
|
||
|
mapM putStrLn (map show( results2 ))
|
||
|
getList :: String -> [String]
|
||
|
getList = splitOn ","
|
||
|
|
||
|
getCommands :: String -> (Char,Int)
|
||
|
getCommands input = (op, number)
|
||
|
where
|
||
|
number = read (tail input)
|
||
|
op = head input
|
||
|
|
||
|
getCoordinates :: [(Int,Int)] -> [(Char,Int)] -> (Int,Int) -> [(Int,Int)]
|
||
|
getCoordinates coordinates (('U',length):xs) (x,y) =
|
||
|
getCoordinates (coordinates ++ [(x,y + i) | i<-[1..length]]) xs (x,y+length)
|
||
|
getCoordinates coordinates (('D',length):xs) (x,y) =
|
||
|
getCoordinates (coordinates ++ [(x,y - i) | i<-[1..length]]) xs (x,y-length)
|
||
|
getCoordinates coordinates (('R',length):xs) (x,y) =
|
||
|
getCoordinates (coordinates ++ [(x+i,y) | i<-[1..length]]) xs (x+length,y)
|
||
|
getCoordinates coordinates (('L',length):xs) (x,y) =
|
||
|
getCoordinates (coordinates ++ [(x-i,y) | i<-[1..length]]) xs (x-length,y)
|
||
|
getCoordinates coordinates [] (x,y) = coordinates
|
||
|
|
||
|
manhattan :: (Int,Int) -> (Int,Int) -> Int
|
||
|
manhattan (x1,y1) (x2,y2) = abs(x1-x2) + abs(y1-y2)
|
||
|
|
||
|
|
||
|
|