-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use Test::More tests=>2; | ||
use List::Util qw/product/; | ||
|
||
subtest "Test $0" => sub{ | ||
my $data = " | ||
Time: 7 15 30 | ||
Distance: 9 40 200 | ||
"; | ||
|
||
my $marginsOfError = marginsOfError($data); | ||
my @exp = (4, 8, 9); | ||
is_deeply($marginsOfError, \@exp); | ||
|
||
is(product(@$marginsOfError), product(@exp), "product"); | ||
}; | ||
|
||
subtest "Real $0" => sub{ | ||
local $/ = undef; | ||
my $data = <DATA>; | ||
$data =~ s/^\s+|\s+$//g; # whitespace trim on each line | ||
|
||
my $marginsOfError = marginsOfError($data); | ||
|
||
is(product(@$marginsOfError), 1159152, "product"); | ||
}; | ||
|
||
sub marginsOfError{ | ||
my($data) = @_; | ||
|
||
my @data = grep {/\S/} split(/\n/,$data); | ||
|
||
my(undef, @times) = split(/\s+/, $data[0]); | ||
my(undef, @dists) = split(/\s+/, $data[1]); | ||
|
||
my @wins; | ||
for(my $i=0; $i<@times; $i++){ | ||
my $wins = timePermutations($times[$i], $dists[$i]); | ||
#note "$times[$i]/$dists[$i]: $wins"; | ||
push(@wins, $wins); | ||
} | ||
|
||
return \@wins; | ||
} | ||
|
||
sub timePermutations{ | ||
my($time, $distToBeat) = @_; | ||
|
||
my $numWins = 0; | ||
for(my $pressTime=0; $pressTime<$time; $pressTime++){ | ||
my $goTime = $time - $pressTime; | ||
my $dist = $pressTime * $goTime; | ||
if($dist > $distToBeat){ | ||
$numWins++; | ||
} | ||
} | ||
|
||
return $numWins; | ||
} | ||
|
||
__DATA__ | ||
Time: 58 81 96 76 | ||
Distance: 434 1041 2219 1218 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use Test::More tests=>2; | ||
|
||
subtest "Test $0" => sub{ | ||
my $data = " | ||
Time: 7 15 30 | ||
Distance: 9 40 200 | ||
"; | ||
|
||
my $wins = marginsOfError($data); | ||
|
||
is($wins, 71503, "num wins with no kerning"); | ||
}; | ||
|
||
subtest "Real $0" => sub{ | ||
local $/ = undef; | ||
my $data = <DATA>; | ||
$data =~ s/^\s+|\s+$//g; # whitespace trim on each line | ||
|
||
my $wins = marginsOfError($data); | ||
|
||
is($wins, 41513103, "num wins with no kerning"); | ||
}; | ||
|
||
sub marginsOfError{ | ||
my($data) = @_; | ||
|
||
my @data = grep {/\S/} split(/\n/,$data); | ||
|
||
my(undef, @times) = split(/\s+/, $data[0]); | ||
my(undef, @dists) = split(/\s+/, $data[1]); | ||
|
||
my $time = join("", @times); | ||
my $dist = join("", @dists); | ||
|
||
my $wins = timePermutations($time, $dist); | ||
|
||
return $wins; | ||
} | ||
|
||
sub timePermutations{ | ||
my($time, $distToBeat) = @_; | ||
|
||
my $numWins = 0; | ||
for(my $pressTime=0; $pressTime<$time; $pressTime++){ | ||
my $goTime = $time - $pressTime; | ||
my $dist = $pressTime * $goTime; | ||
if($dist > $distToBeat){ | ||
$numWins++; | ||
} | ||
} | ||
|
||
return $numWins; | ||
} | ||
|
||
__DATA__ | ||
Time: 58 81 96 76 | ||
Distance: 434 1041 2219 1218 | ||