diff options
Diffstat (limited to 'day13/day13.hs')
-rw-r--r-- | day13/day13.hs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/day13/day13.hs b/day13/day13.hs new file mode 100644 index 0000000..4ca7b65 --- /dev/null +++ b/day13/day13.hs @@ -0,0 +1,57 @@ +import Data.List +import Data.List.Split +import Data.Maybe +import Text.Read + +type Cost = Int +type Vec = (Int, Int) +type Machine = (Vec, Vec, Vec) + +mul :: Int -> Vec -> Vec +mul s (x, y) = (s*x, s*y) +add :: Vec -> Vec -> Vec +add (x, y) (z, w) = (x+z, y+w) + +consume1 :: [String] -> Machine +consume1 (a:b:p:_) = + (parseVec "+," a, parseVec "+," b, parseVec "=," p) + where + toVec [x, y] = (x,y) + parseVec spl str = toVec $ catMaybes $ map (readMaybe::String->Maybe Int) $ splitOneOf spl str + +consume :: String -> [Machine] +consume file = + map consume1 macs + where + macs = chunksOf 4 $ lines file + +winnable :: Machine -> [Vec] +winnable (a, b, p) = + filter winner opts + where + winner (x, y) = (x `mul` a) `add` (y `mul` b) == p + opts = [(na, nb) | na <- [0..100], nb <- [0..100]] + +cost :: Vec -> Cost +cost (a, b) = 3*a + b + +cheapest1 :: Cost -> [Vec] -> Cost +cheapest1 cst [] = cst +cheapest1 cst (h:t) + | hcst < cst = cheapest1 hcst t + | otherwise = cheapest1 cst t + where + hcst = cost h + +cheapest :: [Vec] -> Cost +cheapest [] = 0 +cheapest v = cheapest1 (maxBound :: Int) v + +part1 :: [Machine] -> Cost +part1 = sum . map cheapest . map winnable + +main = do + file <- readFile "day13.input" + let machines = consume file + putStrLn ("Part 1: " ++ (show $ part1 machines)) + --putStrLn ("Part 2: " ++ (show $ part2 machines)) |