September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,40 +1,49 @@
|
|||
import Control.Monad
|
||||
import Data.List (sortBy)
|
||||
import Data.Ord (comparing)
|
||||
import Text.Printf (printf)
|
||||
import Control.Monad (forM_)
|
||||
import Data.Ratio (numerator, denominator)
|
||||
import Text.Printf
|
||||
|
||||
maxWgt :: Rational
|
||||
maxWgt = 15
|
||||
|
||||
data Bounty = Bounty
|
||||
{itemName :: String,
|
||||
itemVal, itemWgt :: Rational}
|
||||
{ itemName :: String
|
||||
, itemVal, itemWgt :: Rational
|
||||
}
|
||||
|
||||
items :: [Bounty]
|
||||
items =
|
||||
[Bounty "beef" 36 3.8,
|
||||
Bounty "pork" 43 5.4,
|
||||
Bounty "ham" 90 3.6,
|
||||
Bounty "greaves" 45 2.4,
|
||||
Bounty "flitch" 30 4.0,
|
||||
Bounty "brawn" 56 2.5,
|
||||
Bounty "welt" 67 3.7,
|
||||
Bounty "salami" 95 3.0,
|
||||
Bounty "sausage" 98 5.9]
|
||||
[ Bounty "beef" 36 3.8
|
||||
, Bounty "pork" 43 5.4
|
||||
, Bounty "ham" 90 3.6
|
||||
, Bounty "greaves" 45 2.4
|
||||
, Bounty "flitch" 30 4.0
|
||||
, Bounty "brawn" 56 2.5
|
||||
, Bounty "welt" 67 3.7
|
||||
, Bounty "salami" 95 3.0
|
||||
, Bounty "sausage" 98 5.9
|
||||
]
|
||||
|
||||
solution :: [(Rational, Bounty)]
|
||||
solution = g maxWgt $ sortBy (flip $ comparing f) items
|
||||
where g room (b@(Bounty _ _ w) : bs) = if w < room
|
||||
then (w, b) : g (room - w) bs
|
||||
else [(room, b)]
|
||||
f (Bounty _ v w) = v / w
|
||||
where
|
||||
g room (b@(Bounty _ _ w):bs) =
|
||||
if w < room
|
||||
then (w, b) : g (room - w) bs
|
||||
else [(room, b)]
|
||||
f (Bounty _ v w) = v / w
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
forM_ solution $ \(w, b) ->
|
||||
printf "%s kg of %s\n" (mixedNum w) (itemName b)
|
||||
printf "Total value: %s\n" $ mixedNum $ sum $ map f solution
|
||||
where f (w, Bounty _ v wtot) = v * (w / wtot)
|
||||
mixedNum q = if b == 0
|
||||
then show a
|
||||
else printf "%d %d/%d" a (numerator b) (denominator b)
|
||||
where a = floor q
|
||||
b = q - toEnum a
|
||||
forM_ solution $ \(w, b) -> printf "%s kg of %s\n" (mixedNum w) (itemName b)
|
||||
(printf "Total value: %s\n" . mixedNum . sum) $ f <$> solution
|
||||
where
|
||||
f (w, Bounty _ v wtot) = v * (w / wtot)
|
||||
mixedNum q =
|
||||
if b == 0
|
||||
then show a
|
||||
else printf "%d %d/%d" a (numerator b) (denominator b)
|
||||
where
|
||||
a = floor q
|
||||
b = q - toEnum a
|
||||
|
|
|
|||
|
|
@ -3,21 +3,24 @@ import Data.Ord (comparing)
|
|||
import Text.Printf (printf)
|
||||
|
||||
-- (name, (value, weight))
|
||||
items = [("beef", (36, 3.8)),
|
||||
("pork", (43, 5.4)),
|
||||
("ham", (90, 3.6)),
|
||||
("greaves", (45, 2.4)),
|
||||
("flitch", (30, 4.0)),
|
||||
("brawn", (56, 2.5)),
|
||||
("welt", (67, 3.7)),
|
||||
("salami", (95, 3.0)),
|
||||
("sausage", (98, 5.9))]
|
||||
items =
|
||||
[ ("beef", (36, 3.8))
|
||||
, ("pork", (43, 5.4))
|
||||
, ("ham", (90, 3.6))
|
||||
, ("greaves", (45, 2.4))
|
||||
, ("flitch", (30, 4.0))
|
||||
, ("brawn", (56, 2.5))
|
||||
, ("welt", (67, 3.7))
|
||||
, ("salami", (95, 3.0))
|
||||
, ("sausage", (98, 5.9))
|
||||
]
|
||||
|
||||
unitWeight (_, (val, weight)) = (fromIntegral val) / weight
|
||||
unitWeight (_, (val, weight)) = fromIntegral val / weight
|
||||
|
||||
solution k = loop k . sortBy (flip $ comparing unitWeight)
|
||||
where loop k ((name, (_, weight)):xs)
|
||||
| weight < k = putStrLn ("Take all the " ++ name) >> loop (k-weight) xs
|
||||
| otherwise = printf "Take %.2f kg of the %s\n" (k :: Float) name
|
||||
where
|
||||
loop k ((name, (_, weight)):xs)
|
||||
| weight < k = putStrLn ("Take all the " ++ name) >> loop (k - weight) xs
|
||||
| otherwise = printf "Take %.2f kg of the %s\n" (k :: Float) name
|
||||
|
||||
main = solution 15 items
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue