Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 28, 2023
1 parent acef8ec commit e8ba318
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions solutions/src/2023/18.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# Language QuasiQuotes, LambdaCase #-}
{-# Language QuasiQuotes, LambdaCase, TemplateHaskell #-}
{-|
Module : Main
Description : Day 18 solution
Expand Down Expand Up @@ -42,18 +42,22 @@ U 2 (#7a21e3)
-}
module Main (main) where

import Advent (format)
import Advent.Coord (east, north, origin, scaleCoord, south, west, Coord(..), norm1)
import Advent (format, stageTH, partialSums)
import Advent.Coord (Coord(..), east, north, south, west, scaleCoord, norm1)
import Data.List (tails)

data D = DD | DU | DR | DL

stageTH

-- | Parse the input and print the answers to both parts.
--
-- >>> :main
-- 41019
-- 96116995735219
main :: IO ()
main =
do input <- [format|2023 18 (%c %d %(#%x%)%n)*|]
do input <- [format|2023 18 (@D %d %(#%x%)%n)*|]
print (area [scaleCoord n (asUnitVec d) | (d,n,_) <- input])
print (area [scaleCoord n ([east, south, west, north] !! d) | (_,_,x) <- input, let (n,d) = x `quotRem` 16])

Expand All @@ -63,19 +67,17 @@ main =
area :: [Coord] -> Int
area input = abs (polyareaRect path) + perimeter `quot` 2 + 1
where
path = scanl (+) origin input
perimeter = sum [norm1 n | n <- input]
path = partialSums input
perimeter = sum (map norm1 input)

-- | Convert the input character to a unit vector.
asUnitVec :: Char -> Coord
asUnitVec :: D -> Coord
asUnitVec = \case
'R' -> east
'D' -> south
'L' -> west
'U' -> north
_ -> error "bad direction digit"
DR -> east
DD -> south
DL -> west
DU -> north

-- | Area of a polygon using Shoelace formula
-- over a closed loop.
-- | Area of a polygon using Shoelace formula over a closed loop.
polyareaRect :: [Coord] -> Int
polyareaRect xs = sum [x1 * y2 - x2 * y1 | C y1 x1 : C y2 x2 : _ <- tails xs] `quot` 2

0 comments on commit e8ba318

Please sign in to comment.