This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1,53 @@
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};
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));
Bounty best = {0, 0, 0};
Tuple!(int, int, int) bestAmounts;
foreach (immutable nPanacea; 0 .. maxPanacea)
foreach (immutable nIchor; 0 .. maxIchor)
foreach (immutable nGold; 0 .. maxGold) {
immutable Bounty current = {
value: nPanacea * panacea.value +
nIchor * ichor.value +
nGold * gold.value,
weight: nPanacea * panacea.weight +
nIchor * ichor.weight +
nGold * gold.weight,
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

@ -0,0 +1,38 @@
import std.stdio, std.algorithm, std.typecons, std.range;
alias Bounty = Tuple!(int,"value", double,"weight", double,"volume");
void main() {
immutable panacea = Bounty(3000, 0.3, 0.025);
immutable ichor = Bounty(1800, 0.2, 0.015);
immutable gold = Bounty(2500, 2.0, 0.002);
immutable sack = Bounty( 0, 25.0, 0.25);
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));
immutable best =
cartesianProduct(maxPanacea.iota, maxIchor.iota, maxGold.iota)
.map!(t => tuple(Bounty(t[0] * panacea.value
+ t[1] * ichor.value
+ t[2] * gold.value,
t[0] * panacea.weight
+ t[1] * ichor.weight
+ t[2] * gold.weight,
t[0] * panacea.volume
+ t[1] * ichor.volume
+ t[2] * gold.volume), t))
.filter!(t => t[0].weight <= sack.weight
&& t[0].volume <= sack.volume)
.reduce!max;
writeln("Maximum value achievable is ", best[0].value);
writefln("This is achieved by carrying (one solution) %d" ~
" panacea, %d ichor and %d gold", best[1].tupleof);
writefln("The weight to carry is %4.1f and the" ~
" volume used is %5.3f", best[0].weight, best[0].volume);
}

View file

@ -0,0 +1,51 @@
$ include "seed7_05.s7i";
include "float.s7i";
const type: bounty is new struct
var integer: value is 0;
var float: weight is 0.0;
var float: volume is 0.0;
end struct;
const func bounty: bounty (in integer: value, in float: weight, in float: volume) is func
result
var bounty: bountyVal is bounty.value;
begin
bountyVal.value := value;
bountyVal.weight := weight;
bountyVal.volume := volume;
end func;
const proc: main is func
local
const bounty: panacea is bounty(3000, 0.3, 0.025);
const bounty: ichor is bounty(1800, 0.2, 0.015);
const bounty: gold is bounty(2500, 2.0, 0.002);
const bounty: sack is bounty(0, 25.0, 0.25);
const integer: maxPanacea is trunc(min(sack.weight / panacea.weight, sack.volume / panacea.volume));
const integer: maxIchor is trunc(min(sack.weight / ichor.weight, sack.volume / ichor.volume));
const integer: maxGold is trunc(min(sack.weight / gold.weight, sack.volume / gold.volume));
var bounty: current is bounty.value;
var bounty: best is bounty.value;
var array integer: bestAmounts is 3 times 0;
var integer: numPanacea is 0;
var integer: numIchor is 0;
var integer: numGold is 0;
begin
for numPanacea range 0 to maxPanacea do
for numIchor range 0 to maxIchor do
for numGold range 0 to maxGold do
current.value := numGold * gold.value + numIchor * ichor.value + numPanacea * panacea.value;
current.weight := flt(numGold) * gold.weight + flt(numIchor) * ichor.weight + flt(numPanacea) * panacea.weight;
current.volume := flt(numGold) * gold.volume + flt(numIchor) * ichor.volume + flt(numPanacea) * panacea.volume;
if current.value > best.value and current.weight <= sack.weight and current.volume <= sack.volume then
best := current;
bestAmounts := [] (numPanacea, numIchor, numGold);
end if;
end for;
end for;
end for;
writeln("Maximum value achievable is " <& best.value);
writeln("This is achieved by carrying " <& bestAmounts[1] <& " panacea, " <& bestAmounts[2] <& " ichor and " <& bestAmounts[3] <& " gold items");
writeln("The weight of this carry is " <& best.weight <& " and the volume used is " <& best.volume digits 4);
end func;