Rename square type to rectangle

This commit is contained in:
George Thomas 2025-12-09 19:50:35 +00:00
parent abda4f84d8
commit b438db7d53

View File

@ -9,7 +9,7 @@ puzzle =
, parser = const $ (V2 <$> decimal <* single ',' <*> decimal) `sepEndBy1` newline , parser = const $ (V2 <$> decimal <* single ',' <*> decimal) `sepEndBy1` newline
, parts = , parts =
[ maximum [ maximum
. fmap (squareSize . uncurry Square) . fmap (squareSize . uncurry Rectangle)
. fromMaybe (error "input too small") . fromMaybe (error "input too small")
. nonEmpty . nonEmpty
. allUnorderedPairs False . allUnorderedPairs False
@ -24,19 +24,19 @@ puzzle =
. fromMaybe (error "no solutions") . fromMaybe (error "no solutions")
. find (not . flip any path . lineIntersectsSquare . fst) . find (not . flip any path . lineIntersectsSquare . fst)
. sortOn (Down . snd) . sortOn (Down . snd)
. fmap ((id &&& squareSize) . uncurry Square) . fmap ((id &&& squareSize) . uncurry Rectangle)
$ allUnorderedPairs False points $ allUnorderedPairs False points
] ]
, extraTests = mempty , extraTests = mempty
} }
data Square = Square data Rectangle = Rectangle
{ corner1 :: V2 Int { corner1 :: V2 Int
, corner2 :: V2 Int , corner2 :: V2 Int
} }
deriving (Show) deriving (Show)
squareSize :: Square -> Int squareSize :: Rectangle -> Int
squareSize Square{corner1, corner2} = (\(V2 x y) -> x * y) . (+ 1) . fmap abs $ corner1 - corner2 squareSize Rectangle{corner1, corner2} = (\(V2 x y) -> x * y) . (+ 1) . fmap abs $ corner1 - corner2
data Line data Line
= LineHorizontal {y :: Int, x1 :: Int, x2 :: Int} = LineHorizontal {y :: Int, x1 :: Int, x2 :: Int}
@ -55,10 +55,10 @@ compareToInterval n (Interval l u)
| n >= u = GT | n >= u = GT
| otherwise = EQ | otherwise = EQ
squareIntervals :: Square -> V2 Interval squareIntervals :: Rectangle -> V2 Interval
squareIntervals Square{corner1, corner2} = uncurry Interval . sortPair <$> liftA2 (,) corner1 corner2 squareIntervals Rectangle{corner1, corner2} = uncurry Interval . sortPair <$> liftA2 (,) corner1 corner2
lineIntersectsSquare :: Square -> Line -> Bool lineIntersectsSquare :: Rectangle -> Line -> Bool
lineIntersectsSquare (squareIntervals -> V2 intervalX intervalY) = \case lineIntersectsSquare (squareIntervals -> V2 intervalX intervalY) = \case
LineHorizontal{y, x1, x2} -> LineHorizontal{y, x1, x2} ->
compareToInterval y intervalY == EQ compareToInterval y intervalY == EQ