From: Neil Smith Date: Thu, 15 Dec 2016 11:05:06 +0000 (+0000) Subject: Renamed the versions X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=42fed81078c6babc676774a0c2816d49209f7953;p=advent-of-code-16.git Renamed the versions --- diff --git a/advent15.hs b/advent15.hs index e3c97fb..31866c0 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,16 @@ 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 ++ [(\i -> (11 + 7 + 0 + i) `mod` 11 == 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 +27,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] diff --git a/advent15f.hs b/advent15f.hs deleted file mode 100644 index 31866c0..0000000 --- a/advent15f.hs +++ /dev/null @@ -1,42 +0,0 @@ -import Text.Parsec -import Text.ParserCombinators.Parsec.Number - -type Disk = (Int -> Bool) - -main :: IO () -main = do - text <- readFile "advent15.txt" - let disks = successfulParse $ parseIfile text - part1 disks - part2 disks - -part1 :: [Disk] -> IO () -part1 disks = print $ head $ filter (canFall disks) [0..] - -part2 :: [Disk] -> IO () -part2 disks = print $ head $ filter (canFall disks2) [0..] - -- where disks2 = disks ++ [(\i -> (11 + 7 + 0 + i) `mod` 11 == 0)] - where disks2 = disks ++ [diskify 7 11 0] - -canFall :: [Disk] -> Int -> Bool -canFall ds i = all (\d -> (d i)) ds - - -instructionFile = instructionLine `endBy` newline -instructionLine = diskify <$> (string "Disc #" *> int) - <*> (string " has " *> int) - <*> (string " positions; at time=0, it is at position " *> int) - <* (string ".") - -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 Disk -parseIline input = parse instructionLine "(unknown)" input - -successfulParse :: Either ParseError [a] -> [a] -successfulParse (Left _) = [] -successfulParse (Right a) = a diff --git a/advent15l.hs b/advent15l.hs new file mode 100644 index 0000000..e3c97fb --- /dev/null +++ b/advent15l.hs @@ -0,0 +1,37 @@ +import Text.Parsec +import Text.ParserCombinators.Parsec.Number + +main :: IO () +main = do + text <- readFile "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..5000000] + where disks2 = id $! map (take 5000000) $ 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