--- 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
let cuboids = successfulParse text
print $ part1 cuboids
print $ part2 cuboids
- -- print $ part2 transScanners
-
part1 cuboids = sweepX (filter isLocal cuboids)
part2 cuboids = sweepX cuboids
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