Solve day 3, part 1

pull/1/head
Gaël Depreeuw 4 years ago
parent 0e125a1cb0
commit a9b566e9ad
Signed by: Mithror
GPG Key ID: 8AB218ABA4867F78
  1. 3
      AdventOfCode2020.cabal
  2. 2
      Main.hs
  3. 323
      input/day3
  4. 65
      src/Day3.hs

@ -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

@ -2,8 +2,10 @@ module Main where
import Day1 (day1)
import Day2 (day2)
import Day3 (day3)
main :: IO ()
main = do
day1
day2
day3

@ -0,0 +1,323 @@
.##.#.........#.....#....#...#.
.#.#.#...#.......#.............
......#..#....#.#...###.......#
.......###......#.....#..##..#.
..#...##.......#.......###.....
....###.#....###......#....#..#
......#..#....#...##...........
..#..#....#...#.....####.......
...#........#.#.......#..#...#.
......#...#........#...#..##...
#..#........#............#...##
..#..#.#....#...........#...###
#.#..#...........#.##.#.#....#.
.#.#....#...##.....#...........
.....##....#...#..............#
...#....#...#.#.#.#...#........
#....#....#.#.#..#....#..#..#..
.................#..#.....#....
#..###...#.#..#.#......#.......
...#..........#......#....#....
.#.#.........##..#.......#...#.
.#..........#...#..#...........
....##.#.......................
.......#...........#...#.......
...#...#..##...#....###..#....#
....#.#.....##...##.#.#........
...........#.#..#.#......#..#..
.....#.....#....#...#........#.
..#......#..#.........#.....#..
.........................#...#.
#...#...#....#........##....#..
#..#.#.............#..........#
.#.........#.....#..#.#.#..#.#.
#...#..#.......####.#....##....
##...##..#.#.#...#.#.....#..#.#
.#..#....#.##........#...#....#
#...#..##.#....##..#..#.#......
.#........#.....#.#....##.##.#.
...#...#........#..#.##.##.....
....................#.#.#.#...#
..####.#..##...#....#.....##...
#......#.....#.#......#.#..#.##
..#.....#..#...........##.#....
#....#........#............#...
..##....#..............#......#
..#......#.#.......####......#.
..............##....#....##.#..
.#...............#....#....#.#.
..#.#.#..#.......##.#..........
.#...#.......#.#....#.##.......
.....#.##...#...........#.#....
..#.#..#...#..##...#.#.......##
.#.....#....#.#......#.#.......
....##.........#.#.............
.......##.......#..............
..........#......#......#....##
..##.....#..#.#..........#.....
...#....#.......#....##........
.......#...........#...........
...#.#......#.#........#....#..
.....#...........#.#.#...#.#..#
.#.#...#.#.#..........#.....###
#........#...#.................
...##.....#.....#..#..#.......#
......##...........#..#....##..
.........#............##...#...
.....#.....##...##.............
.#....#..#.#.#.#...#..#..#.....
.....#..#.#..#....#..#.........
....#.....#......#...#.........
#..#..#.................#......
.###.....#...#.#........##.#...
..#...#....#.##..#.....#.#....#
..#...##.................#.#...
....##..........#..#..#..#....#
....#..##....##.....#.#....#...
.#.#.#.....##........#.##..##.#
....#..#......#..#........#....
.......#.....###.#....#.......#
#....#.......#......##.#.......
.##.#.........#.#..##..#....##.
......#........#.#....#...#....
.####.....#.........#.#......##
##....#......#....#..#.#....##.
...........###.#.....#..#......
.......#...........#...........
........###....#..#.#..........
....#........#......#..........
.........#......#..............
...#...............#......#...#
....#..##...#.........#...#....
##........#.#....#......###....
....#.......................#..
#................#.#..#......##
...#.#.....#...#...........#.##
.#....#.##......#...##.#....#..
#...#....#..............#..#..#
.......#....#.##............#.#
.....#.#.......#.#...#.........
...#.....#..##...##...#........
..#.......#..####..#..#...#....
#.#................##...##.#..#
.....#.....##.#.....#......#..#
....#.#...#.........#.........#
..#......#............#.....#..
.....#..........#.#..#..##...##
........#................#.#...
#...#.#....##...###...#.#......
.............##.#..##..........
#..#......#...........#......#.
#.#....#..........#.##....###..
.............#.........#....#..
#........#..#.#..#...#....#....
..............#..............##
.....#...#..............#.##...
#...##..#...........#..........
..#....#...#.#........#..#.#..#
..##......#...............#....
....#...#..###..#......###.#...
.......##..#.#........#....#...
..##...#.......#...#...........
.#.......#.....#.#...##..#....#
.............#.......#.#.#....#
#.......#..#..#...#.#......##..
#.##..#..#..#....##.#...###.#.#
...##...#..#..#........#.#..#..
#....##........................
##...#...#......#.#.....#..#...
......#............#....#......
#......#.......#.......##.#....
..................#..#..#.#....
..#..................##.#......
..##........#.#.....##..#..#.#.
#....#..............#....####..
#..#..........................#
..#.#.#.#....#.......#....#.#..
.....#.#........#..........#.#.
........#.....#.......#........
#.....#....#.###.....#.......#.
.....##.#...#.#..#...#.#.#.....
......##...#.#...##..........#.
.#............#.....#..#....#..
.#................#.#..#.......
....................##...##....
#.......##...#.....#..#........
.##....#.#.#.#...........#...#.
..#.#..#.#.........#...........
...#......#.....#...##.........
..........#.#.....###.#........
.............#.....##..........
.........#...####........#.####
...................#....#......
.....#.........#.#....#..#...#.
.##...#.......##.#...#.#.#..#..
.....##........#....#...#.##.#.
#...#...#.#....#..............#
#..#.##.............#..........
..#...#..#.#.##..............##
#......#.#...##..........#.##..
.##.#...#...#.........#.#......
......#........##.#..#.........
#..#.......#......#.#..#.#.....
.#..#...........#.#.##.....#...
.....................#..#.#....
........#...##......#.....##...
#.............#...##....##....#
#.#...........#....##.#......##
.....#.....#.#..........###..#.
....#...#....##....#..##.......
.#....#....#.......#.#.....#...
.#...#.......##...##........#..
......##.......#.##.#.###......
....##.......#......#..........
...................#..##.......
......................#...##...
...##....#.#..#..#.............
.#......##..........#...#......
....##..#....#..#...#...####.#.
...#.......#.......#........#.#
#.........#..#...#...##...#.#.#
....#...#.......#...#....#.....
...#.....#.##..##.#.......##.##
.......#....#........#.........
.....#...#....#..#....#....#...
.##....#...#........#...#.#...#
.......##............#..#...#..
#.#...#....#......#.#..........
.#.##...........#........#.....
.#....#.............#.#.##.....
#.......###..#...###.........#.
#..#.#.......#.........#...#..#
..........#......#........#...#
.#.#...#.##.......##...........
.....#.........#.....#.........
.........#.........#....##.#..#
.#.......##..##..#.....#...#...
.#.....##...#..#..............#
..##...#..#..#.#...#..........#
.#.......####......#......####.
##..##........#.....#........#.
..##.#..#.#....................
...........#..#...##....##.....
..#.#........#.........#....##.
..#...#..##..###.#..###........
......#..#.............#..##...
.##.........#.#..#...#.##.###..
.#...............#...........#.
.#....#........#....#........##
..#####.#.#..#.#........##...#.
###....#....#...#..............
.....#...##............#...#...
##...........##.#.##.....#.....
..............#..#.....#...#...
...................#...........
#..........##.........#........
...#.........#..#.....#..#..#..
....###.#......#......##....#..
#......#..........#...#........
...#.#...#..#..........##......
.....##.....#.#............##..
..#..#.###....#.#.#...##....#..
...#........#....##.......#....
.#.............#..##.......#...
..#.#..###..#.....#...##.......
.........#......##...#.#..#....
.............#....##....#.#....
#..#...#....#.#...#......##....
.............#.#......#.....###
#.##....#........#.............
.....#...#.####...#.....#......
....#....###....##.......#.....
..#....##..#....#.#.......#....
...#.....#....#.........#......
.#......#.#....#.#........#....
.......#......#.....#.#..#.....
#......#.........##.##.#...#...
..#.###...................#....
....#..#....##.#........#....#.
...........#..........#......#.
.#..#.#...###..........#..#...#
...#...##..#....#...#..........
.#........#.................##.
....#.......##....#...#........
#.#...##.##...#.#.......#...#..
.....#.#.##.#......#..#..##....
.....##...#.#.....#...#........
#.#.......#..#..........##.....
................#......#..#.#.#
#......#...#...................
...#.....##.#.........#.#..#..#
...#..##..##.......#....#......
....##...#....#..#...........#.
..#..#......#...#..#...........
...#.##....#...##.......#......
.......#....#..#..##..#..#....#
.#.................#.#...#.##..
.....#..................#..#.#.
...#......##...#...........#...
..#.........#....#..#...#.....#
..#...#.....#.........##.#.....
.....#.#....##...............#.
....#...#............#.........
.....#.....###............#....
..#.#.#.......#....#...........
...........##...##...#.......#.
.........###.#......#..........
.#.......#....#.....#.##..#...#
..#..................#..###....
..#....#...#......##.........#.
........#..#........#.........#
.#..#......#.........#.........
...#..##.....#....#....#.....#.
......#.#............###.....##
.......#........#.......#.#....
..#.............#..............
.............##..#.#.#....#....
.................#....#.#......
##..#.#.......#....#.....#.....
.##............##.#.......#.#..
#..#...........##......#.......
.##......#####..##.#....#.#....
.......##.....#...#........#...
.#.#.....##....#..#....#..#...#
............##.#.....##.#......
........##...###.#......#......
......#..#.#...#..#............
.........#...........#......#..
.#.........#............##.....
.#..#..#...#.#.............#...
......#.#..##...#.#...........#
#.##.......#...#.........#.....
.....#..#............#....##...
.#......#........#.............
..#...#....#..#.......###......
....#.......###.#.#...........#
.............#...##............
.##.#.#.#...........#...#....#.
............##.........#......#
...............#......#...#....
...#.....#..###..#...........#.
.#........#.....##........#.#..
....#.#.......#..#..#...##.#.#.
.......##...........#...#......
....#.#..##......#.......#.....
..#........#.#......#.#........
........#....#..#....#..##.....
.#.........##..........#.#.....
..##...##.....##......##..#....
.###.....##...........##.#...##
...#................#.......#..
#.......#.#.#..#.#.##..#...#...
.#.#.......#..#................
..#.#.#......#............#....
#.....#.###..#.#...#...........
#...........#..........#.#.#.##
..#.#...#......##.....#........
........#.......#.#...#...#....
..#..........#......###......#.
..........##.#....#.....#.##...
..#.....#......#.........#..##.
.#...#........#..#.#..#...##..#
..###........#......#.#........
..#.##.#....#.#....#.#...#.....

@ -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
Loading…
Cancel
Save