Skip to content

Instantly share code, notes, and snippets.

@tekkoc
Created November 10, 2013 06:55
Show Gist options
  • Select an option

  • Save tekkoc/7394769 to your computer and use it in GitHub Desktop.

Select an option

Save tekkoc/7394769 to your computer and use it in GitHub Desktop.
最強最速アルゴリズマー養成講座P43より
import std.stdio;
import std.algorithm;
import std.range;
void main()
{
auto capacities = [20, 30];
auto bottles = [5, 8];
auto fromId = [0];
auto toId = [1];
writeln((new KiwiJuiceEasy()).thePouring(capacities, bottles, fromId, toId));
}
class KiwiJuiceEasy
{
int[] thePouring(int[] capacities, int[] bottles, int[] fromId, int[] toId)
{
foreach (ids; zip(fromId, toId)) {
auto sum = bottles[ids[0]] + bottles[ids[1]];
bottles[ids[1]] = min(sum, capacities[ids[1]]);
bottles[ids[0]] = sum - bottles[ids[1]];
}
return bottles;
}
unittest
{
auto capacities = [20, 30];
auto bottles = [5, 8];
auto fromId = [0];
auto toId = [1];
auto result = [0, 13];
assert(result == (new KiwiJuiceEasy()).thePouring(capacities, bottles, fromId, toId));
}
unittest
{
auto capacities = [10, 10];
auto bottles = [5, 8];
auto fromId = [0];
auto toId = [1];
auto result = [3, 10];
assert(result == (new KiwiJuiceEasy()).thePouring(capacities, bottles, fromId, toId));
}
unittest
{
auto capacities = [30, 20, 10];
auto bottles = [10, 5, 5];
auto fromId = [0, 1, 2];
auto toId = [1, 2, 0];
auto result = [10, 10, 0];
assert(result == (new KiwiJuiceEasy()).thePouring(capacities, bottles, fromId, toId));
}
unittest
{
auto capacities = [14, 35, 86, 58, 25, 62];
auto bottles = [6, 34, 27, 38, 9, 60];
auto fromId = [1, 2, 4, 5, 3, 3, 1, 0];
auto toId = [0, 1, 2, 4, 2, 5, 3, 1];
auto result = [0, 14, 65, 35, 25, 35];
assert(result == (new KiwiJuiceEasy()).thePouring(capacities, bottles, fromId, toId));
}
unittest
{
auto capacities = [700000, 800000, 900000, 1000000];
auto bottles = [478478, 478478, 478478, 478478];
auto fromId = [2, 3, 2, 0, 1];
auto toId = [0, 1, 1, 3, 2];
auto result = [0, 156956, 900000, 856956];
assert(result == (new KiwiJuiceEasy()).thePouring(capacities, bottles, fromId, toId));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment