Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,22 +1,22 @@
import std.stdio, std.algorithm, std.typecons;
void main() @safe /*@nogc*/ {
import std.stdio, std.algorithm, std.typecons, std.conv;
struct Bounty {
int value;
double weight, volume;
}
static 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 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));
immutable maxPanacea = min(sack.weight / panacea.weight,
sack.volume / panacea.volume).to!int;
immutable maxIchor = min(sack.weight / ichor.weight,
sack.volume / ichor.volume).to!int;
immutable maxGold = min(sack.weight / gold.weight,
sack.volume / gold.volume).to!int;
Bounty best = {0, 0, 0};
Tuple!(int, int, int) bestAmounts;
@ -38,16 +38,14 @@ void main() {
if (current.value > best.value &&
current.weight <= sack.weight &&
current.volume <= sack.volume) {
best = Bounty(current.value,
current.weight,
current.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);
" panacea, %d ichor and %d gold", bestAmounts[]);
writefln("The weight to carry is %4.1f and the volume used is %5.3f",
best.weight, best.volume);
}

View file

@ -1,38 +1,26 @@
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);
import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
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));
alias Bounty = Tuple!(int,"value", double,"weight", double,"volume");
immutable best =
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 = min(sack.weight / panacea.weight, sack.volume / panacea.volume).to!int;
immutable maxIchor = min(sack.weight / ichor.weight, sack.volume / ichor.volume).to!int;
immutable maxGold = min(sack.weight / gold.weight, sack.volume / gold.volume).to!int;
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)
.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);
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][]);
writefln("The weight to carry is %4.1f and the volume used is %5.3f", best[0][1..$]);
}