Created
December 22, 2016 17:15
-
-
Save rafalio/9a5b0c29d6d0b777a1fb2f8150a869e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.Either | |
import Text.Parsec | |
import Text.Parsec.String | |
import Control.Applicative hiding (many) | |
data Node = Node { | |
nCoords :: (Int, Int), | |
nSize :: Int, | |
nUsed :: Int, | |
nAvail :: Int, | |
nUse :: Int | |
} deriving (Eq, Read, Show) | |
main :: IO () | |
main = do | |
inp <- readFile "input22.txt" | |
let l = drop 2 $ lines inp | |
let res = fmap (parse parseLine "") l | |
putStrLn . show $ part1 (rights res) | |
--mapM_ (putStrLn . show) res | |
parseLine :: Parser Node | |
parseLine = do | |
string "/dev/grid/node-x" | |
xcoord <- integer <* string "-y" | |
ycoord <- integer <* spaces | |
size <- parseSizeWithChar 'T' <* spaces | |
used <- parseSizeWithChar 'T' <* spaces | |
avail <- parseSizeWithChar 'T' <* spaces | |
use <- parseSizeWithChar '%' | |
return (Node (xcoord, ycoord) size used avail use) | |
integer = read <$> many1 digit | |
parseSizeWithChar :: Char -> Parser Int | |
parseSizeWithChar c = integer <* char c | |
part1 :: [Node] -> Int | |
part1 ns = length $ [(a,b) | a <- ns, b <- ns, a /= b, nUsed a /= 0, nUsed a <= nAvail b] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment