aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol2.pl (510B) [raw]


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 my @groups;
      6 open(my $fh, '<', 'input') or die "$!";
      7 {
      8     local $/;
      9     my $content = <$fh>;
     10     @groups = split("\n\n", $content);
     11 }
     12 
     13 # reduce each group to a bag o words, then uniq, count
     14 my $n;
     15 for my $g (@groups) {
     16     my %yes;
     17     my @members = split("\n", $g);
     18     my $goal = scalar @members;
     19     for my $m (@members) {
     20         chomp($m);
     21         $yes{$_} += 1 for split('', $m);
     22     }
     23     $n += scalar grep { $yes{$_} == $goal } keys %yes;
     24 }
     25 print "$n\n";