view parsealloys.pl @ 15:79c79d865218 misc

Add a simple Perl-script for parsing alloy combinations from logfiles.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Apr 2009 12:40:12 +0000
parents
children f9376ab3b366
line wrap: on
line source

#!/usr/bin/perl -w
# Superklunssi by Ggr
use strict;

#You mix obsidian and crystal and create a quantity of glass
#You mix nullium and illumium and create a quantity of dukonium
#You mix nullium and illumium and create a quantity of dukonium

my ($opt_php);

if ($#ARGV >= 0) {
  if ($ARGV[0] eq "-php") { $opt_php = 1; }
}   


## Parse data from input, ignoring fumbled alloys
my %data = ();
my $fumble = 0;
while (<STDIN>) {
  chomp;
  if (/^You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of (.+)$/) {
    my $min1 = $1; my $min2 = $2; my $res = $3;
    if (!$fumble) {
      push(@{$data{$1}{$2}}, $3);
    } else {
      print STDERR "Ignoring fumbled combo $1 + $2 = $3\n";
    }
    $fumble = 0;
  } elsif (/^(You slip up and fumble your attempt.)/) {
    $fumble = 1;
  }
}


## Re-format the data into single "ordered" hash, using as
## close to alphabetical order as possible.
my %final = ();
foreach my $min1 (keys %data) {
  foreach my $min2 (keys %{$data{$min1}}) {
    if (!defined($final{$min1}{$min2}) && !defined($final{$min2}{$min1})) {
      # Get both (a, b) and (b, a) combinations results into same array
      my @tmp = ();
      if (defined($data{$min1}{$min2})) {
        push(@tmp, @{$data{$min1}{$min2}});
      }
      if (defined($data{$min2}{$min1})) {
        push(@tmp, @{$data{$min2}{$min1}});
      }
      
      # Determine if there are multiple results and which is the most likely.
      # Some alloy combinations are inherently "unstable".
      my %seen = ();
      foreach my $mat (@tmp) { $seen{$mat}++; }
      if ($min1 lt $min2) {
        $final{$min1}{$min2} = \%seen;
      } else {
        $final{$min2}{$min1} = \%seen;
      }
    }
  }
}


## Output the results
print
"| Mineral1        | Mineral 2       | Result(s)\n".
"+-----------------+-----------------+--------------------------------\n";
foreach my $min1 (sort { $a cmp $b } keys %final) {
  foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}}) {
    printf "| %-15s | %-15s | ", $min1, $min2;
    foreach my $res (sort { $_{$a} <=> $_{$b} } keys %{$final{$min1}{$min2}}) {
      print "$res($final{$min1}{$min2}{$res}) ";
    }
    print "\n";
  }
}