From: Jan "Yenya" Kasprzak Date: Mon, 7 Dec 2020 06:09:18 +0000 (+0100) Subject: day 7 X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc2020.git;a=commitdiff_plain;h=8143aefd914e6a5e17b0554e363e9b4bf9410a7b day 7 --- diff --git a/13.pl b/13.pl new file mode 100755 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"; diff --git a/14.pl b/14.pl new file mode 100755 index 0000000..ab8f8b4 --- /dev/null +++ b/14.pl @@ -0,0 +1,38 @@ +#!/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/); + push @{ $graph{$bag} }, [ $color => $count ]; + } + } +} + +my %seen = ('shiny gold' => 1); +my %total; + +sub walk { + my ($color) = @_; + + return $total{$color} + if defined $total{$color}; + + $total{$color} = 1; + for my $next (@{ $graph{$color} }) { + my ($ncol, $ncount) = @$next; + $total{$color} += $ncount*walk($ncol); + } + + return $total{$color}; +} + +print "Result is ", walk('shiny gold') - 1, "\n";