diff --git a/AdventOfCode2020.cabal b/AdventOfCode2020.cabal index a70ba56..6224eab 100644 --- a/AdventOfCode2020.cabal +++ b/AdventOfCode2020.cabal @@ -17,13 +17,14 @@ maintainer: gael@depreeuw.dev extra-source-files: CHANGELOG.md, README.md library - exposed-modules: Day1, Day2 + exposed-modules: Day1, Day2, Day3 -- other-modules: -- other-extensions: build-depends: base ^>=4.13.0.0 , containers ^>=0.6.4.1 , text ^>=1.2.4.0 , attoparsec ^>=0.13.2.4 + , vector ^>=0.12.1.2 hs-source-dirs: src default-language: Haskell2010 diff --git a/Main.hs b/Main.hs index f3c0678..01c5606 100644 --- a/Main.hs +++ b/Main.hs @@ -2,8 +2,10 @@ module Main where import Day1 (day1) import Day2 (day2) +import Day3 (day3) main :: IO () main = do day1 day2 + day3 diff --git a/input/day3 b/input/day3 new file mode 100644 index 0000000..51b90f4 --- /dev/null +++ b/input/day3 @@ -0,0 +1,323 @@ +.##.#.........#.....#....#...#. +.#.#.#...#.......#............. +......#..#....#.#...###.......# +.......###......#.....#..##..#. +..#...##.......#.......###..... +....###.#....###......#....#..# +......#..#....#...##........... +..#..#....#...#.....####....... +...#........#.#.......#..#...#. +......#...#........#...#..##... +#..#........#............#...## +..#..#.#....#...........#...### +#.#..#...........#.##.#.#....#. +.#.#....#...##.....#........... +.....##....#...#..............# +...#....#...#.#.#.#...#........ +#....#....#.#.#..#....#..#..#.. +.................#..#.....#.... +#..###...#.#..#.#......#....... +...#..........#......#....#.... +.#.#.........##..#.......#...#. +.#..........#...#..#........... +....##.#....................... +.......#...........#...#....... +...#...#..##...#....###..#....# +....#.#.....##...##.#.#........ +...........#.#..#.#......#..#.. +.....#.....#....#...#........#. +..#......#..#.........#.....#.. +.........................#...#. +#...#...#....#........##....#.. +#..#.#.............#..........# +.#.........#.....#..#.#.#..#.#. +#...#..#.......####.#....##.... +##...##..#.#.#...#.#.....#..#.# +.#..#....#.##........#...#....# +#...#..##.#....##..#..#.#...... +.#........#.....#.#....##.##.#. +...#...#........#..#.##.##..... +....................#.#.#.#...# +..####.#..##...#....#.....##... +#......#.....#.#......#.#..#.## +..#.....#..#...........##.#.... +#....#........#............#... +..##....#..............#......# +..#......#.#.......####......#. +..............##....#....##.#.. +.#...............#....#....#.#. +..#.#.#..#.......##.#.......... +.#...#.......#.#....#.##....... +.....#.##...#...........#.#.... +..#.#..#...#..##...#.#.......## +.#.....#....#.#......#.#....... +....##.........#.#............. +.......##.......#.............. +..........#......#......#....## +..##.....#..#.#..........#..... +...#....#.......#....##........ +.......#...........#........... +...#.#......#.#........#....#.. +.....#...........#.#.#...#.#..# +.#.#...#.#.#..........#.....### +#........#...#................. +...##.....#.....#..#..#.......# +......##...........#..#....##.. +.........#............##...#... +.....#.....##...##............. +.#....#..#.#.#.#...#..#..#..... +.....#..#.#..#....#..#......... +....#.....#......#...#......... +#..#..#.................#...... +.###.....#...#.#........##.#... +..#...#....#.##..#.....#.#....# +..#...##.................#.#... +....##..........#..#..#..#....# +....#..##....##.....#.#....#... +.#.#.#.....##........#.##..##.# +....#..#......#..#........#.... +.......#.....###.#....#.......# +#....#.......#......##.#....... +.##.#.........#.#..##..#....##. +......#........#.#....#...#.... +.####.....#.........#.#......## +##....#......#....#..#.#....##. +...........###.#.....#..#...... +.......#...........#........... +........###....#..#.#.......... +....#........#......#.......... +.........#......#.............. +...#...............#......#...# +....#..##...#.........#...#.... +##........#.#....#......###.... +....#.......................#.. +#................#.#..#......## +...#.#.....#...#...........#.## +.#....#.##......#...##.#....#.. +#...#....#..............#..#..# +.......#....#.##............#.# +.....#.#.......#.#...#......... +...#.....#..##...##...#........ +..#.......#..####..#..#...#.... +#.#................##...##.#..# +.....#.....##.#.....#......#..# +....#.#...#.........#.........# +..#......#............#.....#.. +.....#..........#.#..#..##...## +........#................#.#... +#...#.#....##...###...#.#...... +.............##.#..##.......... +#..#......#...........#......#. +#.#....#..........#.##....###.. +.............#.........#....#.. +#........#..#.#..#...#....#.... +..............#..............## +.....#...#..............#.##... +#...##..#...........#.......... +..#....#...#.#........#..#.#..# +..##......#...............#.... +....#...#..###..#......###.#... +.......##..#.#........#....#... +..##...#.......#...#........... +.#.......#.....#.#...##..#....# +.............#.......#.#.#....# +#.......#..#..#...#.#......##.. +#.##..#..#..#....##.#...###.#.# +...##...#..#..#........#.#..#.. +#....##........................ +##...#...#......#.#.....#..#... +......#............#....#...... +#......#.......#.......##.#.... +..................#..#..#.#.... +..#..................##.#...... +..##........#.#.....##..#..#.#. +#....#..............#....####.. +#..#..........................# +..#.#.#.#....#.......#....#.#.. +.....#.#........#..........#.#. +........#.....#.......#........ +#.....#....#.###.....#.......#. +.....##.#...#.#..#...#.#.#..... +......##...#.#...##..........#. +.#............#.....#..#....#.. +.#................#.#..#....... +....................##...##.... +#.......##...#.....#..#........ +.##....#.#.#.#...........#...#. +..#.#..#.#.........#........... +...#......#.....#...##......... +..........#.#.....###.#........ +.............#.....##.......... +.........#...####........#.#### +...................#....#...... +.....#.........#.#....#..#...#. +.##...#.......##.#...#.#.#..#.. +.....##........#....#...#.##.#. +#...#...#.#....#..............# +#..#.##.............#.......... +..#...#..#.#.##..............## +#......#.#...##..........#.##.. +.##.#...#...#.........#.#...... +......#........##.#..#......... +#..#.......#......#.#..#.#..... +.#..#...........#.#.##.....#... +.....................#..#.#.... +........#...##......#.....##... +#.............#...##....##....# +#.#...........#....##.#......## +.....#.....#.#..........###..#. +....#...#....##....#..##....... +.#....#....#.......#.#.....#... +.#...#.......##...##........#.. +......##.......#.##.#.###...... +....##.......#......#.......... +...................#..##....... +......................#...##... +...##....#.#..#..#............. +.#......##..........#...#...... +....##..#....#..#...#...####.#. +...#.......#.......#........#.# +#.........#..#...#...##...#.#.# +....#...#.......#...#....#..... +...#.....#.##..##.#.......##.## +.......#....#........#......... +.....#...#....#..#....#....#... +.##....#...#........#...#.#...# +.......##............#..#...#.. +#.#...#....#......#.#.......... +.#.##...........#........#..... +.#....#.............#.#.##..... +#.......###..#...###.........#. +#..#.#.......#.........#...#..# +..........#......#........#...# +.#.#...#.##.......##........... +.....#.........#.....#......... +.........#.........#....##.#..# +.#.......##..##..#.....#...#... +.#.....##...#..#..............# +..##...#..#..#.#...#..........# +.#.......####......#......####. +##..##........#.....#........#. +..##.#..#.#.................... +...........#..#...##....##..... +..#.#........#.........#....##. +..#...#..##..###.#..###........ +......#..#.............#..##... +.##.........#.#..#...#.##.###.. +.#...............#...........#. +.#....#........#....#........## +..#####.#.#..#.#........##...#. +###....#....#...#.............. +.....#...##............#...#... +##...........##.#.##.....#..... +..............#..#.....#...#... +...................#........... +#..........##.........#........ +...#.........#..#.....#..#..#.. +....###.#......#......##....#.. +#......#..........#...#........ +...#.#...#..#..........##...... +.....##.....#.#............##.. +..#..#.###....#.#.#...##....#.. +...#........#....##.......#.... +.#.............#..##.......#... +..#.#..###..#.....#...##....... +.........#......##...#.#..#.... +.............#....##....#.#.... +#..#...#....#.#...#......##.... +.............#.#......#.....### +#.##....#........#............. +.....#...#.####...#.....#...... +....#....###....##.......#..... +..#....##..#....#.#.......#.... +...#.....#....#.........#...... +.#......#.#....#.#........#.... +.......#......#.....#.#..#..... +#......#.........##.##.#...#... +..#.###...................#.... +....#..#....##.#........#....#. +...........#..........#......#. +.#..#.#...###..........#..#...# +...#...##..#....#...#.......... +.#........#.................##. +....#.......##....#...#........ +#.#...##.##...#.#.......#...#.. +.....#.#.##.#......#..#..##.... +.....##...#.#.....#...#........ +#.#.......#..#..........##..... +................#......#..#.#.# +#......#...#................... +...#.....##.#.........#.#..#..# +...#..##..##.......#....#...... +....##...#....#..#...........#. +..#..#......#...#..#........... +...#.##....#...##.......#...... +.......#....#..#..##..#..#....# +.#.................#.#...#.##.. +.....#..................#..#.#. +...#......##...#...........#... +..#.........#....#..#...#.....# +..#...#.....#.........##.#..... +.....#.#....##...............#. +....#...#............#......... +.....#.....###............#.... +..#.#.#.......#....#........... +...........##...##...#.......#. +.........###.#......#.......... +.#.......#....#.....#.##..#...# +..#..................#..###.... +..#....#...#......##.........#. +........#..#........#.........# +.#..#......#.........#......... +...#..##.....#....#....#.....#. +......#.#............###.....## +.......#........#.......#.#.... +..#.............#.............. +.............##..#.#.#....#.... +.................#....#.#...... +##..#.#.......#....#.....#..... +.##............##.#.......#.#.. +#..#...........##......#....... +.##......#####..##.#....#.#.... +.......##.....#...#........#... +.#.#.....##....#..#....#..#...# +............##.#.....##.#...... +........##...###.#......#...... +......#..#.#...#..#............ +.........#...........#......#.. +.#.........#............##..... +.#..#..#...#.#.............#... +......#.#..##...#.#...........# +#.##.......#...#.........#..... +.....#..#............#....##... +.#......#........#............. +..#...#....#..#.......###...... +....#.......###.#.#...........# +.............#...##............ +.##.#.#.#...........#...#....#. +............##.........#......# +...............#......#...#.... +...#.....#..###..#...........#. +.#........#.....##........#.#.. +....#.#.......#..#..#...##.#.#. +.......##...........#...#...... +....#.#..##......#.......#..... +..#........#.#......#.#........ +........#....#..#....#..##..... +.#.........##..........#.#..... +..##...##.....##......##..#.... +.###.....##...........##.#...## +...#................#.......#.. +#.......#.#.#..#.#.##..#...#... +.#.#.......#..#................ +..#.#.#......#............#.... +#.....#.###..#.#...#........... +#...........#..........#.#.#.## +..#.#...#......##.....#........ +........#.......#.#...#...#.... +..#..........#......###......#. +..........##.#....#.....#.##... +..#.....#......#.........#..##. +.#...#........#..#.#..#...##..# +..###........#......#.#........ +..#.##.#....#.#....#.#...#..... \ No newline at end of file diff --git a/src/Day3.hs b/src/Day3.hs new file mode 100644 index 0000000..c72886b --- /dev/null +++ b/src/Day3.hs @@ -0,0 +1,65 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE OverloadedStrings #-} + +module Day3 (day3) where + +import Control.Applicative ((<|>)) +import qualified Data.Attoparsec.Text as P +import qualified Data.Text as T +import qualified Data.Vector as V +import Numeric.Natural (Natural) + +data Loc = Tree | Open deriving (Show, Eq) + +type Hor = V.Vector Loc + +type Grid = V.Vector Hor + +parserLoc :: P.Parser Loc +parserLoc = do + loc <- P.char '.' <|> P.char '#' + case loc of + '.' -> return Open + '#' -> return Tree + +parserLine :: P.Parser Hor +parserLine = do + locs <- P.many1 parserLoc + return $ V.fromList locs + +parserGrid :: P.Parser Grid +parserGrid = do + grid <- parserLine `P.sepBy` P.endOfLine + return $ V.fromList grid + +slideDown :: + (Natural, Natural) -> + (Natural, Natural) -> + Grid -> + Maybe [Loc] -> + Maybe [Loc] +slideDown _ _ _ Nothing = Nothing +slideDown (x, y) (x'inc, y'inc) g (Just r) + | y >= (fromIntegral . length) g = Just r + | otherwise = do + hor <- g V.!? fromIntegral y + let x' = x `mod` (fromIntegral . length) hor + loc <- hor V.!? fromIntegral x' + slideDown (x' + x'inc, y + y'inc) (x'inc, y'inc) g (Just (r ++ [loc])) + +countTrees :: [Loc] -> Int +countTrees = length . filter (== Tree) + +parseGrid :: String -> Grid +parseGrid s = + case P.parseOnly parserGrid (T.pack s) of + Left s -> error (show s) + Right r -> r + +day3 :: IO () +day3 = do + r <- readFile "./input/day3" + putStr "[Day 3-1] # trees: " + case slideDown (0, 0) (3, 1) (parseGrid r) (Just []) of + Nothing -> putStrLn "ERROR" + Just locs -> print . countTrees $ locs \ No newline at end of file