Instantly share code, notes, and snippets.

@mjg17 /cpanfile forked from mrallen1/cpanfile
Last active Dec 21, 2016

Embed
What would you like to do?
Complete listing for my 2016 Perl Advent article
requires 'Text::CSV'
requires 'Geo::Calc::XS'
requires 'IO::String'
use 5.014;
use Text::CSV;
use IO::String;
use Geo::Calc::XS;
use List::Util qw(reduce);
my $data =<<_DATA;
id, name, nice, siblings, present
1, Hermione, 1, 0, Owl food
2, Delores, 0, 1, Coal
3, Draco, 0, 0, Dirty sock
4, Ronald, 1, 6, Scarf
5, Harry, 1, 0, Wand
_DATA
my $fh = IO::String->new($data);
my $csv = Text::CSV->new( { allow_whitespace => 1 } );
$csv->column_names( $csv->getline($fh) );
my @nice = grep {; $_->{nice} } @{ $csv->getline_hr_all($fh) };
close $fh;
my %locations = (
1 => [41.94757, -87.6562],
2 => [34.07905, -118.4744],
3 => [30.26759, -97.74299],
4 => [21.30485, -157.8578],
5 => [55.67610, 12.56830],
);
my @deliveries = map {; my $loc = $locations{$_->{id}};
$_->{geo} = Geo::Calc::XS->new( lat => $loc->[0],
lon => $loc->[1], units => 'mi' );
$_ } @nice;
my $last = reduce {
$b->{miles_traveled} = ($a->{miles_traveled} // 0) + $a->{geo}->distance_to( $b->{geo} );
$b;
} @deliveries;
say $last->{miles_traveled};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment