[hs] Solve 2020_03

This commit is contained in:
Joscha 2020-12-03 11:49:24 +00:00
parent 7183f49a6b
commit 23636d3fc0
4 changed files with 56 additions and 6 deletions

43
hs/src/Aoc/Y2020/D03.hs Normal file
View 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