day20
This commit is contained in:
parent
d2d20c8df5
commit
7e38572684
|
|
@ -0,0 +1,80 @@
|
||||||
|
module Day20 where
|
||||||
|
import qualified Data.Array as A
|
||||||
|
import qualified Data.Set as S
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Debug.Trace (traceShow, trace)
|
||||||
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
|
type Algorithm = A.Array Int Bool
|
||||||
|
data Image = Img Bool (S.Set (Int,Int)) deriving (Show)
|
||||||
|
|
||||||
|
getNd :: Image -> S.Set (Int,Int)
|
||||||
|
getNd (Img _ s) = s
|
||||||
|
|
||||||
|
getLit :: Image -> Int
|
||||||
|
getLit (Img True s) = error "all elems lit"
|
||||||
|
getLit (Img False s) = length $ S.elems s
|
||||||
|
|
||||||
|
newDefault :: Algorithm -> Bool -> Bool
|
||||||
|
newDefault algo False = algo A.! 0
|
||||||
|
newDefault algo True = algo A.! 511
|
||||||
|
|
||||||
|
insertManyWith :: Ord k => (a -> a -> a) -> [(k,a)] -> M.Map k a -> M.Map k a
|
||||||
|
insertManyWith f [] = id
|
||||||
|
insertManyWith f ((k,v):xs) = insertManyWith f xs . M.insertWith f k v
|
||||||
|
|
||||||
|
binToDec :: [Bool] -> Int
|
||||||
|
binToDec = foldr (\x acc -> acc * 2 + fromEnum x) 0
|
||||||
|
|
||||||
|
next :: Algorithm -> Image -> Image
|
||||||
|
next algo (Img def diff) = Img (newDefault algo def) newDiff where
|
||||||
|
nonDefault = foldr (\(x,y) -> insertManyWith (++) (neighs x y)) M.empty diff
|
||||||
|
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
|
||||||
|
nonDefToNum ns = map (\i -> if i `elem` ns then not def else def) [0..8]
|
||||||
|
newDiff = S.fromList $ M.keys calcNonDef
|
||||||
|
|
||||||
|
printImage :: Image -> IO ()
|
||||||
|
printImage img = do
|
||||||
|
mapM_ (\y -> do
|
||||||
|
mapM_ (\x -> if (x,y) `S.member` getNd img then putChar '#' else putChar '.') [minx..maxx];
|
||||||
|
putStrLn "") [miny..maxy] where
|
||||||
|
minx = minimum $ map fst $ S.elems $ getNd img
|
||||||
|
maxx = maximum $ map fst $ S.elems $ getNd img
|
||||||
|
miny = minimum $ map snd $ S.elems $ getNd img
|
||||||
|
maxy = maximum $ map snd $ S.elems $ getNd img
|
||||||
|
|
||||||
|
|
||||||
|
main :: IO()
|
||||||
|
main = do
|
||||||
|
algorithm <- A.listArray (0,511) . map (=='#') <$> getLine
|
||||||
|
getLine
|
||||||
|
content <- lines <$> getContents
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
putStr "part 1: "
|
||||||
|
print $ getLit second
|
||||||
|
|
||||||
|
let enhanced = iterate (next algorithm) init !! 50
|
||||||
|
putStr "part 2: "
|
||||||
|
print $ getLit enhanced
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print "bye"
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
#..###.##....#.#.#...#.#.#...##...####......##.##..###...#.####..#..#..#####..#.##.....#..#.###.##...#.#.....#...##.##.##...#####.#.#.#.##.###.#.##..#.##.##.#..#...####.#.#.....#..#.....###.#..#.#.#.#...#.###..#.###..##.#..#...##...####.#.........###..#.##.#..#.#...##.#.#.##.####.###....#####..###...##..#####..###..##..#.#.#..###.##.###..#.#######.####..#....###.##...#.####..#.#######...###...##.##.###...##..#.....#.###....#..#.#..###.#...#######.#...##..#.#..##.#...##.#..##.##..#...#.#.##.####........#..#.
|
||||||
|
|
||||||
|
#.###...#..#...###..#..#.#....#####.#...#..#####...#.##...#....#.#.#..#.#.........#..##..#..#.#..#..
|
||||||
|
###.#..##.#.##.#......#.###.##..###.#..#.###..####.#...#.....#...#.#.#..#######..#.#.##.#.#####..###
|
||||||
|
.....##...#..#.#..#....###.##..######.#..#.###.###.###.#.#.##.####.#..#.#....##..#.##.#.........#.#.
|
||||||
|
.#####...##...###..####.##.##.#..###.#.#..##..###...##.#..#...###.###.#.##.#.#....##.#.##.#.#..##.#.
|
||||||
|
..#.....#####.#.###.###..#..##.##...#######.#...#...#.##.#.###....#.....####..#.....##.#####.###..##
|
||||||
|
.###.#####.#..#...#.#....####.#..###.##...##..#..##..#........###..#..###....###.#.#..##..#...#.#..#
|
||||||
|
##.##.#.##.#...###.###.###....#...##.#.#..#.##...#..#.#.#..####.#.###....#.##.#.##.#..#.#.....###.#.
|
||||||
|
..#..#.###..##..#####.##.#.#..#..##...##..#..##..##....###..#.###..##..#.#..#..#..#.##.#..##.##.....
|
||||||
|
###.#.#.#...#.#...#.###..#.###....##..#.##.###.....###.#..#..#####.##.#.##..#..##..###.###..###....#
|
||||||
|
###......##.##..#.......####...##.#......#.#.....#..#.#.###........##.#.####.#.#.##...#..###.#.#....
|
||||||
|
.##.##...#.#..#...#####.#####..#.......##..##.#.....####.###.#..#...##.#.###.#..##.##..####..#..##.#
|
||||||
|
.#...#.#.#.#....###......##.....#...##...###...##....#..##.#..##.#.##.#...##.#.#.......##.#....#.#.#
|
||||||
|
#..#....####...#.#.#.####...###.###.######..#.#...##.#...#.##......#.#...#.#.###.#.#.#..####..#.#...
|
||||||
|
..#..##....##.##...###..###.#...#.#...#.##..#..#.#.####..#...#..#.#.##....#....#.##..#.#..#.#....#.#
|
||||||
|
.#.#.########.##.#....##.#..###....#.#.###.#...##....###......##########.#..#.#.####...#..##...#..##
|
||||||
|
#..##..#..#.#.#####..#####...#.###..####..##.#.#.####.#..#.#..####......#..#..##....##...#..###.#.##
|
||||||
|
..###.#.#.##.##.#.#...###.#..#....####..###.##..##.#..##....###.#####.#..#...###.#.#..##...#...#####
|
||||||
|
#.##.##.#..##.###.#.#.##..#...#...###..#..##.#..##....#####...#.#.#.##...####.##....#.#.##.#.##.#...
|
||||||
|
##...##.#.###.####.##......##...#..#..#.....##..##..#.###.#..##.#.##.##..#..####.#########...##..#..
|
||||||
|
.###..#.#..##..##..#..#...#.#..###.....#.#####..##.####...#.#...######.........####...........#..#.#
|
||||||
|
#..#...##.#.##...##..##..........#..#...#..#..#.#..#.........######..####.#.##...##.##.##.....##..##
|
||||||
|
#..##.#.###..##..####.#..######....#....####...##.#.#.#.#..#.##..#..#..###..#.#.#.#.#....#..##.#.#..
|
||||||
|
.....##.......#.#.##.......##.##.#..###...###....##.###....##.#.####.###.##..##.#..##...###........#
|
||||||
|
##..##....#.###..###.##.....##.#...##....##..#....#....###...#..#...###..#.####....#.......##.....##
|
||||||
|
#..#..#.#..#...##.#.##.##...#.#.##..##..#.##..#.#...#...#.###.####..#....#####...#.###.##.####..#.##
|
||||||
|
..##..##....####..#...###.############.##.#.##.####.#..#.##.#..##....#####..#.#####...###.#..#.##...
|
||||||
|
...#.##.#.###.....###.#.#...##.#.#.##.##.#..##.#.##.#.##.#..#.###...#####.#..#.#..##...#.####..##..#
|
||||||
|
#.#.###.####..#.#.###...##...###..#......#..##.....###.....#.##...##.#..#.#.####.#..#.#.#.##.####.##
|
||||||
|
.###...####..##..#..###.#.#.#.##..##.##.#..#..##.##.#..#....#.#.###...#...#....##..##.#.##.######.##
|
||||||
|
###.##....##.#.####..#########.....#.###...######.#.#....#...##...####.##.#.#.#####.....##....#.#.##
|
||||||
|
######.#..#......#......###.##.#.###.#####..#.###.......####.#.###.#.#.##.##.....####...#..#.#.#####
|
||||||
|
....##.#.#.#.##..##.###..#.###.##.##..###..#..#####.#####........#..##.#..##........#.##..#..#..#...
|
||||||
|
.###..###.##.###.#.##.####.##.#.#...####.#..#.######.#.#####.#......#####.##..##.#.#.#.##.#..###.#.#
|
||||||
|
#.##....###...#.....#..##.##.##.#..#.#..#.#.#...##..####.#..#.......####.###.#.####..#.#..##..##.#..
|
||||||
|
##.#########..#....#....##...##.###..##.......####...####.#..#...###..##..##.#.#####..##.#....#..###
|
||||||
|
..#..#####..####.####...#..#.#...####...#..###..#...#...########..#.#.#..###....####.....###.##...#.
|
||||||
|
##.###.##..###.#.#..#..#.####...#.#...###..##...#.###.....##.#.#.#.##.#.#...###..###.###..#.#..#.#.#
|
||||||
|
#.##..#.##..##.##...#.##.....##.#..#..###.##..##.#.##..#####..#####.###.##..###...#.#.#.##.##...###.
|
||||||
|
..#.##.###.##.###.##.##.##......###..#.##.#..#...##.##.###..#..###.#......#.#..##..#.#..#####.......
|
||||||
|
..##.#.###.###.###.##..####.#..####.#.##.#..#.######.#..##.#.#.#...#######.##..###..####.##..#..###.
|
||||||
|
.####.##...#.#..#...##..#.#....#.#.##.##.#.#.#..##.#.#......####....###.####..#######.##.#....#.#.#.
|
||||||
|
###.#.##.......#...###...##.#.##.##...#..#.#..###.####.#.###.#.#.##.#.#.###....#....#.#....#..#.#..#
|
||||||
|
.#......#...#.#...#.##.#.###...#...##...#.###.#..#..#...#.#.#.###.##...###..##..###..#.#.##.#...####
|
||||||
|
##.##...##..##...##..#...###...###..##.##.#......#..###.....##..#.#.#####...#######.#.####.#........
|
||||||
|
#..#.....##..##.#.##..#.####......###..#...#.###.######..#.###..#.##..#.#....###..#..########..#.#..
|
||||||
|
##.#..#......###.#.##....#.#...##...##.#..##......###..####.###.###.#.....#...##....#####.###..##..#
|
||||||
|
###.......#.#.##....##..#.#..####.#.#..#.#..#...###.#.#.###.#####...####.#..#.##..#.....#.#.....#...
|
||||||
|
.##.#..###....#.#####..###...##.#...####.###...#.######.#..###...#...#######.#.##.#....##.#.##...###
|
||||||
|
#....#..#.#.#####..###.##..#.####..##.#.#....#.#.##.....##...#..#.###.#...#....#..#....###.#..#.###.
|
||||||
|
#..#.#######.##..#.#..#.##..#######..####....#.#.###..###......###.####.#.....##..#.##.#.....###..##
|
||||||
|
.###.#..##.......###..#.##....#......#.##..#.##..##.####.....###.#...#.#..##....#.#.###.###.#.#.##.#
|
||||||
|
...#####.#.###....#..#.....###.#.....#..#..#..###...###.###..#....#####.#.###..#.....#..######....#.
|
||||||
|
#..####...#...##..##.####..###.....##.###..#.###.#.#.##..#..#####...##.#...##.#..##.##.##.#....#....
|
||||||
|
#...#.####......#.##..##.#.##.#.#####..#######.##.##.#.#.......#.#.#...##.####.###..#..#....#####.##
|
||||||
|
..##...###...##.#..##..#...##.#.......#..#...#.#....#.###..##.#.###..##.#..####.######..#..#.##.#..#
|
||||||
|
.##.....#.##..#..##......###.###.#......####..####.#.#.#...#..#..#..###..#..#...#.####.#.##..###.###
|
||||||
|
........###.####...#...##..##..#.###..##..##.#...#..##.##....###.##.#.#..###.#.####....##..#.####.##
|
||||||
|
.#..#...###.##.#....#.#.##...#..##..#.##...#...#.######.##.##.#####....##.###..####.....#.#..#...#.#
|
||||||
|
#.#....##....#.######.##..#...##.##.#..#..#.#...#.##..##....###.#.#.#.#.#.######...##..###.#.##...#.
|
||||||
|
..#....####.#.#############.####...#####......####.##.##.##..#..##.#....####..####.#..####.#.#..#.##
|
||||||
|
#.##.#..#..####...#######....#...####.#..####....#####..##..#######..#.#....##.......#.#..#..####.#.
|
||||||
|
#.#..#.#.#...####..#.....####.#...###...###.#.##.##.#.##.#........#.##..##.###.##.......#.###.....##
|
||||||
|
..#.#.###.#.###.#.....###.#...##...#..#.....###.#..#..#..#...##..#..#.##..#..#.#.##..##.....#...##..
|
||||||
|
..##.###.##.##..##.##.####..##..#.#.##.##.#...##...##....###..##...#..###.##.##.....###.#.##.##.#.##
|
||||||
|
#.####...##.###..####...#.##.#####.#....#.#..#.....#......##..##.######...#....####...#..#.#..#..#.#
|
||||||
|
.###..#.#.##.#.....###.#..#..#..#...####.#.####.#...###.##....###..##.##..#.##.#...#.#.#...####..##.
|
||||||
|
##..##..####.####.#.##.#.#..####..##.######.###...##.##..#.#####.###..##...#.....##.##..#..###..####
|
||||||
|
#..##.####.#.#.#####.....##.####..##..###...##...##...##....#.#.#...######...#...........##..###.##.
|
||||||
|
...###.##...#.#.##...####...#..#.######.####.##.##.#....#.#.###..#.##...###.#...##.....##...###..##.
|
||||||
|
.....#.###.##.....##.###.#.#..###..##..#..#.#.###..####......#.#####..##.####..###..##.#######.##.##
|
||||||
|
#...#..#...##..#...####.##...#.#..##.......#.#...#.##.####.....#########...##....####.......#.###.##
|
||||||
|
.#.....#.....####...#####..###.#.........##..##.##.#...#.#..#.#.#.#.#.###.#####..####..#......##.#.#
|
||||||
|
#..#..###.#..#######.#..###.##..#..##.#..#..#.##.###.#..###.###..........#.###.#####.#.##.#.#...##..
|
||||||
|
.##..###.#.#####.##.####.#.#..#..###....##.#.#..###.#.##..#.#..###..##.#.##.....#.#.#.#.#.####.#.##.
|
||||||
|
..##.###...#....##.##.#####.##....#..##.######....###..#.#.##.###.#.......######.#..#..#..###.##....
|
||||||
|
#.#..#.##.###...#..#####.##.#.###.#.###.......#######....#.####...####.##..#.#.###.##..#..#..###..#.
|
||||||
|
#......#.##.##....##.#..#.#....##..#.###.....#.##...#.#..#...##.####......##..#.#.#...#...#.....#.##
|
||||||
|
#....##.#.##.#.......#.##.#.###..#...###.#.###.##......#..#####..##.#..#....######.##.#..#...##..##.
|
||||||
|
######.#.....#.#..##.##.....##.#..####.###..#.#.###....#..###..#..##.#..#.###..#.##...####....#.#...
|
||||||
|
#.#..###..#####..#....#.#...##....###..##....#.##.#......#.#.###.....#.#.#..#.#..###.#....####......
|
||||||
|
#..#.###.###.#.#.#..#.##..#.#.###.#...........#.#...#.##..#....##..#.#.##..##.#...##..#.##.##.#..#.#
|
||||||
|
#.....#..#.#..##.##.######.#######....###.###...#####..#.....#..##.#.##.#.###...#..##.....###...#...
|
||||||
|
#####.#.#..#.#.##..#.####.#...###.##.#....##.##.####.#...####.##.#...####..#..#.####.###....###.....
|
||||||
|
#...#.......##.#...#..####...#.#..#.##.###..#.#.#.##...#.###......##....########..##.####.##.####.##
|
||||||
|
#..#...##.###....##.##.#.##.##..#.#.#...###..#.#........###########.##..###.###.#..#....#.###....##.
|
||||||
|
.#.#..##..##.##.#....##....###.##..###..#.###.#.###.#.###..######..###...####.....#.#...#.#.#..##.#.
|
||||||
|
##...#..######.#.#...#.##....#...#..#..#.##.######.#..#..#.#...##..#..#...###..###.#.#......#....#.#
|
||||||
|
.#.......#...#..#####.#####.#########..#......#..#.#.##.#.#.##.#.#.........####.##.#.###...#.##..#..
|
||||||
|
..###.#.....###..#..#.#..#.#..####..###.....#.####.##...#.#..#.#.#.#.#.#..###..#......#..#####.#.#..
|
||||||
|
#...#....###..####...#.##.#......###.#.##.###.#..###..##....##....#..#.....#...###.##.#......#..####
|
||||||
|
#.####....######.####.#.####..#.#..#..#...####..#.#####..#..##.##..#.##...###...###.#.#..###.#.##.#.
|
||||||
|
.####.###.#.##...###........#...##.#.#.######..#...##.###.##..#.#.######.#####..###....##...##.##...
|
||||||
|
#.###.#.###..#.#.##..####.##..#...###...####..#..####.#.######.#####..#.##.##.##.##..#.#.##..#.##...
|
||||||
|
.#.......#....##..#.#.##.#.#.##..###..#..##.....####.#...###....##.##......#....#.......#...##.##...
|
||||||
|
.####.###..####.#.##..#.#.#..#...#..#.##.###..######.##.##.#.####.##.##.#..#..##..######.####..#..##
|
||||||
|
#.###.##..#.#......##.#...#..#..#.##.#....##.....#......#..#...#..##....#.#####..####.#.#..#..##.##.
|
||||||
|
#.#....#.#.#....#.#####.......#..#..#..#.##...#.#########..##..##...####.##..##.##.##..#.##...#.#.##
|
||||||
|
.####....#..#.####.###.#.##########.###.#...#..##.##.#.####.##.###.##.#..#..#.##.##....###.#...##.#.
|
||||||
|
...####.#.......#......#.######.#.##...###....####.##...##...##.#..#.##..##......#...####.#..##...##
|
||||||
|
.#####...#....#....##.#.####....###..#.##...#.##..##..###.#.##.##.##...#.###..###.###...###...##.#..
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
|
||||||
|
|
||||||
|
#..#.
|
||||||
|
#....
|
||||||
|
##..#
|
||||||
|
..#..
|
||||||
|
..###
|
||||||
Loading…
Reference in New Issue