diff options
Diffstat (limited to '04.pl')
-rwxr-xr-x | 04.pl | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -0,0 +1,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"; |