projects
/
advent-of-code-21.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Done day 22
[advent-of-code-21.git]
/
advent09
/
Main.hs
diff --git
a/advent09/Main.hs
b/advent09/Main.hs
index 4cd269e3f42cce4cbfad67d0b33ff320e4379587..df5b1be8e413054286c52e43ce7928209f59d0d7 100644
(file)
--- a/
advent09/Main.hs
+++ b/
advent09/Main.hs
@@
-1,30
+1,25
@@
--- 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.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 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 =
type Grid = Array Coord Int
type Basin = S.Set Coord
main :: IO ()
main =
- do text <- readFile "data/advent09.txt"
+ do text <- readFile "data/advent09
a
.txt"
let grid = mkGrid text
let grid = mkGrid text
- print $ bounds grid
print $ part1 grid
print $ part1 grid
- -- print $ lowPoints grid
- -- print $ breadthFirstSearch grid (S.singleton (4, 6)) S.empty
print $ part2 grid
mkGrid :: String -> Grid
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
where rows = lines text
r = length rows - 1
c = (length $ head rows) - 1
@@
-68,18
+63,8
@@
breadthFirstSearch grid agenda basin
else S.insert here basin
agenda' = S.union candidates $ S.delete here agenda
else S.insert here basin
agenda' = S.union candidates $ S.delete here agenda
-
neighbours :: Grid -> Coord -> [Coord]
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