#!/usr/local/bin/perl # Version: 1.0 # Author: Paul E. Zaremba III # Inspired by: Brent N. Reeves Riddles page at # http://sracog.srainc.com/~brentr/riddles.html # Contact the author at: samiel_@hotmail.com # This tests the 12 Weights riddle in the proper format # DISCLAIMER: # I am not responsible for any damages caused through misuse or modification # of this program, if you don't agree with this, then don't use the program. # This program is by no means pretty, and I haven't commented it, but I may # do so in the future. This is "Free" software, no royalty or any other # such thing may be charged for it, if you don't agree, don't use it. # Feel free to send me any bug reports, etc. at the above mentioned # email address, before anyone asks, no I will not give out the # answer to the Trouble in River City riddle (or any other riddle on # the riddles page), it's fun to figure them out, especially with the help # of a friend. # This is just a quick little perl hack to help in testing the answer # to the riddle on Brent N. Reeves riddles page. if (!@ARGV || (!(-e @ARGV[0]) and {print "\nErr: @ARGV[0] does not exist!\n\n"})) { print < $0 use: $0 file This perl script emulates a scale and reads the format specified on Brent N. Reeves "Trouble in River City" riddle, at http://sracog.srainc.com/~brentr/riddle-12.html The L/R/= order doesn't matter as long as they line up appropriately For great riddles, go to Brent N. Reeves riddles page at: http://sracog.srainc.com/~brentr/riddles.html EndOfUse exit; } open INPUT, "<@ARGV[0]"; @Data = grep /^\d+\./, ; chomp(@Data); close INPUT; foreach $item (@Data) { $item =~ s/^\d+\.\s*//; } sub Weigh_Balls { sub comp { (my $comp1, my $comp2, my $num) = @_; $comp1 =~ s/\d+[\.RrLl=]//g; if ($comp1 =~ m/\s$num\s|\s$num$/) { if ($weight eq "H") { $ret = "L"; } else { $ret = "R"; } } elsif ($comp2 =~ m/\s$num\s|\s$num$/) { if ($weight eq "H") { $ret = "R"; } else { $ret = "L"; } } else { $ret = "="; } return $ret; } (my $number, $weight) = @_; $weigh = 1; $line = 0; $lines = "1"; @compare = split(/:/, @Data[0]); $LoR = comp(@compare[0],@compare[1],$number); while (!(@Data[++$line] =~ m/$weigh$LoR\s+(.*)$/)) {}; $lines = $lines.$LoR."\t"; ++$weigh; @compare = split(/:/, @Data[$line]); $LoR = comp(@compare[0], @compare[1], $number); while (!(@Data[++$line] =~ m/$weigh$LoR\s+(.*)$/)) {}; $lines = $lines.$weigh.$LoR."\t"; ++$weigh; @compare = split(/:/, @Data[$line]); $LoR = comp(@compare[0], @compare[1], $number); $lines = $lines.$weigh.$LoR; while (!(@Data[++$line] =~ m/$weigh$LoR\s+(.*)$/)) {}; if (@Data[$line++] =~ m/$number\s\->\s$weight/) { print "$number$weight passed"; } else { print "$number$weight failed"; exit; } print "\tResults \t( $lines )\n"; } $MAX_BALLS = 12; @Weight = ("H","L"); $| = 1; # for those looking at the script this turns line buffering off for # STDOUT for ($i = 1; $i <= 12; ++$i) { foreach $w (@Weight) { Weigh_Balls($i,$w); } } print "Passed all tests, Congratulations!\n"; print "press enter to quit"; $temp = ;