From 9e104a7ffbd3b6e57b98b9103a4b532802e47d52 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 9 Dec 2021 16:18:30 +0000 Subject: [PATCH] Tidying, added blog link --- advent-of-code21.cabal | 2 +- advent09/Main.hs | 32 ++++++++++---------------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index 9e50851..d7c0fa8 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -128,4 +128,4 @@ executable advent08a executable advent09 import: common-extensions, build-directives main-is: advent09/Main.hs - build-depends: array, containers + build-depends: array, containers, linear diff --git a/advent09/Main.hs b/advent09/Main.hs index 4cd269e..a5c4ccc 100644 --- a/advent09/Main.hs +++ b/advent09/Main.hs @@ -1,30 +1,26 @@ --- 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 @@ -70,16 +66,8 @@ breadthFirstSearch grid agenda basin 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 + -- 2.34.1