Question 5: Another Perl solution
#!/usr/bin/perl
use strict;
use warnings;
use 5.014;
use Data::Dump qw(dump);
use File::Slurp;
my %lines_for;
sub match {
my ($a, $b) = @_;
my $alines = $lines_for{$a} ||= [
read_file('contestants/contestant' . sprintf('%02d', $a))
];
my $blines = $lines_for{$b} ||= [
read_file('contestants/contestant' . sprintf('%02d', $b))
];
shift @$alines if $alines->[0] =~ /Name/;
shift @$blines if $blines->[0] =~ /Name/;
while ($alines->[0] eq $blines->[0]) {
shift @$alines;
shift @$blines;
}
my $ac = $alines->[0];
chomp $ac;
my $bc = $blines->[0];
chomp $bc;
shift @$alines;
shift @$blines;
if ($ac eq 'scissors') {
if ($bc eq 'rock') {
return $b;
}
else {
return $a;
}
}
if ($ac eq 'rock') {
if ($bc eq 'paper') {
return $b;
}
else {
return $a;
}
}
if ($ac eq 'paper') {
if ($bc eq 'scissors') {
return $b;
}
else {
return $a;
}
}
}
my @result = ();
for my $i (1..32) {
push @result, match(2*$i - 1, 2*$i);
}
my @input = @result;
@result = ();
say '--';
while (@input) {
push @result, match(shift @input, shift @input);
}
say dump(\@result);
@input = @result;
@result = ();
say '--';
while (@input) {
push @result, match(shift @input, shift @input);
}
say dump(\@result);
@input = @result;
@result = ();
say '--';
while (@input) {
push @result, match(shift @input, shift @input);
}
say dump(\@result);
@input = @result;
@result = ();
say '--';
while (@input) {
push @result, match(shift @input, shift @input);
}
say dump(\@result);
@input = @result;
@result = ();
say '--';
while (@input) {
push @result, match(shift @input, shift @input);
}
say dump(\@result);