import Data.Array import Data.List import Debug.Trace (traceShow) main :: IO () main = do input <- readFile "input" let ls = lines input let map = makeMap ls let pos = findStart ls let path = walkGuard map pos (-1,0) [pos] -- print (nub path) print (length (nub path)) -- print (grid ! (1,7)) walkGuard map pos dir path = if (atEdge map pos) then path else let front = addPair pos dir in if (map ! front) then walkGuard map pos (rotRight dir) path else walkGuard map front dir (front:path) atEdge map pos = let ((miny,minx), (maxy,maxx)) = bounds map (y,x) = pos in x == minx || x == maxx || y == miny || y == maxy addPair (x,y) (dx,dy) = (x+dx,y+dy) rotRight (up,right) = (right,-up) makeMap :: [String] -> Array (Int, Int) Bool makeMap grid = array bounds elements where n = length grid m = length (head grid) bounds = ((0,0), (n-1,m-1)) elements = [((i,j), (grid!!i!!j) == '#') | i <- [0..n-1], j <- [0..m-1]] findStart grid = head [(i,j) | i <- [0..n-1], j <- [0..m-1], grid!!i!!j == '^'] where n = length grid m = length (head grid)