From: Neil Smith Date: Wed, 29 Dec 2021 16:46:28 +0000 (+0000) Subject: Tidyied day 22 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=063d9534648f286c83f6f26ae619cd670acfcc4a;p=advent-of-code-21.git Tidyied day 22 --- 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 diff --git a/problems/day21-x10.svg b/problems/day21-x10.svg new file mode 100644 index 0000000..3e2e3e9 --- /dev/null +++ b/problems/day21-x10.svg @@ -0,0 +1,5455 @@ + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y + + + + + + z + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 13 + + + + + + 14 + + + + + + 12 + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 12 + + + + + + 13 + + + + + + 14 + + + + + + x = 10 + + + + + + + + diff --git a/problems/day21-x11.svg b/problems/day21-x11.svg new file mode 100644 index 0000000..0827324 --- /dev/null +++ b/problems/day21-x11.svg @@ -0,0 +1,5457 @@ + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y + + + + + + z + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 13 + + + + + + 14 + + + + + + 12 + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 12 + + + + + + 13 + + + + + + 14 + + + + + + x = 11 + + + + + + + +