1 module Main(main) where
4 import Text.ParserCombinators.Parsec.Number
8 text <- readFile "data/advent15.txt"
9 let disks = successfulParse $ parseIfile text
13 part1 :: [[Int]] -> IO ()
14 part1 disks = print $ head $ filter (canFall disks) [0..]
16 part2 :: [[Int]] -> IO ()
17 part2 disks = print $ head $ filter (canFall disks2) [0..]
18 where disks2 = disks ++ [drop 7 $ drop 0 $ cycle [0..(11-1)]]
20 canFall :: [[Int]] -> Int -> Bool
21 canFall ds i = all (\d -> (d!!i) == 0) ds
24 instructionFile = instructionLine `endBy` newline
25 instructionLine = diskify <$> (string "Disc #" *> int)
26 <*> (string " has " *> int)
27 <*> (string " positions; at time=0, it is at position " *> int)
29 where diskify n size pos0 = drop n $ drop pos0 $ cycle [0..(size-1)]
31 parseIfile :: String -> Either ParseError [[Int]]
32 parseIfile input = parse instructionFile "(unknown)" input
34 parseIline :: String -> Either ParseError [Int]
35 parseIline input = parse instructionLine "(unknown)" input
37 successfulParse :: Either ParseError [a] -> [a]
38 successfulParse (Left _) = []
39 successfulParse (Right a) = a