X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=42.pl;fp=42.pl;h=05bf2ac85b7635f81f547fe8242a901a8d90da40;hb=63b534141e63cf51990d5fa2234e1d21f0bc1bbc;hp=0000000000000000000000000000000000000000;hpb=a5a6272d28f8ecd0c24250cbf75f195ed5385e72;p=aoc2020.git diff --git a/42.pl b/42.pl new file mode 100755 index 0000000..05bf2ac --- /dev/null +++ b/42.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl -w + +use strict; + +my %allergens; +my %is_al; + +while (<>) { + chomp; + my ($ingr, $al) = /\A([^\(]+) \(contains (.*)\)\z/; + my %ingr = map { $_ => 1 } split /\s+/, $ingr; + my @al = split /, /, $al; + for my $al (@al) { + if (defined $allergens{$al}) { + for my $in1 (keys %{ $allergens{$al} }) { + if (! $ingr{$in1}) { + delete $allergens{$al}->{$in1}; + } + } + } else { + $allergens{$al} = { %ingr }; + } + } +} + +AGAIN: +for my $al (keys %allergens) { + if (keys %{ $allergens{$al} } == 1) { + my $in1; + for my $in (keys %{ $allergens{$al} }) { + $is_al{$in} = $al; + $in1 = $in; + } + delete $allergens{$al}; + for my $al1 (keys %allergens) { + delete $allergens{$al1}->{$in1}; + } + goto AGAIN; + } +} + +print join(',', sort { $is_al{$a} cmp $is_al{$b} } keys %is_al), "\n";