part 2
This commit is contained in:
parent
621fcf2b23
commit
8879ae24f9
|
@ -1,3 +1,4 @@
|
||||||
|
import Control.Arrow
|
||||||
import Data.Function
|
import Data.Function
|
||||||
import Data.Ix
|
import Data.Ix
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
|
@ -6,10 +7,10 @@ import Data.Ord
|
||||||
import Linear.V2
|
import Linear.V2
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = interact $ show . day11 . parse . lines
|
main = interact $ show . (day11 1 &&& day11 999999) . parse . lines
|
||||||
|
|
||||||
day11 :: [V2 Int] -> Int
|
day11 :: V2 Int -> [V2 Int] -> Int
|
||||||
day11 = (`div` 2) . sum . f . (expand =<< findEmptyRows)
|
day11 n = (`div` 2) . sum . f . (expandBy n =<< findEmptyRows)
|
||||||
where f xs = [sum $ abs $ x-y | x<- xs, y<- xs]
|
where f xs = [sum $ abs $ x-y | x<- xs, y<- xs]
|
||||||
|
|
||||||
parse :: [String] -> [V2 Int]
|
parse :: [String] -> [V2 Int]
|
||||||
|
@ -17,9 +18,9 @@ parse = map fst . filter ((=='#') . snd) . concat . zipWith (map . f) [0..] . ma
|
||||||
where f x (y,z) = (V2 y x, z)
|
where f x (y,z) = (V2 y x, z)
|
||||||
|
|
||||||
|
|
||||||
expand :: [V2 Int] -> [V2 Int] -> [V2 Int]
|
expandBy :: V2 Int -> [V2 Int] -> [V2 Int] -> [V2 Int]
|
||||||
expand [] input = input
|
expandBy _ [] input = input
|
||||||
expand (d:ds) input = expand (map (moveCoord d) ds) (map (moveCoord d) input)
|
expandBy n (d:ds) input = expandBy n (map (moveCoord n d) ds) (map (moveCoord n d) input)
|
||||||
|
|
||||||
|
|
||||||
findEmptyRows :: [V2 Int] -> [V2 Int]
|
findEmptyRows :: [V2 Int] -> [V2 Int]
|
||||||
|
@ -29,6 +30,6 @@ findEmptyRows input = map flipV2 (findEmptyRows $ map flipV2 input) ++ findEmpt
|
||||||
maxP = maximumBy (comparing (view _x)) input
|
maxP = maximumBy (comparing (view _x)) input
|
||||||
flipV2 (V2 x y) = V2 y x
|
flipV2 (V2 x y) = V2 y x
|
||||||
|
|
||||||
moveCoord :: V2 Int -> V2 Int -> V2 Int
|
moveCoord :: V2 Int -> V2 Int -> V2 Int -> V2 Int
|
||||||
moveCoord x y = if y^.p >= x ^.p then y + signum x else y
|
moveCoord n x y = if y^.p >= x ^.p then y + n * signum x else y
|
||||||
where p = if x^._x == 0 then _y else _x
|
where p = if x^._x == 0 then _y else _x
|
||||||
|
|
Loading…
Reference in New Issue
Block a user