Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Jercan committed Dec 26, 2023
1 parent dde38f0 commit 9c594cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
38 changes: 22 additions & 16 deletions src/Day24.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Day24 (main, part1, part2) where

import Util.Parser (Parser, parse)
import qualified Text.Parsec as P
import Control.Monad (forM_, join)
import qualified Text.Parsec as P
import Util.Parser (Parser, parse)
import Z3.Monad

type Vec3 = (Integer, Integer, Integer)
Expand All @@ -21,16 +21,16 @@ numP = negP P.<|> posP

vec3P :: Parser Vec3
vec3P = do
x <- numP <* P.char ',' <* P.spaces
y <- numP <* P.char ',' <* P.spaces
z <- numP
return (x, y, z)
x <- numP <* P.char ',' <* P.spaces
y <- numP <* P.char ',' <* P.spaces
z <- numP
return (x, y, z)

particleP :: Parser Particle
particleP = do
p <- vec3P <* P.spaces <* P.char '@' <* P.spaces
v <- vec3P
return (p, v)
p <- vec3P <* P.spaces <* P.char '@' <* P.spaces
v <- vec3P
return (p, v)

inputP :: Parser [Particle]
inputP = P.many1 (particleP <* P.spaces) <* P.eof
Expand All @@ -44,14 +44,19 @@ intersect ((x1, y1, _), (dx1, dy1, _)) ((x2, y2, _), (dx2, dy2, _)) =
d = (det ((x1, y1), (x1 + dx1, y1 + dy1)), det ((x2, y2), (x2 + dx2, y2 + dy2)))
x = det (d, xdiff) `div` div'
y = det (d, ydiff) `div` div'
in div' /= 0
&& 200000000000000 <= x && x <= 400000000000000 && 200000000000000 <= y && y <= 400000000000000
&& (x - x1 > 0) == (dx1 > 0) && (y - y1 > 0) == (dy1 > 0)
&& (x - x2 > 0) == (dx2 > 0) && (y - y2 > 0) == (dy2 > 0)
in div' /= 0
&& 200000000000000 <= x
&& x <= 400000000000000
&& 200000000000000 <= y
&& y <= 400000000000000
&& (x - x1 > 0) == (dx1 > 0)
&& (y - y1 > 0) == (dy1 > 0)
&& (x - x2 > 0) == (dx2 > 0)
&& (y - y2 > 0) == (dy2 > 0)

combinations2 :: [a] -> [(a, a)]
combinations2 [] = []
combinations2 (x:xs) = map ((,) x) xs ++ combinations2 xs
combinations2 (x : xs) = map ((,) x) xs ++ combinations2 xs

solution :: [Particle] -> Int
solution particles = length $ filter (uncurry intersect) $ combinations2 particles
Expand All @@ -68,7 +73,7 @@ script particles = do
dy <- mkFreshIntVar "dy"
dz <- mkFreshIntVar "dz"

forM_ (zip particles [0..]) $ \(((xi, yi, zi), (dxi, dyi, dzi)), i) -> do
forM_ (zip particles [0 ..]) $ \(((xi, yi, zi), (dxi, dyi, dzi)), i) -> do
ti <- mkFreshIntVar ("t" ++ (show :: Integer -> String) i)

xi' <- mkInteger xi
Expand Down Expand Up @@ -101,7 +106,8 @@ script particles = do
join . snd <$> (withModel $ \m -> (evalInt m sumXYZ))

part2 :: String -> IO String
part2 input = evalZ3 (script $ parse inputP input) >>= \case
part2 input =
evalZ3 (script $ parse inputP input) >>= \case
Just x -> return $ show x
Nothing -> return "No solution"

Expand Down
22 changes: 12 additions & 10 deletions src/Day25.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Day25 (main, part1) where

import qualified Data.Map as M
import Util.Parser (Parser, parse)
import qualified Text.Parsec as P
import Control.Monad (replicateM)
import Control.Monad.State (
MonadState (get, put),
State,
evalState,
modify,
)
import System.Random
import Data.List (minimumBy)
import Control.Monad (replicateM)
import Data.Function (on)
import Data.List (minimumBy)
import qualified Data.Map as M
import System.Random
import qualified Text.Parsec as P
import Util.Parser (Parser, parse)

pairP :: Parser (String, [String])
pairP = do
Expand All @@ -31,7 +31,8 @@ data KargerState = KargerState (StdGen)
choice :: StdGen -> [a] -> (a, StdGen)
choice g [] = error "choice: empty list"
choice g xs = (xs !! i, g')
where (i, g') = randomR (0, length xs - 1) g
where
(i, g') = randomR (0, length xs - 1) g

contract :: String -> String -> (M.Map String [String], M.Map String [String]) -> (M.Map String [String], M.Map String [String])
contract u v (g, s) =
Expand All @@ -41,7 +42,7 @@ contract u v (g, s) =
g'''' = M.update (\xs -> Just $ filter (/= u) xs) u g'''
s' = M.delete v $ M.delete u s
s'' = M.insert u (s M.! u ++ s M.! v) s'
in (g'''', s'')
in (g'''', s'')

minCutM :: M.Map String [String] -> M.Map String [String] -> State KargerState (Int, M.Map String [String])
minCutM graph s = do
Expand All @@ -51,14 +52,15 @@ minCutM graph s = do
else do
let keys = M.keys graph
let (u, g') = choice g keys
let (v, g'') = choice g' $ filter (\x -> x `M.member` graph) $ graph M.! u
let (v, g'') = choice g' $ filter (\x -> x `M.member` graph) $ graph M.! u
let (graph', s') = contract u v (graph, s)
put $ KargerState g''
minCutM graph' s'

karger :: M.Map String [String] -> (Int, M.Map String [String])
karger graph = head $ dropWhile (\(c, _) -> c > 3) $ evalState (replicateM ((length graph) * (length graph)) (minCutM graph s)) (KargerState $ mkStdGen 0)
where s = M.fromList $ zip (M.keys graph) (map (:[]) $ M.keys graph)
where
s = M.fromList $ zip (M.keys graph) (map (: []) $ M.keys graph)

solution :: M.Map String [String] -> String
solution graph = show $ M.foldl (*) 1 $ M.map length $ snd $ karger graph
Expand Down
1 change: 0 additions & 1 deletion test/TestDay24.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ test = hspec $ do

describe "part2" $ do
it "should work for the examples" test2

0 comments on commit 9c594cb

Please sign in to comment.