From 267a1e640b21840c26fd1506a3d6a76448cfb4f1 Mon Sep 17 00:00:00 2001 From: tzlil Date: Sun, 21 Apr 2024 00:21:25 +0300 Subject: make it nicer --- 10/Main.hs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to '10') diff --git a/10/Main.hs b/10/Main.hs index 0988b6d..27b4c8d 100644 --- a/10/Main.hs +++ b/10/Main.hs @@ -13,7 +13,7 @@ import Debug.Trace maze tiles = fold [f tile (x,y) | (y, row) <- enumerate tiles, (x, tile) <- enumerate row] where enumerate = zip [0..] pipe f g = (,First Nothing) . (Overlay.((Connect . Vertex) <*> Vertex . f) <*> (Connect . Vertex <*> Vertex . g)) - f '.' = (,) . Vertex <*> (const (First Nothing)) + f '.' = (,) . Vertex <*> const (First Nothing) f '|' = pipe (second (+1)) (second $ subtract 1) f '-' = pipe (first (+1)) (first $ subtract 1) f 'L' = pipe (second $ subtract 1) (first (+1)) @@ -29,17 +29,16 @@ mainloop tiles = circuit where g' = overlay (connect (vertex s) $ vertices . toList $ preSet s g) g [g''] = dfsForestFrom g' [s] -- find all paths from root (there should be only one) - summarize (Node l ts) = if ts /= [] then [l:summary | t <- ts, summary <- summarize t] else [[l]] + summarize (Node l []) = [[l]] + summarize (Node l ts) = [l:summary | t <- ts, summary <- summarize t] [circuit] = summarize g'' -part1 tiles = (length (mainloop tiles) - 1) `div` 2 + 1 where +part1 tiles = (length (mainloop tiles) - 1) `div` 2 + 1 part2 tiles = length enclosed where (g, First (Just s)) = maze tiles circuit = mainloop tiles - -- even-odd rule - -- inside tile = (foldl (/=) False $ [isLeftOf tile pipe | pipe <- circuit]) && (foldl (/=) False $ [isRightOf tile pipe | pipe <- circuit]) both = bimap <*> id pairwise = zip <*> tail @@ -52,9 +51,9 @@ part2 tiles = length enclosed where llintersect (a,b) (c,d) = ccw a c d /= ccw b c d && ccw a b c /= ccw a b d -- a tile is inside the circuit if it intersects an odd amount of edges - inside tile@(x,_) = foldl (/=) False $ llintersect (both fromIntegral $ tile,(fromIntegral x,1/0)) <$> pairwise offgrid + inside tile@(x,_) = foldl (/=) False $ llintersect (both fromIntegral tile,(fromIntegral x,1/0)) <$> pairwise offgrid -- all tiles that dont lie on the circuit that are inside it - enclosed = filter inside $ (vertexList g) \\ circuit + enclosed = filter inside $ vertexList g \\ circuit -main = readFile "input.txt" >>= (putStrLn . show . part2 . lines) +main = readFile "input.txt" >>= (print . part1 . lines) -- cgit 1.4.1