day 12 dead code cleanup

This commit is contained in:
Jill 2022-12-12 18:23:24 +03:00
parent 9fe5b4e014
commit 299beb9c73
2 changed files with 0 additions and 67 deletions

34
12-a.hs
View File

@ -1,48 +1,17 @@
import Prelude hiding (Left, Right)
import AStar import AStar
import Data.List (findIndex, elemIndex) import Data.List (findIndex, elemIndex)
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import GHC.Utils.Misc (uncurry3) 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 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) 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 Pos = (Int, Int)
type Grid a = [[a]] 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 a -> Pos -> a
(!!!) grid (x, y) = grid !! y !! x (!!!) 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 :: (Eq a) => a -> Grid a -> Maybe Pos
findPos target grid = do findPos target grid = do
y <- findIndex (target `elem`) grid y <- findIndex (target `elem`) grid
@ -69,9 +38,6 @@ parseGrid g = (gridMap parseElevation gridChars, fromJust start, fromJust end)
end = findPos 'E' gridChars end = findPos 'E' gridChars
gridChars = lines g gridChars = lines g
showGrid :: (Show a) => Grid a -> String
showGrid = unlines . map (concatMap show)
isValidTravel :: Grid Elevation -> Pos -> Pos -> Bool isValidTravel :: Grid Elevation -> Pos -> Pos -> Bool
isValidTravel grid (x1, y1) (x2, y2) isValidTravel grid (x1, y1) (x2, y2)
| x2 < 0 || x2 >= gridWidth grid || y2 < 0 || y2 >= gridHeight grid = False | x2 < 0 || x2 >= gridWidth grid || y2 < 0 || y2 >= gridHeight grid = False

33
12-b.hs
View File

@ -1,6 +1,5 @@
{-# LANGUAGE TupleSections #-} {-# LANGUAGE TupleSections #-}
import Prelude hiding (Left, Right)
import AStar import AStar
import Data.List (findIndex, elemIndex) import Data.List (findIndex, elemIndex)
import Data.Maybe (fromJust, mapMaybe) 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 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) 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 Pos = (Int, Int)
type Grid a = [[a]] 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 a -> Pos -> a
(!!!) grid (x, y) = grid !! y !! x (!!!) 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 :: (Eq a) => a -> Grid a -> Maybe Pos
findPos target grid = do findPos target grid = do
y <- findIndex (target `elem`) grid y <- findIndex (target `elem`) grid
@ -73,9 +43,6 @@ parseGrid g = (gridMap parseElevation gridChars, fromJust start, fromJust end)
end = findPos 'E' gridChars end = findPos 'E' gridChars
gridChars = lines g gridChars = lines g
showGrid :: (Show a) => Grid a -> String
showGrid = unlines . map (concatMap show)
isValidTravel :: Grid Elevation -> Pos -> Pos -> Bool isValidTravel :: Grid Elevation -> Pos -> Pos -> Bool
isValidTravel grid (x1, y1) (x2, y2) isValidTravel grid (x1, y1) (x2, y2)
| x2 < 0 || x2 >= gridWidth grid || y2 < 0 || y2 >= gridHeight grid = False | x2 < 0 || x2 >= gridWidth grid || y2 < 0 || y2 >= gridHeight grid = False