this repo has no description
1module Lib
2 ( countSteps
3 , getAt
4 ) where
5
6import Data.List
7
8countSteps :: [String] -> Int
9countSteps grid = go (initX, 0) (0, 1) 0
10 where initX = getInitX grid
11 go :: (Int, Int) -> (Int, Int) -> Int -> Int
12 go c@(x, y) d@(mx, my) count
13 | charAt == '|' || charAt == '-' = go (x + mx, y + my) d (count + 1)
14 | charAt `elem` ['A'..'Z'] = go (x + mx, y + my) d (count + 1)
15 | charAt == '+' = let newD@(mx', my') = getNewD grid c d
16 in go (x + mx', y + my') newD (count+1)
17 | otherwise = count
18 where charAt = getAt c grid
19
20getInitX :: [String] -> Int
21getInitX str = head $ elemIndices '|' $ head str
22
23getNewD :: [String] -> (Int, Int) -> (Int, Int) -> (Int, Int)
24getNewD grid (x, y) (mx, my)
25 | mx == 0 && getAt (x - 1, y) grid == '-' = (-1, 0)
26 | mx == 0 && getAt (x + 1, y) grid == '-' = (1, 0)
27 | my == 0 && getAt (x, y - 1) grid == '|' = (0, -1)
28 | my == 0 && getAt (x, y + 1) grid == '|' = (0, 1)
29 | otherwise = (mx, my)
30
31getAt :: (Int, Int) -> [String] -> Char
32getAt (x, y) grid = grid !! y !! x