Lets just be simple
This commit is contained in:
parent
4af44ebd0b
commit
7b28a5a035
108
day10.hs
108
day10.hs
|
@ -1,5 +1,5 @@
|
||||||
import Data.List as List
|
import Data.List as List
|
||||||
|
import Debug.Trace as Trace
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
content <- getContents
|
content <- getContents
|
||||||
|
@ -7,46 +7,45 @@ main = do
|
||||||
let indexlayers = map(\x-> mapInd(\x y -> (y,x)) x ) layers
|
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 points = concat (mapInd(\x y->map(\x->((fst x,y),snd x))x) $ indexlayers)
|
||||||
let asteroids = (map fst (filter(\(x,y) -> y == '#' ) points))
|
let asteroids = (map fst (filter(\(x,y) -> y == '#' ) points))
|
||||||
let changedAsteroids = changeCoordinate asteroids (2,2)
|
let maximum = List.maximum(map(\x-> length(getViews (changeCoordinate (asteroids) x))) (asteroids))
|
||||||
--let sortedAsteroids = sortBy sortDistance asteroids
|
let station = head $ map(\x-> fst x) $ filter(\x-> snd x == maximum )(map(\x-> (x,( length(getViews (changeCoordinate asteroids x))))) (asteroids))
|
||||||
--let view = length (getViews (asteroids) (3,4))
|
let changedAsteroids = changeCoordinate asteroids station
|
||||||
let maximum = List.maximum(map(\x-> length(getViews (asteroids) x)) (asteroids))
|
let sortedAsteroids = sortBy sortDistance changedAsteroids
|
||||||
let station = head $ map(\x-> fst x) $ filter(\x-> snd x == maximum )(map(\x-> (x,( length(getViews (asteroids) x)))) (asteroids))
|
let views = getViews sortedAsteroids
|
||||||
|
let sortedViews = sortBy sortDegree views
|
||||||
let views = (getViews asteroids (2,2))
|
let destroyed = getDestroyOrder sortedAsteroids []
|
||||||
let lines = map reduce(map(\(x,y)-> ((x-2),(y-2))) (getBorder (2,2) 4))
|
let destroyNormal = map (\(a,b)-> (((fst station) + a),((snd station) + b))) destroyed
|
||||||
let order = destroyOrders changedAsteroids lines (2,2) []
|
putStrLn(show maximum)
|
||||||
--let destroyed = destroyOrder views lines []
|
putStrLn(show station)
|
||||||
--mapM putStrLn (map(\(x,y)->(show x ) ++(show y)) asteroids)
|
putStrLn(show views)
|
||||||
--mapM putStrLn (map(\(x,y)->(show x ) ++(show y)) sortedAsteroids)
|
putStrLn(show sortedViews)
|
||||||
-- putStrLn ( show result)
|
putStrLn(show $ map degree destroyed)
|
||||||
--mapM putStrLn ( map show views)
|
putStrLn(show $ destroyNormal!!19)
|
||||||
putStrLn (show maximum)
|
|
||||||
putStrLn (show station)
|
|
||||||
mapM putStrLn ( map show order)
|
|
||||||
--mapM putStrLn (map show (layers))
|
|
||||||
--mapM putStrLn ( map show destroyed)
|
|
||||||
|
|
||||||
destroyOrders :: [(Int,Int)] ->[(Int,Int)] -> (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
|
|
||||||
destroyOrders ast laser station out
|
|
||||||
|length ast > 0 = do
|
|
||||||
let views = foldl getView [] ast
|
|
||||||
let destroyed = (destroyOrder views laser [])
|
|
||||||
let newout = out ++ destroyed
|
|
||||||
let newast = (ast \\ destroyed)
|
|
||||||
destroyOrders newast laser station newout
|
|
||||||
|otherwise = out
|
|
||||||
|
|
||||||
mapInd :: (a -> Int -> b) -> [a] -> [b]
|
mapInd :: (a -> Int -> b) -> [a] -> [b]
|
||||||
mapInd f l = zipWith f l [0..]
|
mapInd f l = zipWith f l [0..]
|
||||||
|
|
||||||
sortDistance ((a,b),c) ((a2,b2),c2)
|
getDestroyOrder :: [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)]
|
||||||
| a + b > a2 + b2 = GT
|
getDestroyOrder ast out
|
||||||
| a + b < a2 + b2 = LT
|
| length ast > 0 = do
|
||||||
| a + b == a2 + b2 = EQ
|
let views = getViews ast
|
||||||
|
let sortedViews = sortBy sortDegree views
|
||||||
|
let newout = out ++ sortedViews
|
||||||
|
let newast = ast \\ sortedViews
|
||||||
|
getDestroyOrder newast newout
|
||||||
|
| otherwise = out
|
||||||
|
|
||||||
getViews :: [(Int,Int)] -> (Int,Int) -> [(Int,Int)]
|
sortDistance ((a,b)) ((a2,b2))
|
||||||
getViews xs (a,b) = foldl getView [](delete (0,0)( (map(\(x,y) -> ((x-a),(y-b)))xs)))
|
| abs(a) + abs(b) > abs(a2) + abs(b2) = GT
|
||||||
|
| abs(a) + abs(b) < abs(a2) + abs(b2) = LT
|
||||||
|
| abs(a) + abs(b) == abs(a2) + abs(b2) = EQ
|
||||||
|
|
||||||
|
sortDegree a b
|
||||||
|
| degree a > degree b = GT
|
||||||
|
| degree a < degree b = LT
|
||||||
|
| degree a == degree b = EQ
|
||||||
|
|
||||||
|
getViews :: [(Int,Int)] -> [(Int,Int)]
|
||||||
|
getViews xs = foldl getView [] xs
|
||||||
|
|
||||||
changeCoordinate :: [(Int, Int)] -> (Int,Int) -> [(Int,Int)]
|
changeCoordinate :: [(Int, Int)] -> (Int,Int) -> [(Int,Int)]
|
||||||
changeCoordinate xs (a,b) = (delete (0,0)( (map(\(x,y) -> ((x-a),(y-b)))xs)))
|
changeCoordinate xs (a,b) = (delete (0,0)( (map(\(x,y) -> ((x-a),(y-b)))xs)))
|
||||||
|
@ -59,33 +58,8 @@ getView xs y
|
||||||
reduce :: (Int,Int) -> (Int,Int)
|
reduce :: (Int,Int) -> (Int,Int)
|
||||||
reduce (a,b) = ((div a (gcd a b)),(div b (gcd a b)))
|
reduce (a,b) = ((div a (gcd a b)),(div b (gcd a b)))
|
||||||
|
|
||||||
destroyOrder :: [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)]
|
degree :: (Int,Int) -> Double
|
||||||
destroyOrder ast (x:xs) out
|
degree (a,b) = do
|
||||||
| length xs > 0 = do
|
let x = fromIntegral a
|
||||||
let asteroid = getLineAst ast x
|
let y = fromIntegral b
|
||||||
if length asteroid /= 0
|
y / x
|
||||||
then do
|
|
||||||
let newast = delete (head asteroid) ast
|
|
||||||
let newout = out ++ asteroid
|
|
||||||
destroyOrder newast xs newout
|
|
||||||
else
|
|
||||||
destroyOrder ast xs out
|
|
||||||
| length xs == 0 = do
|
|
||||||
let asteroid = getLineAst ast x
|
|
||||||
if length asteroid /= 0
|
|
||||||
then out ++ asteroid
|
|
||||||
else out
|
|
||||||
|
|
||||||
getLineAst :: [(Int,Int)] -> (Int,Int) -> [(Int,Int)]
|
|
||||||
getLineAst xs r =
|
|
||||||
filter(\x->reduce(x) == r) xs
|
|
||||||
|
|
||||||
getBorder :: (Int,Int) -> Int -> [(Int,Int)]
|
|
||||||
getBorder (a,b) l = do
|
|
||||||
let p1 = [(i,0) | i <- [a..l]]
|
|
||||||
let p2 = [(l,j) | j <- [1..l]]
|
|
||||||
let p3 = [(i,l) | i <- reverse [0..(l-1)]]
|
|
||||||
let p4 = [(l,j) | j <- [(l-1)..0]]
|
|
||||||
let p5 = [(0,j) | j <- reverse [0..(l-1)]]
|
|
||||||
let p6 = [(i,0) | i <- [0..(a-1)]]
|
|
||||||
p1 ++ p2 ++ p3 ++ p4 ++ p5 ++ p6
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user