diff --git a/day2/day2.pl b/day2/day2.pl new file mode 100755 index 0000000..c207f67 --- /dev/null +++ b/day2/day2.pl @@ -0,0 +1,7 @@ +#!/usr/bin/perl + +use 5.20.0; + +use List::AllUtils qw/ minmax sum pairmap /; + +say sum pairmap { -$a, $b } map { minmax split } <>; diff --git a/day2/day2b.pl b/day2/day2b.pl new file mode 100755 index 0000000..6d68294 --- /dev/null +++ b/day2/day2b.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +use 5.20.0; +use experimental 'signatures'; + +use List::AllUtils qw/ sum first /; + +say sum map { divisors( sort { $b <=> $a } split ) } <>; + +sub divisors(@nums) { + while( my $x = shift @nums ) { + return $x / $_ for grep { defined $_ } first { not $x % $_ } @nums; + } +}