X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent22%2FMain.hs;h=3712b67459ad794f63427508863e947a1300bd1b;hb=f961efa630553f9630649481bc6380ec70949720;hp=51c7a8e23f8e7d0af1fcc25b6993fa7ec3628893;hpb=e2b15781f674220586e860fb9a85b6ad0f278fad;p=advent-of-code-21.git diff --git a/advent22/Main.hs b/advent22/Main.hs index 51c7a8e..3712b67 100644 --- a/advent22/Main.hs +++ b/advent22/Main.hs @@ -1,13 +1,11 @@ --- Writeup at https://work.njae.me.uk/2021/12/21/advent-of-code-2021-day-19/ +-- Writeup at https://work.njae.me.uk/2021/12/29/advent-of-code-2021-day-22/ import Data.Text () import qualified Data.Text.IO as TIO import Data.Attoparsec.Text -- hiding (take, takeWhile) import Control.Applicative -import Linear -- (V3(..), (^+^), (^-^)) --- import Linear.V3 --- import Data.Ix +import Linear import Control.Lens import Data.List @@ -31,8 +29,6 @@ main = let cuboids = successfulParse text print $ part1 cuboids print $ part2 cuboids - -- print $ part2 transScanners - part1 cuboids = sweepX (filter isLocal cuboids) part2 cuboids = sweepX cuboids @@ -57,34 +53,34 @@ isActive cs = ((last scs) ^. parity) == On where scs = sortOn (^. time) cs sweepX :: [Cuboid] -> Int -sweepX cuboids = sum $ map (volumeSize cuboids) segments +sweepX cuboids = sum $ map (volumeSize cuboids) $ segment evs where evs = events _x cuboids - segments = if null evs then [] else zip evs $ tail evs volumeSize :: [Cuboid] -> (Int, Int) -> Int volumeSize cuboids (here, there) = (sweepY cuboidsHere) * (there - here) where cuboidsHere = filter (straddles _x here) cuboids +-- assume for a given x sweepY :: [Cuboid] -> Int -sweepY cuboids = sum $ map (areaSize cuboids) segments +sweepY cuboids = sum $ map (areaSize cuboids) $ segment evs where evs = events _y cuboids - segments = if null evs then [] else zip evs $ tail evs areaSize :: [Cuboid] -> (Int, Int) -> Int -areaSize cuboids (here, there) = (countActive cuboidsHere) * (there - here) +areaSize cuboids (here, there) = (sweepZ cuboidsHere) * (there - here) where cuboidsHere = filter (straddles _y here) cuboids -- assume for a given x and y. -countActive :: [Cuboid] -> Int -countActive cuboids = sum $ map (segmentSize cuboids) segments +sweepZ :: [Cuboid] -> Int +sweepZ cuboids = sum $ map (segmentSize cuboids) $ segment evs where evs = events _z cuboids - segments = if null evs then [] else zip evs $ tail evs segmentSize :: [Cuboid] -> (Int, Int) -> Int segmentSize cuboids (here, there) | isActive $ filter (straddles _z here) cuboids = (there - here) | otherwise = 0 +segment :: [Int] -> [(Int, Int)] +segment evs = if null evs then [] else zip evs $ tail evs -- Parse the input file