-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.hs
67 lines (54 loc) · 1.9 KB
/
day10.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Data.List
import Data.Map qualified as M
import Data.Maybe (fromJust)
data Direction = North | South | West | East
deriving (Show, Eq)
data Tile = NS | WE | NE | NW | SW | SE | Ground | Start
deriving (Show, Eq)
parseTile :: Char -> Tile
parseTile c = case c of
'|' -> NS
'-' -> WE
'L' -> NE
'J' -> NW
'7' -> SW
'F' -> SE
'.' -> Ground
'S' -> Start
other -> error ("Unknown tile type: " ++ show other)
newtype Maze = Maze {tiles :: M.Map (Int, Int) Tile}
deriving (Show)
parseMaze :: [String] -> Maze
parseMaze inputs = Maze (parseMazeHelper M.empty inputs 0)
where
parseMazeHelper :: M.Map (Int, Int) Tile -> [String] -> Int -> M.Map (Int, Int) Tile
parseMazeHelper m [] _ = m
parseMazeHelper m (curLine : rest) rowIndex = M.union mergedTiles (parseMazeHelper mergedTiles rest (rowIndex + 1))
where
mergedTiles = M.union m (M.fromList (zip (zip (replicate (length curLine) rowIndex) [0 .. length curLine - 1]) (map parseTile curLine)))
findStart :: Maze -> (Int, Int)
findStart m = fst (head (M.toList (M.filterWithKey (\_ tile -> tile == Start) (tiles m))))
validDirections :: Maze -> (Int, Int) -> [Direction]
validDirections (Maze tiles) pos = case fromJust (M.lookup pos tiles) of
NS -> [North, South]
WE -> [West, East]
NE -> [North, East]
NW -> [North, West]
SW -> [South, West]
SE -> [South, East]
_ -> error "Invalid directions for position"
tracePosition :: Maze -> (Int, Int) -> [[(Int, Int)]]
tracePosition = undefined
where
isStart :: Maze -> (Int, Int) -> Bool
isStart (Maze t) pos = fromJust (M.lookup pos t) == Start
-- `traceMaze` follows the path in the maze and returns a list of all the
-- positions and their distances from the start
traceMaze :: Maze -> [(Int, (Int, Int))]
traceMaze = undefined
main :: IO ()
main = do
input <- fmap lines (readFile "input")
let maze = parseMaze input
print (findStart maze)
print maze