]> www.fi.muni.cz Git - aoc2020.git/blobdiff - 13.pl
day 7
[aoc2020.git] / 13.pl
diff --git a/13.pl b/13.pl
new file mode 100755 (executable)
index 0000000..93739e0
--- /dev/null
+++ b/13.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my %graph;
+my $count = 0;
+
+while (<>) {
+       my ($bag, $rest) = /\A(.*) bags? contain (.*)./;
+       if ($rest eq 'no other bags') {
+               # $graph{$bag} = []; # ale neprojevi se
+       } else {
+               for my $contain (split /, /, $rest) {
+                       my ($count, $color) = ($contain =~ /\A(\d+) (.*) bag/);
+                       print "\t$count\t$color.\n";
+                       push @{ $graph{$color} }, $bag;
+               }
+       }
+}
+
+my %seen = ('shiny gold' => 1);
+my @todo = 'shiny gold';
+
+while (my $color = shift @todo) {
+       for my $next (@{ $graph{$color} }) {
+               next if $seen{ $next };
+               unshift @todo, $next;
+               $seen{ $next } = 1;
+       }
+}
+
+print "Seen ", keys(%seen)-1, " nodes\n";