do not use intermediate sets
This commit is contained in:
parent
7e38572684
commit
fbf5324a9d
|
|
@ -6,14 +6,14 @@ import Debug.Trace (traceShow, trace)
|
||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
type Algorithm = A.Array Int Bool
|
type Algorithm = A.Array Int Bool
|
||||||
data Image = Img Bool (S.Set (Int,Int)) deriving (Show)
|
data Image = Img Bool [(Int,Int)] deriving (Show)
|
||||||
|
|
||||||
getNd :: Image -> S.Set (Int,Int)
|
getNd :: Image -> [(Int,Int)]
|
||||||
getNd (Img _ s) = s
|
getNd (Img _ s) = s
|
||||||
|
|
||||||
getLit :: Image -> Int
|
getLit :: Image -> Int
|
||||||
getLit (Img True s) = error "all elems lit"
|
getLit (Img True s) = error "all elems lit"
|
||||||
getLit (Img False s) = length $ S.elems s
|
getLit (Img False s) = length s
|
||||||
|
|
||||||
newDefault :: Algorithm -> Bool -> Bool
|
newDefault :: Algorithm -> Bool -> Bool
|
||||||
newDefault algo False = algo A.! 0
|
newDefault algo False = algo A.! 0
|
||||||
|
|
@ -32,17 +32,17 @@ next algo (Img def diff) = Img (newDefault algo def) newDiff where
|
||||||
neighs x y = map (\i -> ((x-1 + i `mod` 3, y-1 + i `div` 3), [i])) [0..8]
|
neighs x y = map (\i -> ((x-1 + i `mod` 3, y-1 + i `div` 3), [i])) [0..8]
|
||||||
calcNonDef = M.filter (/= newDefault algo def) $ M.map (\ns -> algo A.! binToDec (nonDefToNum ns)) nonDefault
|
calcNonDef = M.filter (/= newDefault algo def) $ M.map (\ns -> algo A.! binToDec (nonDefToNum ns)) nonDefault
|
||||||
nonDefToNum ns = map (\i -> if i `elem` ns then not def else def) [0..8]
|
nonDefToNum ns = map (\i -> if i `elem` ns then not def else def) [0..8]
|
||||||
newDiff = S.fromList $ M.keys calcNonDef
|
newDiff = M.keys calcNonDef
|
||||||
|
|
||||||
printImage :: Image -> IO ()
|
printImage :: Image -> IO ()
|
||||||
printImage img = do
|
printImage img = do
|
||||||
mapM_ (\y -> do
|
mapM_ (\y -> do
|
||||||
mapM_ (\x -> if (x,y) `S.member` getNd img then putChar '#' else putChar '.') [minx..maxx];
|
mapM_ (\x -> if (x,y) `elem` getNd img then putChar '#' else putChar '.') [minx..maxx];
|
||||||
putStrLn "") [miny..maxy] where
|
putStrLn "") [miny..maxy] where
|
||||||
minx = minimum $ map fst $ S.elems $ getNd img
|
minx = minimum $ map fst $ getNd img
|
||||||
maxx = maximum $ map fst $ S.elems $ getNd img
|
maxx = maximum $ map fst $ getNd img
|
||||||
miny = minimum $ map snd $ S.elems $ getNd img
|
miny = minimum $ map snd $ getNd img
|
||||||
maxy = maximum $ map snd $ S.elems $ getNd img
|
maxy = maximum $ map snd $ getNd img
|
||||||
|
|
||||||
|
|
||||||
main :: IO()
|
main :: IO()
|
||||||
|
|
@ -51,29 +51,16 @@ main = do
|
||||||
getLine
|
getLine
|
||||||
content <- lines <$> getContents
|
content <- lines <$> getContents
|
||||||
let nonDef = catMaybes $ concat $ zipWith (\j -> zipWith (\i e -> if e =='#' then Just (i,j) else Nothing) [0..]) [0..] content
|
let nonDef = catMaybes $ concat $ zipWith (\j -> zipWith (\i e -> if e =='#' then Just (i,j) else Nothing) [0..]) [0..] content
|
||||||
let init = Img False $ S.fromList nonDef
|
let init = Img False nonDef
|
||||||
|
|
||||||
mapM_ print content
|
|
||||||
putStr "initial: "
|
|
||||||
print init
|
|
||||||
printImage init
|
|
||||||
|
|
||||||
putStr "after 1: "
|
|
||||||
let first = next algorithm init
|
|
||||||
print first
|
|
||||||
printImage first
|
|
||||||
|
|
||||||
putStr "after 2: "
|
|
||||||
let second = next algorithm first
|
|
||||||
print second
|
|
||||||
printImage second
|
|
||||||
|
|
||||||
|
let enhanced = iterate (next algorithm) init
|
||||||
putStr "part 1: "
|
putStr "part 1: "
|
||||||
print $ getLit second
|
print $ getLit $ enhanced !! 2
|
||||||
|
|
||||||
let enhanced = iterate (next algorithm) init !! 50
|
-- let enhanced = iterate (next algorithm) init !! 50
|
||||||
putStr "part 2: "
|
putStr "part 2: "
|
||||||
print $ getLit enhanced
|
print $ getLit $ enhanced !! 50
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue