--- Writeup at https://work.njae.me.uk/2021/12/09/advent-of-code-2021-day-8/
-
+-- Writeup at https://work.njae.me.uk/2021/12/09/advent-of-code-2021-day-9/
import Data.Array
import Data.Char
--- import Data.Maybe
-import Data.List hiding ((\\))
+import Data.List (sort)
import qualified Data.Set as S
import Data.Set ((\\))
+import Linear (V2(..), (^+^))
-
-type Coord = (Int, Int) -- (row, column)
+type Coord = V2 Int
type Grid = Array Coord Int
type Basin = S.Set Coord
main :: IO ()
main =
- do text <- readFile "data/advent09.txt"
+ do text <- readFile "data/advent09a.txt"
let grid = mkGrid text
print $ bounds grid
print $ part1 grid
- -- print $ lowPoints grid
- -- print $ breadthFirstSearch grid (S.singleton (4, 6)) S.empty
print $ part2 grid
mkGrid :: String -> Grid
-mkGrid text = listArray ((0, 0), (r, c)) $ map digitToInt $ concat rows
+mkGrid text = listArray ((V2 0 0), (V2 r c)) $ map digitToInt $ concat rows
where rows = lines text
r = length rows - 1
c = (length $ head rows) - 1
neighbours :: Grid -> Coord -> [Coord]
-neighbours grid (r, c) = filter (gValid grid)
- [ (r + dr, c + dc)
- | (dr, dc) <- [(-1, 0), (1, 0), (0, -1), (0, 1)]
+neighbours grid here = filter (inRange (bounds grid))
+ [ here ^+^ delta
+ | delta <- [V2 -1 0, V2 1 0, V2 0 -1, V2 0 1]
]
-
-gValid :: Grid -> Coord -> Bool
-gValid grid (r, c)
- | r < minR = False
- | c < minC = False
- | r > maxR = False
- | c > maxC = False
- | otherwise = True
- where ((minR, minC), (maxR, maxC)) = bounds grid
+