Tweaked some parsing code
[advent-of-code-21.git] / advent22 / Main.hs
index 51c7a8e23f8e7d0af1fcc25b6993fa7ec3628893..ca6bddd6a994ba4c4078d82ed612031118fa67b7 100644 (file)
@@ -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,36 @@ 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
+segment [] = []
+segment evs@(_ : tevs) = zip evs tevs
 
 -- Parse the input file
 
@@ -101,7 +99,8 @@ cuboidP = cubify <$> (partiyP <* " ") <*> (boundsP `sepBy` ",")
                  }
         vecify [c1, c2, c3] = V3 c1 c2 c3
 
-partiyP = ("on" *> pure On) <|> ("off" *> pure Off)
+-- partiyP = ("on" *> pure On) <|> ("off" *> pure Off)
+partiyP = (On <$ "on") <|> (Off <$ "off")
 
 boundsP = (,) <$> (("x" <|> "y" <|> "z") *> "=" *> signed decimal) <*> (".." *> signed decimal)