AoC/days/04/part1.pl

42 lines
970 B
Perl

#!/usr/bin/env perl
use warnings;
use strict;
use List::Util;
my $filename = "./data/input.txt";
open(FH, '<', $filename) or die $!;
my $score = 0;
while (<FH>) {
my $card = $_;
my $points = 0;
my @parts = split /:/, $card;
my @lists = split /\|/, $parts[1];
my @winning = split ' ', $lists[0];
my @numbers = split ' ', $lists[1];
my @previous = ();
foreach my $number (@numbers) {
if ($number eq " ") {
print $_;
}
if ((List::Util::any { $_ eq $number } @winning) && (List::Util::all { $_ ne $number } @previous)) {
push(@previous, $number);
if ($points == 0) {
$points = 1;
} else {
$points *= 2;
}
# print $number . ", ";
}
}
#if ($points == 0) {
# print "no points" . "\n";
#} else {
# print $points . " points\n";
#}
$score += $points;
}
print $score . "\n";