diff --git a/12-a.hs b/12-a.hs index 00e7885..8c55a5c 100644 --- a/12-a.hs +++ b/12-a.hs @@ -1,48 +1,17 @@ -import Prelude hiding (Left, Right) import AStar import Data.List (findIndex, elemIndex) import Data.Maybe (fromJust) import GHC.Utils.Misc (uncurry3) -import Data.Char () data Elevation = A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z deriving (Show, Eq, Enum, Bounded) -data Direction = Left | Down | Up | Right - deriving (Show, Eq) - -directionToChar Left = '<' -directionToChar Down = 'v' -directionToChar Up = '^' -directionToChar Right = '>' - type Pos = (Int, Int) type Grid a = [[a]] -directionTo :: Pos -> Pos -> Direction -directionTo (x1, y1) (x2, y2) - | x2 > x1 = Right - | x2 < x1 = Left - | y2 > y1 = Down - | y2 < y1 = Up - | otherwise = undefined - (!!!) :: Grid a -> Pos -> a (!!!) grid (x, y) = grid !! y !! x --- stolen from https://hackage.haskell.org/package/relude-1.1.0.0/docs/src/Relude.List.html#%21%21%3F --- (!!) but maybe monad (real) -infix 9 !!? -(!!?) :: [a] -> Int -> Maybe a -(!!?) xs i - | i < 0 = Nothing - | otherwise = go i xs - where - go :: Int -> [a] -> Maybe a - go 0 (x:_) = Just x - go j (_:ys) = go (j - 1) ys - go _ [] = Nothing - findPos :: (Eq a) => a -> Grid a -> Maybe Pos findPos target grid = do y <- findIndex (target `elem`) grid @@ -69,9 +38,6 @@ parseGrid g = (gridMap parseElevation gridChars, fromJust start, fromJust end) end = findPos 'E' gridChars gridChars = lines g -showGrid :: (Show a) => Grid a -> String -showGrid = unlines . map (concatMap show) - isValidTravel :: Grid Elevation -> Pos -> Pos -> Bool isValidTravel grid (x1, y1) (x2, y2) | x2 < 0 || x2 >= gridWidth grid || y2 < 0 || y2 >= gridHeight grid = False diff --git a/12-b.hs b/12-b.hs index 02ee549..1077c7e 100644 --- a/12-b.hs +++ b/12-b.hs @@ -1,6 +1,5 @@ {-# LANGUAGE TupleSections #-} -import Prelude hiding (Left, Right) import AStar import Data.List (findIndex, elemIndex) import Data.Maybe (fromJust, mapMaybe) @@ -9,41 +8,12 @@ import GHC.Utils.Misc (uncurry3) data Elevation = A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z deriving (Show, Eq, Enum, Bounded) -data Direction = Left | Down | Up | Right - deriving (Show, Eq) - -directionToChar Left = '<' -directionToChar Down = 'v' -directionToChar Up = '^' -directionToChar Right = '>' - type Pos = (Int, Int) type Grid a = [[a]] -directionTo :: Pos -> Pos -> Direction -directionTo (x1, y1) (x2, y2) - | x2 > x1 = Right - | x2 < x1 = Left - | y2 > y1 = Down - | y2 < y1 = Up - | otherwise = undefined - (!!!) :: Grid a -> Pos -> a (!!!) grid (x, y) = grid !! y !! x --- stolen from https://hackage.haskell.org/package/relude-1.1.0.0/docs/src/Relude.List.html#%21%21%3F --- (!!) but maybe monad (real) -infix 9 !!? -(!!?) :: [a] -> Int -> Maybe a -(!!?) xs i - | i < 0 = Nothing - | otherwise = go i xs - where - go :: Int -> [a] -> Maybe a - go 0 (x:_) = Just x - go j (_:ys) = go (j - 1) ys - go _ [] = Nothing - findPos :: (Eq a) => a -> Grid a -> Maybe Pos findPos target grid = do y <- findIndex (target `elem`) grid @@ -73,9 +43,6 @@ parseGrid g = (gridMap parseElevation gridChars, fromJust start, fromJust end) end = findPos 'E' gridChars gridChars = lines g -showGrid :: (Show a) => Grid a -> String -showGrid = unlines . map (concatMap show) - isValidTravel :: Grid Elevation -> Pos -> Pos -> Bool isValidTravel grid (x1, y1) (x2, y2) | x2 < 0 || x2 >= gridWidth grid || y2 < 0 || y2 >= gridHeight grid = False