commit deletes

This commit is contained in:
Ingy döt Net 2013-10-27 23:48:49 +00:00
parent 776bba907c
commit 372c577f83
233 changed files with 0 additions and 6724 deletions

View file

@ -1,53 +0,0 @@
import std.stdio, std.algorithm, std.typecons;
struct Bounty {
int value;
double weight, volume;
}
void main() {
immutable Bounty panacea = {3000, 0.3, 0.025};
immutable Bounty ichor = {1800, 0.2, 0.015};
immutable Bounty gold = {2500, 2.0, 0.002};
immutable Bounty sack = { 0, 25.0, 0.25};
Bounty best = {0, 0.0, 0.0};
Bounty current = {0, 0.0, 0.0};
Tuple!(int, int, int) bestAmounts;
immutable maxPanacea = cast(int)(min(sack.weight / panacea.weight,
sack.volume / panacea.volume));
immutable maxIchor = cast(int)(min(sack.weight / ichor.weight,
sack.volume / ichor.volume));
immutable maxGold = cast(int)(min(sack.weight / gold.weight,
sack.volume / gold.volume));
foreach (nPanacea; 0 .. maxPanacea)
foreach (nIchor; 0 .. maxIchor)
foreach (nGold; 0 .. maxGold) {
current.value = nPanacea * panacea.value +
nIchor * ichor.value +
nGold * gold.value;
current.weight = nPanacea * panacea.weight +
nIchor * ichor.weight +
nGold * gold.weight;
current.volume = nPanacea * panacea.volume +
nIchor * ichor.volume +
nGold * gold.volume;
if (current.value > best.value &&
current.weight <= sack.weight &&
current.volume <= sack.volume) {
best = Bounty(current.value,
current.weight,
current.volume);
bestAmounts = tuple(nPanacea, nIchor, nGold);
}
}
writeln("Maximum value achievable is ", best.value);
writefln("This is achieved by carrying (one solution) %d" ~
" panacea, %d ichor and %d gold", bestAmounts.tupleof);
writefln("The weight to carry is %4.1f and the volume used is %5.3f",
best.weight, best.volume);
}

View file

@ -1,22 +0,0 @@
# Define consts
weights <- c(panacea=0.3, ichor=0.2, gold=2.0)
volumes <- c(panacea=0.025, ichor=0.015, gold=0.002)
values <- c(panacea=3000, ichor=1800, gold=2500)
sack.weight <- 25
sack.volume <- 0.25
max.items <- floor(pmin(sack.weight/weights, sack.volume/volumes))
# Some utility functions
getTotalValue <- function(n) sum(n*values)
getTotalWeight <- function(n) sum(n*weights)
getTotalVolume <- function(n) sum(n*volumes)
willFitInSack <- function(n) getTotalWeight(n) <= sack.weight && getTotalVolume(n) <= sack.volume
# Find all possible combination, then eliminate those that won't fit in the sack
knapsack <- expand.grid(lapply(max.items, function(n) seq.int(0, n)))
ok <- apply(knapsack, 1, willFitInSack)
knapok <- knapsack[ok,]
# Find the solutions with the highest value
vals <- apply(knapok, 1, getTotalValue)
knapok[vals == max(vals),]