musifter

AoC 2021 day 3, part 2 (Perl)

Dec 3rd, 2021 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.49 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use List::AllUtils qw(true product);
  7.  
  8. sub filter {
  9.     my ($negate, @list) = @_;
  10.  
  11.     for (my $i = 0; @list > 1; $i++) {
  12.         my $ones = true { $_->[$i] } @list;
  13.         my $next = ($ones >= (@list / 2)) ^ $negate;
  14.  
  15.         @list = grep { $_->[$i] == $next } @list;
  16.     }
  17.  
  18.     return (oct("0b" . join('', @{$list[0]})));
  19. }
  20.  
  21. my @input = map { chomp; [split //] } <>;
  22.  
  23. print "Part 2: ", (product map {&filter( $_, @input )} (0,1)), "\n";
Add Comment
Please, Sign In to add comment