8 .../ypp-restock-rum [<information> ...]
10 where <information> is
12 have [<swill> <grog>] <fine> [<hold_shot>+<loaded_shot>]
13 want [<swill> <grog>] <fine> [<shot>]
14 price <swill> <grog> <fine> [<shot>]
16 Each of which may appear only once, except \`have' which may appear
17 more than once which case we calculate the differences and the profit
20 If /<shot> is not specified at all, relevant information about shot is
21 not reported. For have and want, specifying an empty string means
22 zero. Entirely Missing <swill>/<grog>/ is taken as if both were
25 In price, missing entries mean the commodity is unavailable.
29 our (@kinds) = qw(Swill Grog Fine Shot);
30 our (@proofs) = qw(40 60 100);
32 sub parse_info ($$$\@$) {
33 my ($omitswgok,$default,$multishot, $storeary, $what) = @_;
34 @ARGV or badusage("missing value for information argument \`$_'");
35 badusage("$what: specified more than once")
36 if defined $storeary->[2];
38 while (@ARGV and $ARGV[0] =~ m/^\d/) {
43 badusage("$what: swill and grog amounts must be specified")
45 @v=($default,$default,@v);
47 if ($multishot and @v==4 and length $v[3]) {
48 $v[3] =~ m/^0*(\d+)\+0*(\d+)$/ or
49 badusage("$what: shot must be specified as <hold>+<loaded>");
56 badusage("$what: invalid syntax (wrong number of /s)");
60 $_ = $default if !length;
61 m/^0*(\d+)$/ or badusage("$what: $kinds[$i] \`$_': bad syntax");
68 our (@have,@want,@price);
71 @ARGV or badusage("need some information to go on");
75 parse_info(1,0,1, @{ $have[@have] }, 'have');
77 parse_info(1,0,0, @want, 'want');
78 } elsif (m/^price$/) {
79 parse_info(0,1e6,0, @price, 'price');
81 badusage("unknown information argument \`$_'");
88 print STDERR "\nbad usage: $m\n\n$usage\n";
96 defined $x and $x>0 and $x<1e4;
100 my ($what, $format, $ary, $unit) = @_;
101 printf("%-25s", "$what:");
102 for my $i (qw(0 1 2 3)) {
104 my $y= valid($x) ? sprintf $format, $x : ' ';
107 printf " %s\n", $unit;
111 my ($what, $ary, $unit) = @_;
112 prvff($what, '%4d ', @$ary, $unit);
116 my ($what, $ary, $unit) = @_;
117 prvff($what, $ff, @$ary, $unit);
122 printf "%-20s %s\n", "$k:", $v;
127 our ($best, $best_norm_price);
129 sub print_inputs () {
131 map { printf " %5s ", $_ } @kinds;
133 pr('prices', @price, 'poe ea.') if valid(@price);
134 pr('target stocks', @want, 'units') if valid(@want);
135 my $i=0; for my $stocks (@have) {
136 pr('actual stocks'.(@have==1 ? '' : ' #'.++$i),
142 sub compute_cheapest_rum() {
143 return unless @price;
145 my (@perorder) = map { $_*10 } @price;
146 prf('equiv. ordering price', @perorder, 'poe/order');
149 my $best_norm_price= 1e5;
150 for my $i (qw(0 1 2)) {
151 next unless $price[$i];
152 $norm_price[$i] = $price[$i] * 100 / $proofs[$i];
153 if ($norm_price[$i] <= $best_norm_price) {
155 $best_norm_price= $norm_price[$i];
158 prf('normalised prices', @norm_price, 'poe/fine');
161 printf "best is %-10s%*s^^\n",
164 my (@bestperorder) = map {
165 $best_norm_price * $proofs[$_] / 100 * 10;
167 #push @bestperorder, $perorder[3];
168 prf('best is equiv. ordering', @bestperorder, 'poe/order');
175 compute_cheapest_rum();
179 # if (defined $price{Swill}) {
180 # map { $price{$_}= undef if $price{$_} eq 'x' } @rums;
182 # if (defined $have{Swill}) {
184 # map { $have_proof += $have{$_} * $proof{$_} } @rums;
191 our ($need_proof, %need, %buy);
193 sub compute_restock_requirements () {
194 if ($ship =~ m/^\d+/) {
198 open F, "/home/ian/private/puzzle-pirates" or die $!;
202 if (!m/\S/ || m/^\s*\#/) {
209 if (m/^\@(( [A-Z][-a-z]+){2,})\s*$/) {
210 $this_ship= (uc $1 eq uc " $ship" or
211 uc $+ eq uc " $ship");
215 next unless $this_ship;
216 if (m/^\@\s+(\d+)\s+fine\s*/) {
222 die "unknown ship $ship" unless defined $need{Fine};
224 pr1("vessel",$the_ship);
227 pr1('desired stock level', sprintf("%4d fine rum", $need{Fine}));
228 $need_proof= $need{Fine} * $proof{Fine} - $have_proof;
230 $buy{$_} = $need_proof / $proof{$_};
232 pr1("stock equivalent", sprintf "$ff fine rum", $have_proof / $proof{Fine});
233 pr1("restock equivalent", sprintf "$ff fine rum", $need_proof / $proof{Fine});
234 prf('would need', %buy, 'rum');
237 sub compute_restock_cheapest_rum() {
240 $bill{$_} = $buy{$_} * $price{$_} if defined $price{$_};
242 prf('nominal bill', %bill, 'poe');
244 if ($need_proof < 0) {
245 printf "stocks are sufficient";
247 my $buy= ceil($buy{$best});
248 printf "buy %d %s at %d poe each for %d poe",
249 $buy, $best, $price{$best}, $buy * $price{$best};