-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.hs
More file actions
24 lines (16 loc) · 800 Bytes
/
Copy pathday04.hs
File metadata and controls
24 lines (16 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env runghc
module Main where
import Data.Set qualified as S
type Grid = S.Set (Int, Int)
main = interact (unlines . sequence [part1, part2] . grid . lines)
part1, part2 :: Grid -> String
part1 = ("Part 1: " ++) . show . S.size . runForklift
part2 = ("Part 2: " ++) . show . countRemoved
countRemoved :: Grid -> Int
countRemoved grid = case runForklift grid of
removed | null removed -> 0
removed -> S.size removed + countRemoved (grid S.\\ removed)
runForklift :: Grid -> Grid
runForklift grid = S.filter (\roll -> (< 4) . S.size $ grid `S.intersection` adjacent roll) grid
adjacent p@(x, y) = S.fromList [a | dx <- [-1 .. 1], dy <- [-1 .. 1], let a = (x + dx, y + dy), a /= p]
grid rows = S.fromList [(x, y) | (y, cols) <- zip [0 ..] rows, (x, v) <- zip [0 ..] cols, v == '@']