Skip to content

Commit

Permalink
Miscellaneous cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 23, 2022
1 parent 4c0e48d commit 3c9b797
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
5 changes: 3 additions & 2 deletions hs/src/Day14.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Description: <https://adventofcode.com/2022/day/14 Day 14: Regolith Reservoir
module Day14 (day14) where

import Common (readEntire)
import Control.Arrow ((&&&), (***))
import Control.Monad (forM_, when)
import Control.Monad.ST (ST, runST)
import Data.Array.MArray (MArray, newArray, readArray, writeArray)
Expand Down Expand Up @@ -40,8 +41,8 @@ parse input = do
]
| line <- T.lines input
]
f (x, y) = (Min x, Max x, Min y, Max y)
(Min minX, Max maxX, Min minY, Max maxY) = foldMap f $ concat segments
((Min minX, Max maxX), (Min minY, Max maxY)) =
foldMap ((Min &&& Max) *** (Min &&& Max)) $ concat segments

fill :: (MArray a Bool (ST s), Ix i, Num i, Show i) => a (i, i) Bool -> i -> ST s (Int, Int)
fill blocks maxY = do
Expand Down
5 changes: 3 additions & 2 deletions hs/src/Day18.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Description: <https://adventofcode.com/2022/day/18 Day 18: Boiling Boulders>
-}
module Day18 (day18a, day18b) where

import Common (count)
import Control.Arrow ((&&&))
import Data.Ix (inRange)
import Data.List (foldl')
Expand All @@ -17,12 +18,12 @@ neighbors (x, y, z) =
[(x - 1, y, z), (x + 1, y, z), (x, y - 1, z), (x, y + 1, z), (x, y, z - 1), (x, y, z + 1)]

day18a :: Text -> Int
day18a input = length . filter (`Set.notMember` points') $ points >>= neighbors where
day18a input = count (`Set.notMember` points') $ points >>= neighbors where
points = read @(Int, Int, Int) . T.unpack . T.cons '(' . flip T.snoc ')' <$> T.lines input
points' = Set.fromList points

day18b :: Text -> Int
day18b input = length . filter (`Set.member` outside) $ points >>= neighbors where
day18b input = count (`Set.member` outside) $ points >>= neighbors where
points = read @(Int, Int, Int) . T.unpack . T.cons '(' . flip T.snoc ')' <$> T.lines input
points' = Set.fromList points
((Min minX, Max maxX), (Min minY, Max maxY), (Min minZ, Max maxZ)) = mconcat
Expand Down
15 changes: 6 additions & 9 deletions hs/src/Day21.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,12 @@ day21b input = do
lhs :/ rhs -> (lhs, rhs)
monkeys' = Map.insert "humn" (1, 0) $ eval <$> monkeys
eval (Literal a) = (0, a % 1)
eval (x :+ y) = (monkeys' Map.! x) +: (monkeys' Map.! y)
eval (x :- y) = (monkeys' Map.! x) -: (monkeys' Map.! y)
eval (x :* y) = (monkeys' Map.! x) *: (monkeys' Map.! y)
eval (x :/ y) = (monkeys' Map.! x) /: (monkeys' Map.! y)
(a, b) +: (c, d) = (a + c, b + d)
(a, b) -: (c, d) = (a - c, b - d)
(0, b) *: (c, d) = (b * c, b * d)
(a, b) *: (0, d) = (a * d, b * d)
(a, b) /: (0, d) = (a / d, b / d)
eval (x :+ y) | (a, b) <- monkeys' Map.! x, (c, d) <- monkeys' Map.! y = (a + c, b + d)
eval (x :- y) | (a, b) <- monkeys' Map.! x, (c, d) <- monkeys' Map.! y = (a - c, b - d)
eval (x :* y)
| (0, b) <- monkeys' Map.! x, (c, d) <- monkeys' Map.! y = (b * c, b * d)
| (a, b) <- monkeys' Map.! x, (0, d) <- monkeys' Map.! y = (a * d, b * d)
eval (x :/ y) | (a, b) <- monkeys' Map.! x, (0, d) <- monkeys' Map.! y = (a / d, b / d)
(m, b) = monkeys' Map.! lhs
(n, c) = monkeys' Map.! rhs
x = (c - b) / (m - n)
Expand Down
5 changes: 3 additions & 2 deletions hs/src/Day23.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Description: <https://adventofcode.com/2022/day/23 Day 23: Unstable Diffusion
-}
module Day23 (day23a, day23b) where

import Control.Arrow ((&&&), (***))
import Data.List (findIndex, scanl', tails)
import qualified Data.Map as Map (elems, filter, fromListWith, keysSet)
import Data.Maybe (fromJust)
Expand Down Expand Up @@ -50,8 +51,8 @@ parse input = Set.fromList
day23a :: Text -> Int
day23a input = (maxX - minX + 1) * (maxY - minY + 1) - Set.size state where
state = scanl' step (parse input) dirs !! 10
(Min minX, Max maxX, Min minY, Max maxY) = mconcat
[(Min x, Max x, Min y, Max y) | (x, y) <- Set.toList state]
((Min minX, Max maxX), (Min minY, Max maxY)) =
foldMap ((Min &&& Max) *** (Min &&& Max)) state

day23b :: Text -> Int
day23b input = fromJust (findIndex id . zipWith (==) states $ drop 1 states) + 1 where
Expand Down

0 comments on commit 3c9b797

Please sign in to comment.