-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path24b.hs
28 lines (20 loc) · 868 Bytes
/
24b.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
import AOC
main = interact $ f . parselist (many1 enump)
data Dir = E | SE | SW | W | NW | NE deriving (Show, Eq, Read, Ord, Bounded, Enum)
dirToCoord E = ( 1, 0)
dirToCoord W = (-1, 0)
dirToCoord NE = ( 1, 1)
dirToCoord NW = ( 0, 1)
dirToCoord SE = ( 0, -1)
dirToCoord SW = (-1, -1)
godir z d = z + dirToCoord d
nbs = map dirToCoord [minBound .. maxBound]
rule x ns = let n = count True ns in n == 2 || x && n == 1
coordsToMap margin zs = [[(x, y) `elem` zs | x <- [minx..maxx]] | y <- [miny..maxy]]
where
(minx, miny) = (minimum $ map fst zs, minimum $ map snd zs) - (margin, margin)
(maxx, maxy) = (maximum $ map fst zs, maximum $ map snd zs) + (margin, margin)
nIters = 100
f xs = count True $ elems $ mapnbsN nIters nbs rule False m
where
m = coordsToMap 0 $ map head $ filter (odd . length) $ group $ sort $ map (foldl' godir 0) xs