diff --git a/2020/1.hs b/2020/1.hs index 8cb128b..f4935dd 100644 --- a/2020/1.hs +++ b/2020/1.hs @@ -3,3 +3,10 @@ main = readFile "1" >>= putStrLn . show . f . g . map (\x -> read x :: Integer) f (x:xs, _:ys) = f (x:xs, ys) f (_:xs, []) = f $ g xs g (x:xs) = (x:xs, xs) + +main2 = readFile "1" >>= putStrLn . show . f . g . map (\x -> read x :: Integer) . lines + where f (x:_, y:_, z:_) | x + y + z == 2020 = x * y * z + f (x:xs, y:ys, _:zs) = f (x:xs, y:ys, zs) + f (x:xs, _:y:ys, []) = f (x:xs, y:ys, ys) + f (_:xs, _:[], []) = f $ g xs + g (x1:x2:xs) = (x1:x2:xs, x2:xs, xs) diff --git a/2020/2.hs b/2020/2.hs index 5b18e62..76e45a3 100644 --- a/2020/2.hs +++ b/2020/2.hs @@ -8,3 +8,9 @@ main = readFile "2" >>= putStrLn . show . length . filter (parse 0 0) . lines parse min tmpnum ('-':xs) = parse tmpnum 0 xs parse min tmpnum (' ':x:':':' ':xs) = valid min tmpnum x xs parse min tmpnum (x:xs) = parse min (tmpnum * 10 + digitToInt x) xs + +main2 = readFile "2" >>= putStrLn . show . length . filter (parse 0 0) . lines + where valid min max chr pass = (if length pass > min - 1 then pass !! (min - 1) == chr else False) /= (if length pass > max - 1 then pass !! (max - 1) == chr else False) + parse min tmpnum ('-':xs) = parse tmpnum 0 xs + parse min tmpnum (' ':x:':':' ':xs) = valid min tmpnum x xs + parse min tmpnum (x:xs) = parse min (tmpnum * 10 + digitToInt x) xs diff --git a/2020/3.hs b/2020/3.hs index 08476e8..ecce461 100644 --- a/2020/3.hs +++ b/2020/3.hs @@ -1,3 +1,12 @@ -main = readFile "3" >>= putStrLn . show . sum . map (\(i, elem) -> case elem !! (i * 3 `mod` length elem) of - '#' -> 1 - _ -> 0) . zip [0..] . lines +import Data.List + +main = readFile "3" >>= putStrLn . show . product . tuple5map sum . unzip5 . map (\(i, elem) -> let f i = case elem !! (i `mod` length elem) of + '#' -> 1 + _ -> 0 + in + tuple5 ((if i `mod` 2 == 0 then + f (i `div` 2) + else + 0):(map f [i, i * 3, i * 5, i * 7]))) . zip [0..] . lines + where tuple5 [a, b, c, d, e] = (a, b, c, d, e) + tuple5map f (a, b, c, d, e) = [f a, f b, f c, f d, f e]