Skip to content

Commit

Permalink
day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
lskatz committed Dec 6, 2023
1 parent 0d9984c commit 688c273
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
69 changes: 69 additions & 0 deletions 2023/t/06a.t
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
64 changes: 64 additions & 0 deletions 2023/t/06b.t
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

0 comments on commit 688c273

Please sign in to comment.