Day 3: Extract unoverlap into its own function
authorJack Kinsey <j.jameskinsey@gmail.com>
Mon, 3 Dec 2018 16:45:24 +0000 (11:45 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Mon, 3 Dec 2018 16:45:24 +0000 (11:45 -0500)
day03/fabric.hs

index 792158837376f978b04d26bec7895888fcec9c97..6d7504286e7ae71f7f85c998b77775ebf25f8121 100644 (file)
@@ -6,7 +6,7 @@ main = do
     let layout = claimOverlap claims
     let counts = M.fromListWith (+) . map (\v -> (v,1)) . M.elems $ layout
     print $ counts M.! (-1)
-    print . index . head . filter (\c -> (fst . size $ c)*(snd . size $ c) == M.findWithDefault 0 (index c) counts) $ claims
+    print . index . claimUnoverlapped counts $ claims
 
 data Claim = Claim { index :: Int
                    , coord :: (Int, Int)
@@ -24,6 +24,12 @@ claimOverlap = foldl go M.empty . map claimedAreas
           go :: ClaimMap -> [(Int, (Int, Int))] -> ClaimMap
           go cmap = foldl mn cmap
 
+claimUnoverlapped :: M.Map Int Int -> [Claim] -> Claim
+claimUnoverlapped counts = head . filter go
+    where go :: Claim -> Bool
+          go (Claim ind _ (sx, sy)) = 
+              (Just (sx*sy)) == counts M.!? ind
+
 claimedAreas :: Claim -> [(Int, (Int, Int))]
 claimedAreas (Claim ind (cx, cy) (sx, sy)) = 
     [(ind,(i,j)) | i <- [cx+1..cx+sx], j <- [cy+1..cy+sy]]