-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
63 lines (52 loc) · 2.21 KB
/
run.hs
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
{-# LANGUAGE OverloadedLists #-}
import AoC
import Data.List (intercalate)
import Data.Maybe (mapMaybe)
import Data.Vector ((!), (!?))
import qualified Data.Vector as V
type Grid = V.Vector (V.Vector Bool)
printGrid = intercalate "\n" . map (map (\x -> if x then '#' else '.')) . map V.toList . V.toList
readGrid :: String -> Grid
readGrid = V.fromList . map (V.fromList . line) . lines
where line = map (\x -> case x of
'#' -> True
'.' -> False
)
neighbours :: Grid -> Int -> Int -> [Bool]
neighbours grid x y = mapMaybe (\(x', y') -> (grid !? y') >>= (!? x'))
[(x', y') | x' <- [x-1, x, x+1]
, y' <- [y-1, y, y+1]
, (x', y') /= (x, y)]
step :: Grid -> Grid
step grid = V.map (\y -> V.map (\x -> f x y) [0..s-1]) [0..s-1]
where s = V.length grid
f x y = case ((grid ! y) ! x, length (filter id (neighbours grid x y))) of
(True, 2) -> True
(True, 3) -> True
(False, 3) -> True
_ -> False
turnOnCorners grid = V.map (\y -> V.map (\x -> f x y) [0..s-1]) [0..s-1]
where s = V.length grid
corners = [(0, 0), (0, s - 1), (s - 1, 0), (s - 1, s - 1)] :: [(Int, Int)]
f x y | elem (x, y) corners = True
| otherwise = (grid ! y) ! x
step' :: Grid -> Grid
step' grid = V.map (\y -> V.map (\x -> f x y) [0..s-1]) [0..s-1]
where s = V.length grid
corners = [(0, 0), (0, s - 1), (s - 1, 0), (s - 1, s - 1)] :: [(Int, Int)]
f x y
| elem (x, y) corners = True
| otherwise = case ((grid ! y) ! x, length (filter id (neighbours grid x y))) of
(True, 2) -> True
(True, 3) -> True
(False, 3) -> True
_ -> False
on :: Grid -> Int
on = V.sum . V.map (V.length . V.filter id)
part1 g = on $ iterateN 100 step g
part2 g = on $ iterateN 100 step' g'
where g' = turnOnCorners g
main = do
input <- readGrid <$> readFile "input.txt"
print (part1 input)
print (part2 input)