[hs] Clean up 2020 days

This commit is contained in:
Joscha 2020-12-03 11:51:19 +00:00
parent 23636d3fc0
commit c48319423d
3 changed files with 16 additions and 10 deletions

View file

@ -31,8 +31,9 @@ solver values = do
let (x1, x2) = findPair values let (x1, x2) = findPair values
putStrLn $ show x1 ++ " * " ++ show x2 ++ " = " ++ show (x1 * x2) putStrLn $ show x1 ++ " * " ++ show x2 ++ " = " ++ show (x1 * x2)
let (y1, y2, y3) = findTriple values putStrLn ""
putStrLn ">> Part 2" putStrLn ">> Part 2"
let (y1, y2, y3) = findTriple values
putStrLn $ show y1 ++ " * " ++ show y2 ++ " * " ++ show y3 ++ " = " ++ show (y1 * y2 * y3) putStrLn $ show y1 ++ " * " ++ show y2 ++ " * " ++ show y3 ++ " = " ++ show (y1 * y2 * y3)
day :: Day day :: Day

View file

@ -37,6 +37,7 @@ solver ls = do
putStrLn ">> Part 1" putStrLn ">> Part 1"
print $ length $ filter validCount ls print $ length $ filter validCount ls
putStrLn ""
putStrLn ">> Part 2" putStrLn ">> Part 2"
print $ length $ filter validPositions ls print $ length $ filter validPositions ls

View file

@ -7,18 +7,21 @@ import Data.List
import Aoc.Day import Aoc.Day
import Aoc.Parse import Aoc.Parse
parser :: Parser [[Bool]] type Forest = [[Bool]]
type Slope = [Maybe Int]
parser :: Parser Forest
parser = manyLines $ many $ do parser = manyLines $ many $ do
c <- lineChar c <- lineChar
pure $ c == '#' pure $ c == '#'
slope :: Int -> Int -> [Maybe Int] slope :: Int -> Int -> Slope
slope dx dy = intercalate (replicate (dy - 1) Nothing) [[Just x] | x <- [0,dx..]] slope dx dy = intercalate (replicate (dy - 1) Nothing) [[Just x] | x <- [0,dx..]]
onSlope :: [[Bool]] -> [Maybe Int] -> Int onSlope :: Forest -> Slope -> Int
onSlope trees s = length $ filter id $ [row !! x | (row, Just x) <- zip trees s] onSlope trees s = length $ filter id [row !! x | (row, Just x) <- zip trees s]
solver :: [[Bool]] -> IO () solver :: Forest -> IO ()
solver trees = do solver trees = do
let infTrees = map cycle trees let infTrees = map cycle trees
@ -26,6 +29,7 @@ solver trees = do
let treesHit = length $ filter id $ zipWith (!!) infTrees [0,3..] let treesHit = length $ filter id $ zipWith (!!) infTrees [0,3..]
putStrLn $ "Trees hit for slope 3-1: " ++ show treesHit putStrLn $ "Trees hit for slope 3-1: " ++ show treesHit
putStrLn ""
putStrLn ">> Part 2" putStrLn ">> Part 2"
let oneOne = onSlope infTrees $ slope 1 1 let oneOne = onSlope infTrees $ slope 1 1
threeOne = onSlope infTrees $ slope 3 1 threeOne = onSlope infTrees $ slope 3 1