advent of code solutions op.tngl.io
haskell aoc
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

2018: add all

Signed-off-by: oppiliappan <me@oppi.li>

+100
+26
src/2018/01.lhs
··· 1 + {-# LANGUAGE LambdaCase #-} 2 + 3 + import qualified Data.Set as S 4 + import System.Environment (getArgs) 5 + 6 + parse :: String -> Int 7 + parse ('+' : f) = read f 8 + parse f = read f 9 + 10 + p1 = sum 11 + 12 + p2 n = firstDup $ scanl1 (+) (cycle n) 13 + 14 + firstDup = go S.empty 15 + where 16 + go seen (x : xs) = if S.member x seen then x else go (S.insert x seen) xs 17 + 18 + main = do 19 + n <- 20 + fmap (map parse . lines) $ 21 + getArgs 22 + >>= \case 23 + ["-"] -> getContents 24 + [file] -> readFile file 25 + print $ p1 n 26 + print $ p2 n
+25
src/2018/02.lhs
··· 1 + {-# LANGUAGE LambdaCase #-} 2 + 3 + import qualified Data.Map as M 4 + import System.Environment (getArgs) 5 + 6 + freqs = M.fromListWith (+) . map (,1) 7 + 8 + p1 n = occurs 2 fqs * occurs 3 fqs 9 + where 10 + fqs = map freqs n 11 + occurs n = length . filter (not . M.null . M.filter (== n)) 12 + 13 + lev x y = sum [1 | (a, b) <- zip x y, a /= b] 14 + 15 + p2 n = [(a, b) | a <- n, b <- n, a /= b, lev a b == 1] 16 + 17 + main = do 18 + n <- 19 + fmap lines $ 20 + getArgs 21 + >>= \case 22 + ["-"] -> getContents 23 + [file] -> readFile file 24 + print $ p1 n 25 + print $ p2 n
+49
src/2018/03.lhs
··· 1 + {-# LANGUAGE LambdaCase #-} 2 + 3 + import Data.List.Split (splitOn) 4 + import qualified Data.Map as M 5 + import qualified Data.Set as S 6 + import System.Environment (getArgs) 7 + 8 + parse :: String -> (Int, (Int, Int), (Int, Int)) 9 + parse l = (read (tail a), (read x, read y), (read w, read h)) 10 + where 11 + [a, b, c, d] = splitOn " " l 12 + [x, y] = splitOn "," (init c) 13 + [w, h] = splitOn "x" d 14 + 15 + grid = 16 + M.fromList 17 + [ ((x, y), []) 18 + | x <- [0 .. 999], 19 + y <- [0 .. 999] 20 + ] 21 + 22 + claim grid (id, (x, y), (w, h)) = foldl conv grid subgrid 23 + where 24 + subgrid = [(x', y') | x' <- [x .. x + w - 1], y' <- [y .. y + h - 1]] 25 + conv g (x', y') = M.adjust (id :) (x', y') g 26 + 27 + p1 n = M.size $ M.filter ((> 1) . length) $ foldl claim grid n 28 + 29 + (-|-) x y = (x S.\\ y) `S.union` (y S.\\ x) 30 + 31 + p2 n = a -|- seen 32 + where 33 + a = S.fromList $ map (\(a, _, _) -> a) n 34 + seen = 35 + foldl1 S.union $ 36 + map S.fromList $ 37 + M.elems $ 38 + M.filter ((> 1) . length) $ 39 + foldl claim grid n 40 + 41 + main = do 42 + n <- 43 + fmap (map parse . lines) $ 44 + getArgs 45 + >>= \case 46 + ["-"] -> getContents 47 + [file] -> readFile file 48 + print $ p1 n 49 + print $ p2 n