-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
181 lines (149 loc) · 4.62 KB
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import AoC.Parse (numP)
import Data.Foldable
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
type N = Int
type Parser = Parsec Void String
type Bounds = (N, N)
data Instruction = Instr Bool Bounds Bounds Bounds
deriving Show
isOn :: Instruction -> Bool
isOn = \case Instr on _ _ _ -> on
instrP :: Parser Instruction
instrP = do
b <- (== "on") <$> (try (string "on") <|> string "off")
_ <- char ' '
x <- boundsP 'x'
_ <- char ','
y <- boundsP 'y'
_ <- char ','
z <- boundsP 'z'
pure $ Instr b x y z
boundsP :: Char -> Parser Bounds
boundsP c = do
_ <- char c *> char '='
l <- numP
_ <- string ".."
h <- numP
pure (l, h)
parseAll :: String -> [Instruction]
parseAll =
map (\case Right v -> v)
. map (\s -> parse instrP "" s)
. lines
data Cuboid = Cuboid { cX :: (N, N)
, cY :: (N, N)
, cZ :: (N, N)
}
deriving (Show, Eq, Ord)
-- EXCLUSIVE upper bounds
volume :: Cuboid -> Integer
volume Cuboid {..} = s cX * s cY * s cZ
where s (l, h) = fromIntegral $ h - l
cuboid :: Instruction -> Cuboid
cuboid (Instr _ x y z) =
Cuboid x y z
type SplitCubes = ([Bounds], [Bounds], [Bounds])
-- inclusive bounds
overlaps1d :: Bounds -> Bounds -> Bool
overlaps1d (l1, h1) (l2, h2) =
not $ h1 < l2 || l1 > h2
-- inclusive bounds
overlaps3d :: Cuboid -> Cuboid -> Bool
overlaps3d c1 c2 =
overlaps1d (cX c1) (cX c2)
&& overlaps1d (cY c1) (cY c2)
&& overlaps1d (cZ c1) (cZ c2)
clusters :: [Cuboid] -> [[Cuboid]]
clusters = foldr merge []
where merge c as =
let (matched, rest) = partition (any (overlaps3d c)) as
in (c:concat matched):rest
instructionClusters :: [Instruction] -> [[Instruction]]
instructionClusters instrs =
let cs = clusters $ map cuboid instrs
in
map (\c -> filter (\i -> cuboid i `elem` c) instrs) cs
-- exclusive upper bound
overlaps1d' :: Bounds -> Bounds -> Bool
overlaps1d' (l1, h1) (l2, h2) =
not $ h1 <= l2 || l1 >= h2
-- exclusive upper bounds
overlaps3d' :: Cuboid -> Cuboid -> Bool
overlaps3d' c1 c2 =
overlaps1d' (cX c1) (cX c2)
&& overlaps1d' (cY c1) (cY c2)
&& overlaps1d' (cZ c1) (cZ c2)
cuboid' :: Instruction -> Cuboid
cuboid' (Instr _ (xl, xh) (yl, yh) (zl, zh)) =
Cuboid (xl, xh+1) (yl, yh+1) (zl, zh+1)
fullCovering' :: [Cuboid] -> SplitCubes
fullCovering' cuboids =
let bx = bounds' $ map cX cuboids
by = bounds' $ map cY cuboids
bz = bounds' $ map cZ cuboids
in (bx, by, bz)
bounds' :: [Bounds] -> [Bounds]
bounds' bs =
let combined = IntSet.toList
. IntSet.fromList
. concatMap (\(l, h) -> [l, h])
$ bs
in zip combined (drop 1 combined)
covering' :: Bounds -> [Bounds] -> [Bounds]
covering' (l, h) = takeWhile p . dropWhile (not . p)
where p (l', _) = l <= l' && l' < h
covering3d' :: SplitCubes -> Cuboid -> [Cuboid]
covering3d' (bsx, bsy, bsz) c =
let bsx' = covering' (cX c) bsx
bsy' = covering' (cY c) bsy
bsz' = covering' (cZ c) bsz
in
Cuboid <$> bsx' <*> bsy' <*> bsz'
-- split two lists of cuboids so that they overlap exactly
jitSplit :: [Cuboid] -> [Cuboid] -> ([Cuboid], [Cuboid])
jitSplit xs ys = go xs ys []
where go xs [] acc = (xs, acc)
go xs (y:ys) acc =
case partition (overlaps3d' y) xs of
([], _) -> go xs ys (y:acc)
(ol, rest) ->
let cs = fullCovering' (y:ol)
xs' = concatMap (covering3d' cs) ol
ys' = concatMap (covering3d' cs) ys
y' = covering3d' cs y
in go (xs' ++ rest) ys' (y' ++ acc)
-- TODO: improvement, specialize the split above to perform the
-- operation too.
partSolve' :: [Instruction] -> Integer
partSolve' = sum . map volume . toList . foldl' f Set.empty
where f on i =
let (on', i') = jitSplit (toList on) [cuboid' i]
in if isOn i
then Set.fromList $ on' ++ i'
else Set.fromList on' `Set.difference` Set.fromList i'
part1 :: [Instruction] -> Integer
part1 = partSolve' . head . instructionClusters
part2 :: [Instruction] -> Integer
part2 = sum . map partSolve' . instructionClusters
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)