-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part2.hs
45 lines (37 loc) · 1.85 KB
/
Part2.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
module Main where
import Text.Parsec
import Data.List (nub, sortBy)
main = do
txt <- readFile "input.txt"
let (Right inp) = parse parser "" txt
print $ product $ take 3 $ sortBy (flip compare) $ getLowPoints inp
getLowPoints xs = [getBasinSize (getFlowPoints xs x y w h p) | x <- [0..w-1], y <- [0..h-1],
let neighbours = getNeighbours x y w h,
let p = xs !! y !! x,
all (\(nx,ny) -> p < (xs !! ny !! nx)) neighbours ]
where w = length (head xs)
h = length xs
getNeighbours x y w h = [(x',y') | (i,j) <- [(-1,0),(0,-1),(1,0),(0,1)],
let x' = x+i,
let y' = y+j,
x' >= 0 && x' < w,
y' >= 0 && y' < h
]
getBasinSize = (+1) . length . nub . map fst
getFlowPoints :: [[Int]] -> Int -> Int -> Int -> Int -> Int -> [((Int,Int),Int)]
getFlowPoints xs x y w h currentHeight | currentHeight >= 8 = []
| otherwise = higherNeighbours ++ theirFlowPoints
where higherNeighbours = filter (\(_,n) -> n > currentHeight && n < 9) zipped
currentHeight = xs !! y !! x
neighbourCoords = getNeighbours x y w h
neighbours = map (\(i,j) -> xs !! j !! i) neighbourCoords
zipped = zip neighbourCoords neighbours
theirFlowPoints = concat $ zipWith (\(i,j) nHeight ->
if nHeight < 8 && nHeight > currentHeight
then getFlowPoints xs i j w h nHeight
else []) neighbourCoords neighbours
parser :: Parsec String () [[Int]]
parser =
many1 (read . (:[]) <$> digit) `sepEndBy1` newline
number :: Parsec String () Int
number = read <$> many1 digit