From bc5be98a8df355fa9d1f3f9455bedcc933a1f609 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 3 Dec 2017 12:06:46 -0500 Subject: [PATCH] day2 --- day2/day2.pl | 7 +++++++ day2/day2b.pl | 14 ++++++++++++++ 2 files changed, 21 insertions(+) create mode 100755 day2/day2.pl create mode 100755 day2/day2b.pl 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; + } +}