Skip to content

Instantly share code, notes, and snippets.

@mjgpy3
Created December 3, 2020 09:49
Show Gist options
  • Select an option

  • Save mjgpy3/38122a97b964d92ac633c2aba63e39f1 to your computer and use it in GitHub Desktop.

Select an option

Save mjgpy3/38122a97b964d92ac633c2aba63e39f1 to your computer and use it in GitHub Desktop.
aoc-2020-day3.hs
module Main where
import qualified Data.Map.Strict as M
import Data.List (unfoldr)
import Control.Monad (guard)
input = [
"....#...............#.#..###.##",
-- etc...
]
grid = M.fromList $ do
(y, row) <- zip [0..] input
(x, cell) <- zip [0..] row
pure ((x, y), cell)
height = length input
width = length $ head input
spots = unfoldr (stepAdjust (3, 1)) (0, 0)
where
stepAdjust (dx, dy) (x, y) = do
guard $ y < height
cell <- M.lookup (x, y) grid
pure (cell, ((x+dx) `mod` width, y+dy))
main = print $ length $ filter (== '#') spots
module Main where
import qualified Data.Map.Strict as M
import Data.List (unfoldr)
import Control.Monad (guard)
input = [
"....#...............#.#..###.##",
-- etc...
]
grid = M.fromList $ do
(y, row) <- zip [0..] input
(x, cell) <- zip [0..] row
pure ((x, y), cell)
height = length input
width = length $ head input
spotsDelta delta = unfoldr (stepAdjust delta) (0, 0)
where
stepAdjust (dx, dy) (x, y) = do
guard $ y < height
cell <- M.lookup (x, y) grid
pure (cell, ((x+dx) `mod` width, y+dy))
treesDelta = length . filter (== '#') . spotsDelta
-- I know, I know... `product` is not great, but it's over 5 elements!
main = print $ product $ map treesDelta [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment