tidy perl script
This commit is contained in:
parent
223a77e9f2
commit
52f487e578
69
day02/d02.pl
69
day02/d02.pl
|
@ -1,24 +1,26 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use feature qw/ say /;
|
||||
use Test::More;
|
||||
|
||||
my $input = "input";
|
||||
my %shape = ( "X" => 1, "Y" => 2, "Z" => 3 );
|
||||
my %win = ( "X" => "C", "Y" => "A", "Z" => "B" );
|
||||
my %draw = ( "X" => "A", "Y" => "B", "Z" => "C" );
|
||||
my %lose = ( "X" => "B", "Y" => "C", "Z" => "A" );
|
||||
my $input = "input";
|
||||
my %shape = ( "X" => 1, "Y" => 2, "Z" => 3 );
|
||||
my %win = ( "X" => "C", "Y" => "A", "Z" => "B" );
|
||||
my %draw = ( "X" => "A", "Y" => "B", "Z" => "C" );
|
||||
my %lose = ( "X" => "B", "Y" => "C", "Z" => "A" );
|
||||
my %result = ( "X" => 0, "Y" => 3, "Z" => 6 );
|
||||
my %win_r = reverse %win;
|
||||
my %win_r = reverse %win;
|
||||
my %draw_r = reverse %draw;
|
||||
my %lose_r = reverse %lose;
|
||||
|
||||
ok(score("A", "Y") == 8);
|
||||
ok(score("B", "X") == 1);
|
||||
ok(score("C", "Z") == 6);
|
||||
ok(score_part2("A", "Y") == 4);
|
||||
ok(score_part2("B", "X") == 1);
|
||||
ok(score_part2("C", "Z") == 7);
|
||||
ok( score( "A", "Y" ) == 8 );
|
||||
ok( score( "B", "X" ) == 1 );
|
||||
ok( score( "C", "Z" ) == 6 );
|
||||
ok( score_part2( "A", "Y" ) == 4 );
|
||||
ok( score_part2( "B", "X" ) == 1 );
|
||||
ok( score_part2( "C", "Z" ) == 7 );
|
||||
done_testing();
|
||||
|
||||
say "Part 1";
|
||||
|
@ -41,40 +43,39 @@ open( my $fh, "<", $input )
|
|||
my $total_score = 0;
|
||||
while ( my $line = <$fh> ) {
|
||||
chomp $line;
|
||||
my ($p1, $p2) = split(/ /, $line);
|
||||
$total_score += score($p1, $p2);
|
||||
my ( $p1, $p2 ) = split( / /, $line );
|
||||
$total_score += score( $p1, $p2 );
|
||||
}
|
||||
close($fh);
|
||||
|
||||
say $total_score;
|
||||
|
||||
|
||||
say "Part 2";
|
||||
|
||||
sub score_part2 {
|
||||
my ( $p1, $r ) = @_;
|
||||
my $score = $result{$r};
|
||||
if ($score == 6){
|
||||
$score += $shape{$win_r{$p1}};
|
||||
}
|
||||
elsif ($score == 3){
|
||||
$score += $shape{$draw_r{$p1}};
|
||||
}
|
||||
else {
|
||||
$score += $shape{$lose_r{$p1}};
|
||||
}
|
||||
return $score;
|
||||
my ( $p1, $r ) = @_;
|
||||
my $score = $result{$r};
|
||||
if ( $score == 6 ) {
|
||||
$score += $shape{ $win_r{$p1} };
|
||||
}
|
||||
elsif ( $score == 3 ) {
|
||||
$score += $shape{ $draw_r{$p1} };
|
||||
}
|
||||
else {
|
||||
$score += $shape{ $lose_r{$p1} };
|
||||
}
|
||||
return $score;
|
||||
}
|
||||
|
||||
open( my $fh, "<", $input )
|
||||
open( my $fh2, "<", $input )
|
||||
or die "Can't open < $input: $!";
|
||||
|
||||
my $total_score = 0;
|
||||
while ( my $line = <$fh> ) {
|
||||
my $total_score_2 = 0;
|
||||
while ( my $line = <$fh2> ) {
|
||||
chomp $line;
|
||||
my ($p1, $r) = split(/ /, $line);
|
||||
$total_score += score_part2($p1, $r);
|
||||
my ( $p1, $r ) = split( / /, $line );
|
||||
$total_score_2 += score_part2( $p1, $r );
|
||||
}
|
||||
close($fh);
|
||||
close($fh2);
|
||||
|
||||
say $total_score;
|
||||
say $total_score_2;
|
||||
|
|
Loading…
Reference in New Issue