summaryrefslogtreecommitdiff
path: root/04.pl
diff options
context:
space:
mode:
Diffstat (limited to '04.pl')
-rwxr-xr-x04.pl38
1 files changed, 38 insertions, 0 deletions
diff --git a/04.pl b/04.pl
new file mode 100755
index 0000000..3a1e629
--- /dev/null
+++ b/04.pl
@@ -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";