day07
This commit is contained in:
parent
25281404f1
commit
88fc836bd5
15
07/1.pl
Normal file
15
07/1.pl
Normal file
@ -0,0 +1,15 @@
|
||||
use 5.20.0;
|
||||
use experimental qw/ smartmatch /;
|
||||
|
||||
my %prog;
|
||||
|
||||
while(<>) {
|
||||
my( $name, @deps ) =
|
||||
map { split ', ' } /(\S+).*?-> (.*)/;
|
||||
$prog{$name} = \@deps;
|
||||
}
|
||||
|
||||
my @all_deps = map { @$_ } values %prog;
|
||||
|
||||
say $_
|
||||
for grep { not $_ ~~ @all_deps } keys %prog;
|
33
07/2.pl
Normal file
33
07/2.pl
Normal file
@ -0,0 +1,33 @@
|
||||
use 5.20.0;
|
||||
use experimental qw/ smartmatch signatures postderef /;
|
||||
use List::AllUtils qw/ sum /;
|
||||
use List::UtilsBy qw/ nsort_by /;
|
||||
|
||||
my %prog;
|
||||
|
||||
while(<>) {
|
||||
my( $name, $weight, @deps ) =
|
||||
map { split ', ' } /(\S+).*?(\d+)(?:.*?-> (.*))?/;
|
||||
$prog{$name} = [ $weight, \@deps ];
|
||||
}
|
||||
|
||||
balance('qibuqqg');
|
||||
|
||||
sub balance($prog) {
|
||||
my %child = map { $_ => balance($_) } $prog{$prog}[1]->@*;
|
||||
|
||||
my %w;
|
||||
while ( my( $n, $w ) = each %child ) {
|
||||
push @{ $w{$w} }, $n;
|
||||
}
|
||||
|
||||
if( 1 < keys %w ) {
|
||||
my( $bad, $good ) = nsort_by { scalar $w{$_}->@* } keys %w;
|
||||
die $w{$bad}[0], " needs to be ", $prog{$w{$bad}[0]}[0] - $bad + $good;
|
||||
}
|
||||
|
||||
return $prog{$prog}[0] + sum values %child;
|
||||
}
|
||||
|
||||
|
||||
|
1317
07/input.txt
Normal file
1317
07/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user