Day 4 - Solved
This commit is contained in:
parent
c335ec57a6
commit
9646029fea
|
@ -0,0 +1,41 @@
|
|||
#!/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";
|
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env python3
|
||||
import queue
|
||||
|
||||
with open("data/input.txt", "r") as f:
|
||||
data = f.read()
|
||||
|
||||
q = queue.Queue()
|
||||
|
||||
def winning_copies(card):
|
||||
lists = card.split(":")[1].split("|")
|
||||
winning = lists[0].split()
|
||||
numbers = lists[1].split()
|
||||
matching = 0
|
||||
for number in numbers:
|
||||
if number in winning:
|
||||
matching += 1
|
||||
return matching
|
||||
|
||||
cards = data.split("\n")
|
||||
cards.remove("")
|
||||
for i, card in enumerate(cards):
|
||||
q.put((i, card))
|
||||
|
||||
counter = 0
|
||||
while not q.empty():
|
||||
i, card = q.get()
|
||||
copies = winning_copies(card)
|
||||
if copies > 0:
|
||||
for j in range(1, copies+1):
|
||||
copy = cards[i+j]
|
||||
q.put((i+j, copy))
|
||||
counter += 1
|
||||
|
||||
print(counter)
|
Loading…
Reference in New Issue