Day 12, plus some edits to days 1 and 2

This commit is contained in:
Jonathan Chan 2017-12-11 22:43:01 -08:00
parent 1078b40201
commit a90a59c846
5 changed files with 2043 additions and 18 deletions

11
1.hs
View File

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

2
1.txt
View File

@ -1 +1 @@
77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277
77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277

32
12.hs Normal file
View File

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

2000
12.txt Normal file

File diff suppressed because it is too large Load Diff

16
2.hs
View File

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