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 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 ns =
|
||||
let ms = drop (length ns `div` 2) ns ++ take (length ns `div` 2) ns
|
||||
nms = zip ns ms
|
||||
in foldr (\(n, m) sum -> if n == m then n + sum else sum) 0 nms
|
||||
in foldr (\(n, m) sum -> n * fromEnum (n == m) + sum) 0 $ zip ns ms
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- readFile "1.txt"
|
||||
let nums = catMaybes $ map (readMaybe . (:[])) input :: [Int]
|
||||
nums <- fmap (map (read . pure)) $ readFile "1.txt"
|
||||
print $ circularSum 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
|
16
2.hs
16
2.hs
|
@ -1,20 +1,18 @@
|
|||
import Data.List
|
||||
import Data.Maybe
|
||||
{-# LANGUAGE TupleSections #-}
|
||||
|
||||
import Data.List (find)
|
||||
import Data.Maybe (catMaybes)
|
||||
|
||||
checksum :: [[Int]] -> Int
|
||||
checksum = sum . map (\line -> maximum line - minimum line)
|
||||
|
||||
divsum :: [[Int]] -> Int
|
||||
divsum = sum . map divline
|
||||
divsum = sum . map divline
|
||||
|
||||
divline :: [Int] -> Int
|
||||
divline ns =
|
||||
let (n, m) = head $ catMaybes $ map (\x -> maybeTuplefy x $ find (greaterAndDivisible x) ns) ns
|
||||
in n `div` m
|
||||
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
|
||||
uncurry div . head . catMaybes . map (\x -> Just (x,) <*> find (greaterAndDivisible x) ns) $ ns
|
||||
where greaterAndDivisible n m = n > m && n `mod` m == 0
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
|
|
Loading…
Reference in New Issue