summaryrefslogtreecommitdiff
path: root/2019/24.hs
blob: 52fbc05e33583a8f6902921965e3b08640d3b085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{-# LANGUAGE TupleSections #-}
module Main where

import Control.Monad
import Data.List
import qualified Data.Set as Set

import Input


data World = World [[Bool]]
  deriving (Show, Eq, Ord)

showWorld :: World -> String
showWorld (World bds) = unlines (map showRow [0..4])
  where
    showRow y = intercalate "  " (map (showBdRow y) bds)
    showBdRow y bd = [".#" !! fromEnum (bd !! (5 * y + x)) | x <- [0..4]]

evolve1 :: World -> World
evolve1 (World [bd]) =
    World [[let s = sum [fromEnum (bd !! n) | n <- neighbours idx]
            in s == 1 || (not cell && s == 2)
           | (idx, cell) <- zip [0..] bd]]
  where
    neighbours idx =
        let (y, x) = idx `divMod` 5
        in concat [guard (x > 0) >> [idx-1]
                  ,guard (y > 0) >> [idx-5]
                  ,guard (x < 4) >> [idx+1]
                  ,guard (y < 4) >> [idx+5]]
evolve1 _ = undefined

findWorldReappear1 :: World -> World
findWorldReappear1 initWorld = go initWorld Set.empty
  where
    go w seen | w `Set.member` seen = w
              | otherwise = go (evolve1 w) (Set.insert w seen)

biodiversity1 :: World -> Int
biodiversity1 (World [bd]) = sum [fromEnum c * 2 ^ i | (c, i) <- zip bd [0::Int ..]]
biodiversity1 _ = undefined

numBugs2 :: World -> Int
numBugs2 (World bds) = sum (map fromEnum (concat bds))

evolve2 :: World -> World
evolve2 (World bds) = World newbds
  where
    srcbds = (if all not (head bds) then [] else [emptybd]) ++
             bds ++
             (if all not (last bds) then [] else [emptybd])
    lowestLvlSrc = if all not (head bds) then 0 else -1
    newbds = [[let s = sum [fromEnum (deref n) | n <- neighbours (lvl, idx)]
               in s == 1 || (not cell && s == 2)
              | (idx, cell) <- zip [0..] bd]
             | (lvl, bd) <- zip [lowestLvlSrc..] srcbds]

    emptybd = replicate 25 False

    numlevels = length bds

    deref (lvl, idx) | lvl < 0          = False
                     | lvl >= numlevels = False
                     | otherwise        = bds !! lvl !! idx

    neighbours (lvl, idx) = table !! idx
      where
        table = [[(lvl-1, 11), (lvl-1, 7), (lvl, 1), (lvl, 5)]
                ,[(lvl, 0), (lvl-1, 7), (lvl, 2), (lvl, 6)]
                ,[(lvl, 1), (lvl-1, 7), (lvl, 3), (lvl, 7)]
                ,[(lvl, 2), (lvl-1, 7), (lvl, 4), (lvl, 8)]
                ,[(lvl, 3), (lvl-1, 7), (lvl-1, 13), (lvl, 9)]

                ,[(lvl-1, 11), (lvl, 0), (lvl, 6), (lvl, 10)]
                ,[(lvl, 5), (lvl, 1), (lvl, 7), (lvl, 11)]
                ,[(lvl, 6), (lvl, 2), (lvl, 8)] ++ map (lvl+1,) [0..4]
                ,map (lvl,) [3,7,9,13]
                ,(lvl-1, 13) : map (lvl,) [8,4,14]

                ,(lvl-1,11) : map (lvl,) [5,11,15]
                ,map (lvl,) [10,6,16] ++ map (lvl+1,) [0,5..20]
                ,[]
                ,map (lvl,) [8,14,18] ++ map (lvl+1,) [4,9..24]
                ,(lvl-1, 13) : map (lvl,) [9,13,19]

                ,(lvl-1, 11) : map (lvl,) [10,16,20]
                ,map (lvl,) [15,11,17,21]
                ,map (lvl,) [16,18,22] ++ map (lvl+1,) [20..24]
                ,map (lvl,) [17,13,19,23]
                ,(lvl-1, 13) : map (lvl,) [18,14,24]

                ,[(lvl, 15), (lvl, 21), (lvl-1, 11), (lvl-1, 17)]
                ,(lvl-1, 17) : map (lvl,) [20, 16, 22]
                ,(lvl-1, 17) : map (lvl,) [21, 17, 23]
                ,(lvl-1, 17) : map (lvl,) [22, 18, 24]
                ,[(lvl, 23), (lvl, 19), (lvl-1, 17), (lvl-1, 13)]]

main :: IO ()
main = do
    stringbd <- getInput 24
    let initWorld = World [[stringbd !! (idx `div` 5) !! (idx `mod` 5) == '#' | idx <- [0..24]]]
    print (biodiversity1 (findWorldReappear1 initWorld))

    print (numBugs2 (iterate evolve2 initWorld !! 200))