]> www.fi.muni.cz Git - aoc2020.git/blob - 13.pl
Task 9 Perl Golf-style
[aoc2020.git] / 13.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my %graph;
6 my $count = 0;
7
8 while (<>) {
9         my ($bag, $rest) = /\A(.*) bags? contain (.*)./;
10         if ($rest eq 'no other bags') {
11                 # $graph{$bag} = []; # ale neprojevi se
12         } else {
13                 for my $contain (split /, /, $rest) {
14                         my ($count, $color) = ($contain =~ /\A(\d+) (.*) bag/);
15                         print "\t$count\t$color.\n";
16                         push @{ $graph{$color} }, $bag;
17                 }
18         }
19 }
20
21 my %seen = ('shiny gold' => 1);
22 my @todo = 'shiny gold';
23
24 while (my $color = shift @todo) {
25         for my $next (@{ $graph{$color} }) {
26                 next if $seen{ $next };
27                 unshift @todo, $next;
28                 $seen{ $next } = 1;
29         }
30 }
31
32 print "Seen ", keys(%seen)-1, " nodes\n";