Day10:Part1 working

This commit is contained in:
Arranun 2019-12-10 14:01:09 +01:00
parent 57127b88dd
commit ce7044ecf1

35
day10.hs Normal file
View File

@ -0,0 +1,35 @@
import Data.List as List
main = do
content <- getContents
let layers = lines content
let indexlayers = map(\x-> mapInd(\x y -> (y,x)) x ) layers
let points = concat (mapInd(\x y->map(\x->((fst x,y),snd x))x) $ indexlayers)
let asteroids = map fst (filter(\(x,y) -> y == '#' ) points)
--let sortedAsteroids = sortBy sortDistance asteroids
--let view = length (getViews (asteroids) (3,4))
let result = List.maximum(map(\x-> length(getViews (asteroids) x)) (asteroids))
--mapM putStrLn (map(\(x,y)->(show x ) ++(show y)) asteroids)
--mapM putStrLn (map(\(x,y)->(show x ) ++(show y)) sortedAsteroids)
putStrLn ( show result)
mapM putStrLn (map show (layers))
mapInd :: (a -> Int -> b) -> [a] -> [b]
mapInd f l = zipWith f l [0..]
sortDistance ((a,b),c) ((a2,b2),c2)
| a + b > a2 + b2 = GT
| a + b < a2 + b2 = LT
| a + b == a2 + b2 = EQ
getViews :: [(Int,Int)] -> (Int,Int) -> [(Int,Int)]
getViews xs (a,b) = foldl getView [](delete (0,0)( (map(\(x,y) -> ((x-a),(y-b)))xs)))
getView :: [(Int,Int)] -> (Int,Int) -> [(Int,Int)]
getView xs y
|notElem (reduce y) (map reduce xs) = xs ++ [y]
|otherwise = xs
reduce :: (Int,Int) -> (Int,Int)
reduce (a,b) = ((div a (gcd a b)),(div b (gcd a b)))