diff options
Diffstat (limited to 'day06/day06.hs')
-rw-r--r-- | day06/day06.hs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/day06/day06.hs b/day06/day06.hs new file mode 100644 index 0000000..fa022f7 --- /dev/null +++ b/day06/day06.hs @@ -0,0 +1,82 @@ +import Data.List +import Data.Maybe + +data Cell = Clr | Obs deriving (Show, Eq) +type Grid = [[Cell]] +type Coo = (Int, Int) +data Dir = U | R | D | L deriving (Show, Eq) +type Guard = (Coo, Dir) + +(!?) :: Grid -> Coo -> Maybe Cell +grid !? (row, col) + | row < 0 = Nothing + | row >= length grid = Nothing + | col < 0 = Nothing + | col >= (length $ head grid) = Nothing + | otherwise = Just (grid !! row !! col) + +findGuard :: Int -> [[Char]] -> Guard +findGuard r (h:t) + | c == Nothing = findGuard (r + 1) t + | otherwise = ((r, (fromJust c)), U) + where c = elemIndex '^' h + +consume :: [Char] -> (Grid, Guard) +consume file = + let toCell = \ln -> case ln of + '#' -> Obs + otherwise -> Clr + grid = map (map toCell) (lines file) + guard = findGuard 0 (lines file) in + (grid, guard) + +step :: Grid -> Guard -> Maybe Guard +step grid (coo@(r,c), U) + | cell == Nothing = Nothing + | cell == Just Obs = Just (coo, R) + | cell == Just Clr = Just (ncoo, U) + where + ncoo = (r - 1, c) + cell = grid !? ncoo +step grid (coo@(r,c), R) + | cell == Nothing = Nothing + | cell == Just Obs = Just (coo, D) + | cell == Just Clr = Just (ncoo, R) + where + ncoo = (r, c + 1) + cell = grid !? ncoo +step grid (coo@(r,c), D) + | cell == Nothing = Nothing + | cell == Just Obs = Just (coo, L) + | cell == Just Clr = Just (ncoo, D) + where + ncoo = (r + 1, c) + cell = grid !? ncoo +step grid (coo@(r,c), L) + | cell == Nothing = Nothing + | cell == Just Obs = Just (coo, U) + | cell == Just Clr = Just (ncoo, L) + where + ncoo = (r, c - 1) + cell = grid !? ncoo + +(?:) :: Eq a => a -> [a] -> [a] +el ?: arr + | el `elem` arr = arr + | otherwise = el:arr + +route :: Grid -> [Coo] -> Guard -> [Coo] +route grid seen guard@(coo, dir) + | nguard == Nothing = coo ?: seen + | otherwise = route grid (coo ?: seen) (fromJust nguard) + where + nguard = step grid guard + +part1 :: Grid -> Guard -> Int +part1 grid guard = length $ route grid [] guard + +main = do + file <- readFile "day06.input" + let (grid, guard) = consume file + putStrLn ("Part 1: " ++ (show $ part1 grid guard)) + --putStrLn ("Part 2: " ++ (show $ part2 lns)) |