Day 10: Minor cleanup
Still not pretty but at least nothing glaring
This commit is contained in:
parent
e111bbc48d
commit
b2542fa250
|
@ -1,42 +1,26 @@
|
||||||
{-# LANGUAGE LambdaCase #-}
|
|
||||||
{-# LANGUAGE BangPatterns #-}
|
|
||||||
{-# LANGUAGE ParallelListComp #-}
|
|
||||||
|
|
||||||
import Data.List as L
|
import Data.List as L
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.Ord
|
import Data.Ord
|
||||||
import Data.Ratio
|
|
||||||
import qualified Data.Vector as V
|
import qualified Data.Vector as V
|
||||||
|
|
||||||
type Asteroid_old = (Int, Int)
|
|
||||||
|
|
||||||
data Asteroid =
|
data Asteroid =
|
||||||
Asteroid Int Int
|
Asteroid Int Int
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
instance Eq Asteroid where
|
instance Eq Asteroid where
|
||||||
Asteroid a b == Asteroid c d = reduce a b == reduce c d
|
Asteroid a b == Asteroid c d = phi' a b == phi' c d
|
||||||
where
|
|
||||||
reduce x y
|
|
||||||
| y == 0 && x == 0 = (0, 0)
|
|
||||||
| x == 0 = (0, signum y)
|
|
||||||
| y == 0 = (signum x, 0)
|
|
||||||
| otherwise =
|
|
||||||
(signum x * (abs $numerator z), signum y * (abs $denominator z))
|
|
||||||
where
|
|
||||||
z = x %y
|
|
||||||
|
|
||||||
instance Ord Asteroid where
|
instance Ord Asteroid where
|
||||||
Asteroid a b `compare` Asteroid c d = (phi a b) `compare` (phi c d)
|
Asteroid a b `compare` Asteroid c d = phi a b `compare` phi c d
|
||||||
where
|
where
|
||||||
phi x y = phi' (fromIntegral x) (fromIntegral y)
|
phi x y = phi' (fromIntegral x) (fromIntegral y)
|
||||||
phi' :: Double -> Double -> Double
|
|
||||||
phi' x y
|
|
||||||
| x >= 0 = atan2 x (-y)
|
|
||||||
| otherwise = 2 * pi + atan2 (-x) y
|
|
||||||
|
|
||||||
(+|) :: Asteroid -> Asteroid -> Asteroid
|
phi' :: Int -> Int -> Double
|
||||||
(+|) (Asteroid a b) (Asteroid c d) = Asteroid (a + c) (b + d)
|
phi' xi yi
|
||||||
|
| x >= 0 = atan2 x (-y)
|
||||||
|
| otherwise = 2 * pi + atan2 (-x) y
|
||||||
|
where x = fromIntegral xi
|
||||||
|
y = fromIntegral yi
|
||||||
|
|
||||||
(-|) :: Asteroid -> Asteroid -> Asteroid
|
(-|) :: Asteroid -> Asteroid -> Asteroid
|
||||||
(-|) (Asteroid a b) (Asteroid c d) = Asteroid (a - c) (b - d)
|
(-|) (Asteroid a b) (Asteroid c d) = Asteroid (a - c) (b - d)
|
||||||
|
@ -47,11 +31,11 @@ neg (Asteroid a b) = Asteroid (-a) (-b)
|
||||||
dist :: Asteroid -> Int
|
dist :: Asteroid -> Int
|
||||||
dist (Asteroid x y) = abs x + abs y
|
dist (Asteroid x y) = abs x + abs y
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
content <- lines <$> readFile "input"
|
content <- lines <$> readFile "input"
|
||||||
print $ parse content
|
let (aMax,sMax) = maxInSight $ parse content
|
||||||
let aMax = fst $ maxInSight $ parse content
|
print sMax
|
||||||
print aMax
|
|
||||||
let newlist =
|
let newlist =
|
||||||
listCycle $
|
listCycle $
|
||||||
sort $
|
sort $
|
||||||
|
@ -84,14 +68,4 @@ maxInSight :: [Maybe Asteroid] -> (Asteroid, Int)
|
||||||
maxInSight xs =
|
maxInSight xs =
|
||||||
maximumBy (comparing snd) $ map (f . transformCoordinates xs) (catMaybes xs)
|
maximumBy (comparing snd) $ map (f . transformCoordinates xs) (catMaybes xs)
|
||||||
where
|
where
|
||||||
f (x, y) = (x, pred . length . nub $ y)
|
f (x, y) = (x, length . nub $ y)
|
||||||
|
|
||||||
asts :: [String] -> [Maybe Asteroid_old]
|
|
||||||
asts xxs =
|
|
||||||
L.nub
|
|
||||||
[ if (xxs !! y) !! (x) == '.'
|
|
||||||
then Nothing
|
|
||||||
else Just (x, y)
|
|
||||||
| y <- [0 .. (L.length $ L.head xxs) - 1]
|
|
||||||
, x <- [0 .. L.length xxs - 1]
|
|
||||||
]
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user