Day 12, plus some edits to days 1 and 2
This commit is contained in:
parent
1078b40201
commit
a90a59c846
11
1.hs
11
1.hs
|
@ -1,18 +1,13 @@
|
||||||
import Text.Read
|
|
||||||
import Data.Maybe
|
|
||||||
|
|
||||||
circularSum :: [Int] -> Int
|
circularSum :: [Int] -> Int
|
||||||
circularSum ns = snd $ foldr (\n (prev, sum) -> (n, if n == prev then n + sum else sum)) (last ns, 0) ns
|
circularSum ns = snd $ foldr (\n (prev, sum) -> (n, n * fromEnum (n == prev) + sum)) (last ns, 0) ns
|
||||||
|
|
||||||
halfwaySum :: [Int] -> Int
|
halfwaySum :: [Int] -> Int
|
||||||
halfwaySum ns =
|
halfwaySum ns =
|
||||||
let ms = drop (length ns `div` 2) ns ++ take (length ns `div` 2) ns
|
let ms = drop (length ns `div` 2) ns ++ take (length ns `div` 2) ns
|
||||||
nms = zip ns ms
|
in foldr (\(n, m) sum -> n * fromEnum (n == m) + sum) 0 $ zip ns ms
|
||||||
in foldr (\(n, m) sum -> if n == m then n + sum else sum) 0 nms
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "1.txt"
|
nums <- fmap (map (read . pure)) $ readFile "1.txt"
|
||||||
let nums = catMaybes $ map (readMaybe . (:[])) input :: [Int]
|
|
||||||
print $ circularSum nums
|
print $ circularSum nums
|
||||||
print $ halfwaySum nums
|
print $ halfwaySum nums
|
|
@ -0,0 +1,32 @@
|
||||||
|
import Data.List.Split (splitOn)
|
||||||
|
import Data.IntMap (IntMap, findWithDefault)
|
||||||
|
import Data.IntSet (IntSet, member, notMember, insert, delete, empty, size, findMin)
|
||||||
|
import qualified Data.IntMap as M (fromList)
|
||||||
|
import qualified Data.IntSet as S (fromList, null)
|
||||||
|
|
||||||
|
parseLine :: String -> (Int, [Int])
|
||||||
|
parseLine str =
|
||||||
|
let src : dests : [] = splitOn " <-> " str
|
||||||
|
in (read src, map read $ splitOn ", " dests)
|
||||||
|
|
||||||
|
visit :: IntMap [Int] -> Int -> IntSet -> IntSet
|
||||||
|
visit hashmap node hashset =
|
||||||
|
let neighbours = filter (`notMember` hashset) $ findWithDefault [] node hashmap
|
||||||
|
in foldr (visit hashmap) (foldr insert hashset neighbours) neighbours
|
||||||
|
|
||||||
|
remove :: IntMap [Int] -> Int -> IntSet -> IntSet
|
||||||
|
remove hashmap node hashset =
|
||||||
|
let neighbours = filter (`member` hashset) $ findWithDefault [] node hashmap
|
||||||
|
in foldr (remove hashmap) (foldr delete hashset neighbours) neighbours
|
||||||
|
|
||||||
|
countGroups :: IntMap [Int] -> Int -> IntSet -> Int
|
||||||
|
countGroups hashmap count hashset = if S.null hashset then count else
|
||||||
|
countGroups hashmap (count + 1) $ remove hashmap (findMin hashset) hashset
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
keyvals <- fmap (map parseLine . lines) $ readFile "12.txt"
|
||||||
|
let hashkeys = S.fromList . fst . unzip $ keyvals
|
||||||
|
hashmap = M.fromList keyvals
|
||||||
|
print $ size $ visit hashmap 0 empty
|
||||||
|
print $ countGroups hashmap 0 hashkeys
|
14
2.hs
14
2.hs
|
@ -1,5 +1,7 @@
|
||||||
import Data.List
|
{-# LANGUAGE TupleSections #-}
|
||||||
import Data.Maybe
|
|
||||||
|
import Data.List (find)
|
||||||
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
checksum :: [[Int]] -> Int
|
checksum :: [[Int]] -> Int
|
||||||
checksum = sum . map (\line -> maximum line - minimum line)
|
checksum = sum . map (\line -> maximum line - minimum line)
|
||||||
|
@ -9,12 +11,8 @@ divsum = sum . map divline
|
||||||
|
|
||||||
divline :: [Int] -> Int
|
divline :: [Int] -> Int
|
||||||
divline ns =
|
divline ns =
|
||||||
let (n, m) = head $ catMaybes $ map (\x -> maybeTuplefy x $ find (greaterAndDivisible x) ns) ns
|
uncurry div . head . catMaybes . map (\x -> Just (x,) <*> find (greaterAndDivisible x) ns) $ ns
|
||||||
in n `div` m
|
where greaterAndDivisible n m = n > m && n `mod` m == 0
|
||||||
where greaterAndDivisible :: Int -> Int -> Bool
|
|
||||||
greaterAndDivisible n m = n > m && n `mod` m == 0
|
|
||||||
maybeTuplefy :: Int -> Maybe Int -> Maybe (Int, Int)
|
|
||||||
maybeTuplefy n mm = (,) <$> pure n <*> mm
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
|
Loading…
Reference in New Issue