From 7b8e8914c4dc80f4ea44e6ec34e317f2ea878679 Mon Sep 17 00:00:00 2001 From: Arranun Date: Thu, 5 Dec 2019 08:29:46 +0100 Subject: [PATCH] day3 --- day3.hs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 day3.hs diff --git a/day3.hs b/day3.hs new file mode 100644 index 0000000..d1266b4 --- /dev/null +++ b/day3.hs @@ -0,0 +1,46 @@ +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) + + +