well.. I did not realise that the challenges had two parts t_t. I did them in a hurry so they look trash

This commit is contained in:
Misaka Mikoto 2020-12-03 19:53:27 +02:00
parent c5c24e7305
commit 359e0c06b4
3 changed files with 25 additions and 3 deletions

View file

@ -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)

View file

@ -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

View file

@ -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]