-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.hs
56 lines (43 loc) · 1.15 KB
/
Day12.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
module Main where
import Advent.Grid (Grid, Position)
import Advent.Grid qualified as Grid
import Advent.Utils (run)
import Data.List (nub)
type Input = Grid Char
type Id = Char
type Area = Int
type Perimeter = Int
data Region = Region Id Area Perimeter
deriving (Eq, Show)
testInput :: Input
testInput =
prepare
[ "OOOOO"
, "OXOXO"
, "OOOOO"
, "OXOXO"
, "OOOOO"
]
fencePrice :: Region -> Int
fencePrice (Region _ x y) = x * y
uniqueRegions :: Input -> [Char]
uniqueRegions = nub . Grid.getAllVals
findRegion :: Input -> Char -> Region
findRegion grid c =
let
plots = Grid.findAll (== c) grid
in
Region c (length plots) (sum $ map (countPerimeterPlot grid c) plots)
countPerimeterPlot :: Input -> Char -> Position -> Int
countPerimeterPlot g target pos =
4 - length (filter (== target) neighbors)
where
neighbors = Grid.cardinalNeighborVals g pos
part1 :: Input -> Int
part1 grid = sum $ fencePrice . findRegion grid <$> uniqueRegions grid
part2 :: Input -> ()
part2 = const ()
prepare :: [String] -> Input
prepare = Grid.fromList
main :: IO ()
main = run part1 part2 prepare