A Wiki in the Desert
Log in

User:Beren/Scripts/Glassware

From A Wiki in the Desert
#!/usr/bin/perl

use strict;
use warnings;

# Constants for the individual glassware names
my $beaker = 'Beaker';
my $distillation_coil = 'Distillation Coil';
my $florence_flask = 'Florence Flask';
my $test_tube = 'Test Tube';
my $thistle_tube = 'Thistle Tube';


# Data structure to hold all input glassware
my %candidates = (
	$beaker => [],
	$distillation_coil => [],
	$test_tube => [],
	$thistle_tube => [],
	$florence_flask => []
);

# Found combinations which should result in successful calibration
my %matches = ();

# Parse candidate glassware out of input
while (<>) {
	if (/($distillation_coil|$beaker|$test_tube|$thistle_tube|$florence_flask):Quality (\d+)/) {
		# Found glassware, register as a candidate
		push @{$candidates{$1}}, $2; 
	}
}

# Work through all combinations of each type
foreach my $ff (@{$candidates{$florence_flask}}) {
	foreach my $tt (@{$candidates{$test_tube}}) {
		foreach my $tht (@{$candidates{$thistle_tube}}) {
			foreach my $b (@{$candidates{$beaker}}) {
				foreach my $dc (@{$candidates{$distillation_coil}}) {
					my $sum = $ff + $tt + $tht + $b + $dc;

					# Skip low quality combinations
					next if $sum < 35000;

					# Check for calibration level
					my $calibration_level = 0;

					if (($sum >= 40000 && $sum <= 40004) || ($sum >= 45000 && $sum <= 45004)) {
						# Match for level 3 calibration
						$calibration_level = 3;
					}
					if ($sum >= 35000 && $sum <= 35004) {
						# Match for level 2 calibration
						$calibration_level = 2;
					}
					if ($sum % 100 == 0) {
						# Match for level 1 calibration
						$calibration_level = 1;
					}

					# Store results
					if ($calibration_level > 0) {
						my $set = {$beaker => $b, $distillation_coil => $dc, $florence_flask => $ff, $test_tube => $tt, $thistle_tube => $tht, 'Sum' => $sum};
						$matches{$calibration_level} //= [];
						push @{$matches{$calibration_level}}, $set;
					}
				}
			}
		}
	}
}

# Output any successful combinations
for my $calibration_level (sort keys %matches) {
	print "\nCalibration Level $calibration_level:\n";
	foreach my $set (@{$matches{$calibration_level}}) {
		print "Sum: ${$set}{'Sum'} Beaker: ${$set}{$beaker} Distillation Coil: ${$set}{$distillation_coil} Florence Flask: ${$set}{$florence_flask} Test Tube: ${$set}{$test_tube} Thistle Tube: ${$set}{$thistle_tube}\n";
	}
}