summaryrefslogtreecommitdiff
path: root/04.pl
blob: 3a1e629243b441621dca856a3a5a80743fa170c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env perl
use strict;
use warnings;

use File::Slurp;

my ( $o, $t ) = ( 0, 0 );

foreach ( read_file('04.txt') ) {
  $_ =~ s/Card\s+\d+:\s+//;
  my ( $i, $j ) = split( /\s+\|\s+/, $_ );
  my %k = map  { $_ => 1 } split( /\s+/, $i );
  my @v = grep { $k{$_} } split( /\s+/, $j );
  next unless scalar(@v);
  $o = scalar(@v) == 1 ? $o + 1 : $o + 2**( scalar(@v) - 1 );
}

print( $o . "\n" );

my @c = map {
  chomp;
  s/Card\s+\d+:\s+//;
  my ( $z, $j ) = split( /\s+\|\s+/, $_ );
  my %k = map  { $_ => 1 } split( /\s+/, $z );
  my @v = grep { $k{$_} } split( /\s+/, $j );
  { i => scalar(@v), j => 1 }
} read_file('04.txt');

for ( my $j = 0 ; $j < scalar(@c) ; $j++ ) {
  for ( 1 .. $c[$j]->{j} ) {
    for ( $j + 1 .. $j + $c[$j]->{i} ) {
      $c[$_]->{j}++;
    }
  }
  $t += $c[$j]->{j};
}

print $t . "\n";