Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
slightly cleaner day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
mstksg committed Dec 15, 2023
1 parent caa262a commit 87eb169
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/AOC/Challenge/Day14.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ import qualified Text.Megaparsec as P
import qualified Text.Megaparsec.Char as P
import qualified Text.Megaparsec.Char.Lexer as PP

shiftNorth :: Set Point -> Set Point -> Set Point
shiftNorth ps rs =
shiftDir :: Set Point -> Dir -> Set Point -> Set Point
shiftDir ps dir rs =
S.fromList
[ V2 x y
[ rotPoint (invert dir) $ V2 x y
| (x, ys) <- IM.toList fallens,
y <- IS.toList ys
]
Expand All @@ -59,7 +59,7 @@ shiftNorth ps rs =
IM.fromListWith
(<>)
[ (x, IM.singleton y k)
| (V2 x y, k) <-
| (rotPoint dir -> V2 x y, k) <-
((,True) <$> S.toList ps)
++ ((,False) <$> S.toList rs)
]
Expand All @@ -83,10 +83,11 @@ score :: Int -> Set Point -> Int
score maxRow pts = sum $ S.toList pts <&> \(V2 _ y) -> maxRow - y

shiftCycle :: Set Point -> Set Point -> Set Point
shiftCycle ps rs = snd . shiftRot . shiftRot . shiftRot . shiftRot $ (ps, rs)
where
shiftRot (ps', rs') = (rotator ps', rotator (shiftNorth ps' rs'))
rotator = S.map (rotPoint West)
shiftCycle ps =
shiftDir ps East
. shiftDir ps South
. shiftDir ps West
. shiftDir ps North

day14a :: _ :~> _
day14a =
Expand All @@ -100,7 +101,7 @@ day14a =
sSolve = noFail $ \mp ->
let (ps, rs) = bimap M.keysSet M.keysSet $ M.partition id mp
maxRow = maximum (view _y <$> M.keys mp) + 1
in score maxRow $ shiftNorth ps rs
in score maxRow $ shiftDir ps North rs
}

day14b :: _ :~> _
Expand All @@ -122,4 +123,3 @@ day14b =
iterate (shiftCycle ps) x !!! leftover
where
leftover = (1000000000 - i) `mod` (j - i)

0 comments on commit 87eb169

Please sign in to comment.