This commit is contained in:
pingu 2024-12-12 10:47:05 +01:00
parent 769d144b77
commit 8a316f9f1b

View File

@ -7,34 +7,44 @@ import Data.List
import Debug.Trace import Debug.Trace
import Data.Maybe import Data.Maybe
type Pos = (Int,Int)
parse :: String -> Matrix Char parse :: String -> Matrix Char
parse = fromLists . lines parse = fromLists . lines
getGroup :: (Int,Int) -> Matrix Char -> [(Int,Int)] getGroup :: Pos -> Matrix Char -> [Pos]
getGroup (i,j) s = getGroup (i,j) s =
let e = getElem i j s in let e = getElem i j s in
go e [(i,j)] [] go e [(i,j)] []
where go :: Char -> [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)] where go :: Char -> [Pos] -> [Pos] -> [Pos]
go _ [] v = v go _ [] v = v
go c (x@(i,j):xs) v go c (x@(i,j):xs) v
| x `elem` v = go c xs v | x `elem` v = go c xs v
| uncurry safeGet x s /= Just c = go c xs v | uncurry safeGet x s /= Just c = go c xs v
| otherwise = go c (filter (isJust . flip (uncurry safeGet) s) [(i+1,j),(i-1,j),(i,j+1),(i,j-1)] ++ xs) (x:v) | otherwise = go c (filter (isJust . flip (uncurry safeGet) s) [(i+1,j),(i-1,j),(i,j+1),(i,j-1)] ++ xs) (x:v)
groups :: Matrix Char -> [[(Int,Int)]] groups :: Matrix Char -> [[Pos]]
groups s = go [(i,j) | i <- [1 .. nrows s], j <- [1 .. ncols s]] [] groups s = go [(i,j) | i <- [1 .. nrows s], j <- [1 .. ncols s]] []
where go :: [(Int,Int)] -> [[(Int,Int)]] -> [[(Int,Int)]] where go :: [Pos] -> [[Pos]] -> [[Pos]]
go [] v = v go [] v = v
go (x:xs) v go (x:xs) v
| any (x `elem`) v = go xs v | any (x `elem`) v = go xs v
| otherwise = go xs (nub (getGroup x s):v) | otherwise = go xs (nub (getGroup x s):v)
perimeter :: [(Int,Int)] -> Int perimeter :: [Pos] -> Int
perimeter s = length $ s >>= \(i,j) -> [(i,j+1),(i,j-1),(i+1,j),(i-1,j)] \\ s perimeter s = length $ s >>= \(i,j) -> [(i,j+1),(i,j-1),(i+1,j),(i-1,j)] \\ s
solve1 :: Matrix Char -> Int solve1 :: Matrix Char -> Int
solve1 s = let g = groups s; p = perimeter <$> g in sum $ zipWith (*) (length <$> g) p solve1 s = let g = groups s; p = perimeter <$> g in sum $ zipWith (*) (length <$> g) p
sides :: [Pos] -> Int
sides s = length $ go s []
where go :: [Pos] -> [(Pos,Pos)] -> [(Pos,Pos)]
go [] v = v
go ((y,x):xs) v
| any (\((y1,x1),(y2,x2)) -> (x == x1 && y1 <= y && y <= y2) || (y == y1 && x1 <= x && x <= x2)) v = go xs v
| otherwise = undefined
solve2 :: Matrix Char -> Int solve2 :: Matrix Char -> Int
solve2 = undefined solve2 = undefined