day3
This commit is contained in:
parent
43e2e103cb
commit
7b8e8914c4
|
@ -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)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user