[hs] Solve 2020_03
This commit is contained in:
parent
7183f49a6b
commit
23636d3fc0
4 changed files with 56 additions and 6 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
module Aoc.Day
|
module Aoc.Day
|
||||||
( Day(..)
|
( Day(..)
|
||||||
|
, runDay
|
||||||
, dayPure
|
, dayPure
|
||||||
, dayFile
|
, dayFile
|
||||||
, dayString
|
, dayString
|
||||||
|
|
@ -19,6 +20,11 @@ data Day
|
||||||
= DayPure String (IO ())
|
= DayPure String (IO ())
|
||||||
| DayFile String (FilePath -> IO ())
|
| DayFile String (FilePath -> IO ())
|
||||||
|
|
||||||
|
-- | Helper function for trying out days in ghci.
|
||||||
|
runDay :: Day -> FilePath -> IO ()
|
||||||
|
runDay (DayPure _ f) _ = f
|
||||||
|
runDay (DayFile _ f) p = f p
|
||||||
|
|
||||||
dayPure :: String -> IO () -> Day
|
dayPure :: String -> IO () -> Day
|
||||||
dayPure = DayPure
|
dayPure = DayPure
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,11 @@ module Aoc.Parse
|
||||||
, module Text.Megaparsec.Char.Lexer
|
, module Text.Megaparsec.Char.Lexer
|
||||||
, Parser
|
, Parser
|
||||||
, manyLines
|
, manyLines
|
||||||
, word
|
|
||||||
, untilEol
|
, untilEol
|
||||||
|
, lineChar
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Void
|
import Data.Void
|
||||||
import Data.Char
|
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Text.Megaparsec
|
import Text.Megaparsec
|
||||||
|
|
@ -21,10 +20,10 @@ import Text.Megaparsec.Char.Lexer (binary, decimal, float,
|
||||||
type Parser = Parsec Void T.Text
|
type Parser = Parsec Void T.Text
|
||||||
|
|
||||||
manyLines :: Parser a -> Parser [a]
|
manyLines :: Parser a -> Parser [a]
|
||||||
manyLines p = sepEndBy (try p) newline
|
manyLines p = endBy (try p) newline
|
||||||
|
|
||||||
word :: Parser T.Text
|
|
||||||
word = takeWhileP Nothing (not . isSeparator)
|
|
||||||
|
|
||||||
untilEol :: Parser T.Text
|
untilEol :: Parser T.Text
|
||||||
untilEol = takeWhileP Nothing (/= '\n')
|
untilEol = takeWhileP Nothing (/= '\n')
|
||||||
|
|
||||||
|
lineChar :: Parser Char
|
||||||
|
lineChar = satisfy (/= '\n')
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,11 @@ module Aoc.Y2020
|
||||||
import Aoc.Day
|
import Aoc.Day
|
||||||
import qualified Aoc.Y2020.D01 as D01
|
import qualified Aoc.Y2020.D01 as D01
|
||||||
import qualified Aoc.Y2020.D02 as D02
|
import qualified Aoc.Y2020.D02 as D02
|
||||||
|
import qualified Aoc.Y2020.D03 as D03
|
||||||
|
|
||||||
days :: [Day]
|
days :: [Day]
|
||||||
days =
|
days =
|
||||||
[ D01.day
|
[ D01.day
|
||||||
, D02.day
|
, D02.day
|
||||||
|
, D03.day
|
||||||
]
|
]
|
||||||
|
|
|
||||||
43
hs/src/Aoc/Y2020/D03.hs
Normal file
43
hs/src/Aoc/Y2020/D03.hs
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
module Aoc.Y2020.D03
|
||||||
|
( day
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Data.List
|
||||||
|
|
||||||
|
import Aoc.Day
|
||||||
|
import Aoc.Parse
|
||||||
|
|
||||||
|
parser :: Parser [[Bool]]
|
||||||
|
parser = manyLines $ many $ do
|
||||||
|
c <- lineChar
|
||||||
|
pure $ c == '#'
|
||||||
|
|
||||||
|
slope :: Int -> Int -> [Maybe Int]
|
||||||
|
slope dx dy = intercalate (replicate (dy - 1) Nothing) [[Just x] | x <- [0,dx..]]
|
||||||
|
|
||||||
|
onSlope :: [[Bool]] -> [Maybe Int] -> Int
|
||||||
|
onSlope trees s = length $ filter id $ [row !! x | (row, Just x) <- zip trees s]
|
||||||
|
|
||||||
|
solver :: [[Bool]] -> IO ()
|
||||||
|
solver trees = do
|
||||||
|
let infTrees = map cycle trees
|
||||||
|
|
||||||
|
putStrLn ">> Part 1"
|
||||||
|
let treesHit = length $ filter id $ zipWith (!!) infTrees [0,3..]
|
||||||
|
putStrLn $ "Trees hit for slope 3-1: " ++ show treesHit
|
||||||
|
|
||||||
|
putStrLn ">> Part 2"
|
||||||
|
let oneOne = onSlope infTrees $ slope 1 1
|
||||||
|
threeOne = onSlope infTrees $ slope 3 1
|
||||||
|
fiveOne = onSlope infTrees $ slope 5 1
|
||||||
|
sevenOne = onSlope infTrees $ slope 7 1
|
||||||
|
oneTwo = onSlope infTrees $ slope 1 2
|
||||||
|
putStrLn $ "right 1, down 1: " ++ show oneOne
|
||||||
|
putStrLn $ "right 3, down 1: " ++ show threeOne
|
||||||
|
putStrLn $ "right 5, down 1: " ++ show fiveOne
|
||||||
|
putStrLn $ "right 7, down 1: " ++ show sevenOne
|
||||||
|
putStrLn $ "right 1, down 2: " ++ show oneTwo
|
||||||
|
putStrLn $ "Product: " ++ show (oneOne * threeOne * fiveOne * sevenOne * oneTwo)
|
||||||
|
|
||||||
|
day :: Day
|
||||||
|
day = dayParse "2020_03" parser solver
|
||||||
Loading…
Add table
Add a link
Reference in a new issue