day 10: remove directions
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/Day10.hs
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/Day10.hs
									
									
									
									
									
								
							@@ -8,25 +8,16 @@ type Coord = (Int, Int)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type Grid = M.Map (Int, Int) Int
 | 
					type Grid = M.Map (Int, Int) Int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
data Dir = North | South | East | West deriving (Show)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
next :: Coord -> Dir -> Coord
 | 
					 | 
				
			||||||
next (x, y) North = (x, y - 1)
 | 
					 | 
				
			||||||
next (x, y) South = (x, y + 1)
 | 
					 | 
				
			||||||
next (x, y) East = (x + 1, y)
 | 
					 | 
				
			||||||
next (x, y) West = (x - 1, y)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
part1 :: Grid -> Int
 | 
					part1 :: Grid -> Int
 | 
				
			||||||
part1 grid = sum . map (S.size . go 0 S.empty) . M.keys . M.filter (== 0) $ grid
 | 
					part1 grid = sum . map (S.size . go 0 S.empty) . M.keys . M.filter (== 0) $ grid
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
    go :: Int -> S.Set Coord -> Coord -> S.Set Coord
 | 
					    go :: Int -> S.Set Coord -> Coord -> S.Set Coord
 | 
				
			||||||
    go h s c
 | 
					    go h s c@(x, y)
 | 
				
			||||||
      | h == 9 = S.insert c s
 | 
					      | h == 9 = S.insert c s
 | 
				
			||||||
      | otherwise =
 | 
					      | otherwise =
 | 
				
			||||||
          S.unions
 | 
					          S.unions
 | 
				
			||||||
            [ go h' s n
 | 
					            [ go h' s n
 | 
				
			||||||
              | d <- [North, South, East, West],
 | 
					              | n <- [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)],
 | 
				
			||||||
                let n = next c d,
 | 
					 | 
				
			||||||
                Just h' <- [M.lookup n grid],
 | 
					                Just h' <- [M.lookup n grid],
 | 
				
			||||||
                h' == h + 1
 | 
					                h' == h + 1
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
@@ -35,13 +26,12 @@ part2 :: Grid -> Int
 | 
				
			|||||||
part2 grid = sum . map (go 0) . M.keys . M.filter (== 0) $ grid
 | 
					part2 grid = sum . map (go 0) . M.keys . M.filter (== 0) $ grid
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
    go :: Int -> Coord -> Int
 | 
					    go :: Int -> Coord -> Int
 | 
				
			||||||
    go h c
 | 
					    go h (x, y)
 | 
				
			||||||
      | h == 9 = 1
 | 
					      | h == 9 = 1
 | 
				
			||||||
      | otherwise =
 | 
					      | otherwise =
 | 
				
			||||||
          sum
 | 
					          sum
 | 
				
			||||||
            [ go h' n
 | 
					            [ go h' n
 | 
				
			||||||
              | d <- [North, South, East, West],
 | 
					              | n <- [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)],
 | 
				
			||||||
                let n = next c d,
 | 
					 | 
				
			||||||
                Just h' <- [M.lookup n grid],
 | 
					                Just h' <- [M.lookup n grid],
 | 
				
			||||||
                h' == h + 1
 | 
					                h' == h + 1
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user