Advertisement
musifter

AoC 2023 day 6 (Perl)

Dec 6th, 2023
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.50 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.32;
  4. use warnings;
  5.  
  6. use List::AllUtils  qw(product pairwise);
  7. use POSIX           qw(ceil floor);
  8.  
  9. sub count_wins {
  10.     my ($t, $d) = @_;
  11.  
  12.     $d++;
  13.     my $upper = floor(($t + sqrt( $t*$t - 4*$d )) / 2);
  14.     my $lower = ceil (($t - sqrt( $t*$t - 4*$d )) / 2);
  15.  
  16.     return ($upper - $lower + 1);
  17. }
  18.  
  19. my @in  = map { [m#(\d+)#g] } <>;
  20. say "Part 1: ", product pairwise { &count_wins($a,$b) } $in[0]->@*, $in[1]->@*;
  21. say "Part 2: ", &count_wins( map { join('', @$_) } @in );
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement