summaryrefslogtreecommitdiff
path: root/04.pl
diff options
context:
space:
mode:
authorChristian Segundo2023-12-04 12:31:10 +0100
committerChristian Segundo2023-12-04 12:43:30 +0100
commit312d690d057b0d17560df3b855cb9b50c5aa8381 (patch)
tree117872a735c8d3f606c3bc3f11d64d110d50c670 /04.pl
parentaf54dee0ed31b94cb56bc95c0aacc750743d6a2c (diff)
downloadadvent-of-dotslash-2023-312d690d057b0d17560df3b855cb9b50c5aa8381.tar.gz
add day 4HEADmaster
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";