diff options
| author | Michael Lee Firkins <michael@firkins> | 2022-04-07 09:45:59 +0700 |
|---|---|---|
| committer | Michael Lee Firkins <michael@firkins> | 2022-04-07 09:45:59 +0700 |
| commit | f7d5c0ad3781a701f1690a93fa445bc5179fbd2c (patch) | |
| tree | 3660581847b10501450b79c7f08daa8b03be1400 | |
| parent | b2ad01584238b6bf7603faeb2748d93d431345a6 (diff) | |
| download | perlweeklychallenge-club-f7d5c0ad3781a701f1690a93fa445bc5179fbd2c.tar.gz perlweeklychallenge-club-f7d5c0ad3781a701f1690a93fa445bc5179fbd2c.tar.bz2 perlweeklychallenge-club-f7d5c0ad3781a701f1690a93fa445bc5179fbd2c.zip | |
pwc151 solution in perl
| -rw-r--r-- | challenge-151/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-151/pokgopun/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-151/pokgopun/perl/ch-2.pl | 55 |
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-151/pokgopun/README b/challenge-151/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-151/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-151/pokgopun/perl/ch-1.pl b/challenge-151/pokgopun/perl/ch-1.pl new file mode 100644 index 0000000000..fae41575f6 --- /dev/null +++ b/challenge-151/pokgopun/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; + +my @sample = @ARGV ? @ARGV : ('1 | 2 3 | 4 5', '1 | 2 3 | 4 * * 5 | * 6'); + +foreach my $sample (@sample) { + my @level = split /\|/, $sample; + my @node; + for (my $i = @level - 1; $i >=0; $i--){ + my @array = $level[$i] =~ /(\S+)/g; + foreach my $j (0..2**$i-1){ + my @pair; + $pair[$_] = $node[2*$j+$_] foreach 0..1; + @pair = sort{$a <=> $b} grep{defined $_} @pair; + #printf "%s => %s\n", $j, join(",", map{defined $_ ? $_ : 'undef'} @pair); + $array[$j] = @pair ? $pair[0] : defined $array[$j] && $array[$j] ne '*' ? $i+1 : undef; + } + @node = @array; + #printf "%s\n", join(",", map{defined $_ ? $_ : 'undef'} @node); + } + @node = sort{$a <=> $b} grep{defined $_} @node; + printf "Input: %s\n", $sample; + printf "Output: %d\n\n",$node[0]; +} + + diff --git a/challenge-151/pokgopun/perl/ch-2.pl b/challenge-151/pokgopun/perl/ch-2.pl new file mode 100644 index 0000000000..81a590013d --- /dev/null +++ b/challenge-151/pokgopun/perl/ch-2.pl @@ -0,0 +1,55 @@ +use strict; +use warnings; + +my $debug = 0; + +my @sample = @ARGV && join("",@ARGV) =~ /^\d+$/ ? ([@ARGV]) : ([2,4,5], [4, 2, 3, 6, 5, 3]); + +foreach my $valuables (@sample) { + my @valuables = @$valuables; + my @robs; + &rob([0],[1..@valuables-1],\@robs); + + #use Data::Dumper; + #print Dumper \@rob; + + my @rob; + my $rob_values; + my $rob_amount = 0; + foreach (@robs){ + + my $rob = join(" + ", @valuables[@$_]); + my $amount = eval($rob); + + #print "$rob = $amount\n\n"; + + if ($amount > $rob_amount) { + $rob_amount = $amount; + $rob_values = $rob; + @rob = @$_; + } + } + + if ($debug) { + $valuables[$_] .= '*' foreach @rob; + } + printf "Input: \@valuables = (%s) %s\n", join(", ",@valuables), $debug ? "=> $rob_values" : ""; + #printf "%s\n", $rob_values if $debug == 1; + printf "Output: %d\n\n", $rob_amount; +} + +sub rob{ + my($c,$e,$rob) = @_; + if (@$e < 2){ + #printf "%s\n", join " ", @$c; + push @$rob, $c; + } else { + for (my $i=1; $i <= 2; $i++){ + my @et = @{$e}[$i..@$e-1]; + last unless @et; + my @ct = @$c; + push @ct, shift(@et); + rob([@ct],[@et],$rob); + } + } +} |
