Merge AoC 2019 & 2020 Haskell codebases

This commit is contained in:
Xavier Morel
2020-12-03 10:22:16 +01:00
parent 06a86e9d37
commit a04451f2f7
57 changed files with 364 additions and 688 deletions

View File

@@ -0,0 +1,33 @@
module Y2020.Day01 (y20day01) where
import Data.Maybe
import Data.Sort
findPair :: Int -> Int -> [Int] -> Maybe (Int, Int)
findPair target x (h:t)
| target == x + h = Just (x, h)
| target < x + h = Nothing
| otherwise = findPair target x t
findPair _ _ [] = Nothing
findFirstMatchingPair :: Int -> [Int] -> Maybe (Int, Int)
findFirstMatchingPair target (x:xs)
| isJust res = res
| otherwise = findFirstMatchingPair target (xs)
where res = findPair target x xs
findFirstMatchingPair _ _ = Nothing
findFirstMatchingTriplet :: Int -> [Int] -> (Int, Int, Int)
findFirstMatchingTriplet target (x:xs)
| isJust res = let Just (a, b) = res in (x, a, b)
| otherwise = findFirstMatchingTriplet target xs
where res = findFirstMatchingPair (target - x) xs
y20day01 :: [String] -> (String, String)
y20day01 input = (part1, part2)
where
entries = (sort (map read input)) :: [Int]
Just (a, b) = findFirstMatchingPair 2020 entries
(x, y, z) = findFirstMatchingTriplet 2020 entries
part1 = show (a * b)
part2 = show (x * y * z)

View File

@@ -0,0 +1,25 @@
module Y2020.Day02 (y20day02) where
import Data.List.Split
parseInput :: String -> (Int, Int, Char, String)
parseInput input = (qty 0, qty 1, (parts !! 1) !! 0, parts !! 2)
where parts = splitOn " " $ filter (\c -> not $ elem c ":") input
qty n = read $ (!! n) $ splitOn "-" $ (parts !! 0)
isValidPassP1 :: (Int, Int, Char, String) -> Bool
isValidPassP1 (minP, maxP, c, pass) = count >= minP && count <= maxP
where count = length $ filter (== c) pass
isValidPassP2 :: (Int, Int, Char, String) -> Bool
isValidPassP2 (pos1, pos2, c, pass) = isPos pos1 `xor` isPos pos2
where isPos p = (pass !! (p - 1)) == c
xor a b = (a || b) && (a /= b)
y20day02 :: [String] -> (String, String)
y20day02 input = (part1, part2)
where
entries = map parseInput $ input
strCount f xs = show $ length $ filter f xs
part1 = strCount isValidPassP1 entries
part2 = strCount isValidPassP2 entries

View File

@@ -0,0 +1,22 @@
module Y2020.Day03 (y20day03) where
walkMap :: (Int, Int) -> (Int, Int) -> [(Int, Int)]
walkMap (w, h) (wstep, hstep) = zip wCoords hCoords
where wCoords = map (`mod` w) $ take h $ iterate (+ wstep) 0
hCoords = take h $ iterate (+ hstep) 0
getCharAtPos :: [String] -> (Int, Int) -> Char
getCharAtPos myMap (w, h)
| h <= length myMap = (myMap !! h) !! w
| otherwise = '\0'
getNumberOfTrees :: [String] -> [(Int, Int)] -> Int
getNumberOfTrees myMap coords = length $ filter (== '#') $ map (getCharAtPos myMap) coords
y20day03 :: [String] -> (String, String)
y20day03 input = (part1, part2)
where width = length $ input !! 0
height = (length $ input)
getNb slope = getNumberOfTrees input $ walkMap (width, height) slope
part1 = show $ getNb (3, 1)
part2 = show $ product $ map getNb [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]

View File

@@ -0,0 +1,5 @@
module Y2020.Days (y20day01, y20day02, y20day03) where
import Y2020.Day01
import Y2020.Day02
import Y2020.Day03