6 our ($ship, %have, %price);
10 .../ypp-restock-rum [<prices>] [<ship> <stocks>]
12 <prices> is three arguments: the prices for swill, grog and fine rum
13 <ship> is a ship name or an amount of fine rum
14 <stocks> are amounts on board for swill, grog and fine rum
20 ($ship, $have{Swill}, $have{Grog}, $have{Fine}) = @ARGV;
22 compute_restock_requirements();
24 ($price{Swill}, $price{Grog}, $price{Fine}) = @ARGV;
26 compute_cheapest_rum();
28 ($price{Swill}, $price{Grog}, $price{Fine},
30 $have{Swill}, $have{Grog}, $have{Fine}) = @ARGV;
32 compute_cheapest_rum();
33 compute_restock_requirements();
34 compute_restock_cheapest_rum();
40 our @rums= qw(Swill Grog Fine);
41 our %proof= qw(Swill 40
48 my ($what, $format, $ary, $unit) = @_;
49 printf("%-40s", "$what:");
52 my $y= defined $x ? sprintf $format, $x : ' x';
55 printf " %s\n", $unit;
59 my ($what, $ary, $unit) = @_;
60 prvff($what, '%4d ', %$ary, $unit);
64 my ($what, $ary, $unit) = @_;
65 prvff($what, $ff, %$ary, $unit);
70 printf "%-20s %s\n", "$k:", $v;
75 sub compute_cheapest_rum() {
78 my $best_norm_price= 1e6;
80 next unless defined $price{$_};
81 $norm_price{$_} = $price{$_} * $proof{Fine} / $proof{$_};
82 if ($norm_price{$_} <= $best_norm_price) {
84 $best_norm_price= $norm_price{$_};
87 prf('normalised prices', %norm_price, 'poe/fine');
88 if (defined $best_kind) {
89 printf "best is %s\n\n", $best_kind;
91 die "no rum available ?\n";
99 map { printf " %5s ", $_ } @rums;
101 if (defined $price{Swill}) {
102 map { $price{$_}= undef if $price{$_} eq 'x' } @rums;
103 pr('prices', %price, 'poe ea.');
105 if (defined $have{Swill}) {
107 map { $have_proof += $have{$_} * $proof{$_} } @rums;
108 pr('stock on board', %have, 'rum');
112 our ($need_proof, %need, %buy);
114 sub compute_restock_requirements () {
115 if ($ship =~ m/^\d+/) {
119 open F, "/home/ian/private/puzzle-pirates" or die $!;
123 if (!m/\S/ || m/^\s*\#/) {
130 if (m/^\@(( [A-Z][-a-z]+){2,})\s*$/) {
131 $this_ship= (uc $1 eq uc " $ship" or
132 uc $+ eq uc " $ship");
136 next unless $this_ship;
137 if (m/^\@\s+(\d+)\s+fine\s*/) {
143 die "unknown ship $ship" unless defined $need{Fine};
145 pr1("vessel",$the_ship);
148 pr1('desired stock level', sprintf("%4d fine rum", $need{Fine}));
149 $need_proof= $need{Fine} * $proof{Fine} - $have_proof;
151 $buy{$_} = $need_proof / $proof{$_};
153 pr1("stock equivalent", sprintf "$ff fine rum", $have_proof / $proof{Fine});
154 pr1("restock equivalent", sprintf "$ff fine rum", $need_proof / $proof{Fine});
155 prf('would need', %buy, 'rum');
158 sub compute_restock_cheapest_rum() {
161 $bill{$_} = $buy{$_} * $price{$_} if defined $price{$_};
163 prf('nominal bill', %bill, 'poe');
165 if ($need_proof < 0) {
166 printf "stocks are sufficient";
168 my $buy= ceil($buy{$best_kind});
169 printf "buy %d %s at %d poe each for %d poe",
170 $buy, $best_kind, $price{$best_kind}, $buy * $price{$best_kind};