X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent15.hs;h=27793c14b58618c03598a7d1d09e22d7345cf6cf;hb=e78bf6ce47d6ce753313cfe6a7d669e10b716d66;hp=e3c97fb2cb92f263dd68e7c361ab70e4ee25ab2b;hpb=a14889eed39a71079594da4f4fe1a20adea4df3a;p=advent-of-code-16.git diff --git a/advent15.hs b/advent15.hs index e3c97fb..27793c1 100644 --- a/advent15.hs +++ b/advent15.hs @@ -1,6 +1,8 @@ import Text.Parsec import Text.ParserCombinators.Parsec.Number +type Disk = (Int -> Bool) + main :: IO () main = do text <- readFile "advent15.txt" @@ -8,15 +10,15 @@ main = do part1 disks part2 disks -part1 :: [[Int]] -> IO () +part1 :: [Disk] -> IO () part1 disks = print $ head $ filter (canFall disks) [0..] -part2 :: [[Int]] -> IO () -part2 disks = print $ head $ filter (canFall disks2) [0..5000000] - where disks2 = id $! map (take 5000000) $ disks ++ [drop 7 $ drop 0 $ cycle [0..(11-1)]] +part2 :: [Disk] -> IO () +part2 disks = print $ head $ filter (canFall disks2) [0..] + where disks2 = disks ++ [diskify 7 11 0] -canFall :: [[Int]] -> Int -> Bool -canFall ds i = all (\d -> (d!!i) == 0) ds +canFall :: [Disk] -> Int -> Bool +canFall ds i = all (\d -> (d i)) ds instructionFile = instructionLine `endBy` newline @@ -24,12 +26,14 @@ instructionLine = diskify <$> (string "Disc #" *> int) <*> (string " has " *> int) <*> (string " positions; at time=0, it is at position " *> int) <* (string ".") - where diskify n size pos0 = drop n $ drop pos0 $ cycle [0..(size-1)] -parseIfile :: String -> Either ParseError [[Int]] +diskify :: Int -> Int -> Int -> (Int -> Bool) +diskify n size pos0 = (\i -> (size + n + pos0 + i) `mod` size == 0) + +parseIfile :: String -> Either ParseError [Disk] parseIfile input = parse instructionFile "(unknown)" input -parseIline :: String -> Either ParseError [Int] +parseIline :: String -> Either ParseError Disk parseIline input = parse instructionLine "(unknown)" input successfulParse :: Either ParseError [a] -> [a]