X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=adventofcode1615%2Fapp%2Fadvent15l.hs;fp=adventofcode1615%2Fapp%2Fadvent15l.hs;h=67e2ac07e4a85d295765de50f6b42343b7818f24;hb=245aed74ce202a0be5cf2f61f79b48ed51aa0e62;hp=0000000000000000000000000000000000000000;hpb=474b47bdae540d9e3e3a31a6fd9fbaf450dcc395;p=advent-of-code-16.git diff --git a/adventofcode1615/app/advent15l.hs b/adventofcode1615/app/advent15l.hs new file mode 100644 index 0000000..67e2ac0 --- /dev/null +++ b/adventofcode1615/app/advent15l.hs @@ -0,0 +1,39 @@ +module Main(main) where + +import Text.Parsec +import Text.ParserCombinators.Parsec.Number + +main :: IO () +main = do + text <- readFile "data/advent15.txt" + let disks = successfulParse $ parseIfile text + part1 disks + part2 disks + +part1 :: [[Int]] -> IO () +part1 disks = print $ head $ filter (canFall disks) [0..] + +part2 :: [[Int]] -> IO () +part2 disks = print $ head $ filter (canFall disks2) [0..] + where disks2 = disks ++ [drop 7 $ drop 0 $ cycle [0..(11-1)]] + +canFall :: [[Int]] -> Int -> Bool +canFall ds i = all (\d -> (d!!i) == 0) ds + + +instructionFile = instructionLine `endBy` newline +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]] +parseIfile input = parse instructionFile "(unknown)" input + +parseIline :: String -> Either ParseError [Int] +parseIline input = parse instructionLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a