diff options
author | Leah Neukirchen <leah@vuxu.org> | 2019-12-04 17:20:36 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2019-12-04 17:20:36 +0100 |
commit | 0de529ff41514152fc8e8aaf49368a6e73bb2007 (patch) | |
tree | 4a81601f9aa8cd409d00be76331163be000d574d /day03.pl | |
parent | c612a380785d0f9fec1cf9494386beb271cf1d3c (diff) | |
download | adventofcode2019-0de529ff41514152fc8e8aaf49368a6e73bb2007.tar.gz adventofcode2019-0de529ff41514152fc8e8aaf49368a6e73bb2007.tar.xz adventofcode2019-0de529ff41514152fc8e8aaf49368a6e73bb2007.zip |
day03
Diffstat (limited to 'day03.pl')
-rw-r--r-- | day03.pl | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/day03.pl b/day03.pl new file mode 100644 index 0000000..542e820 --- /dev/null +++ b/day03.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl -w +use v5.16; + +use File::Slurper 'read_lines'; +use List::Util qw'min sum'; + +my @c = read_lines "day03"; + +my %f; +my @dur; + +while (my ($i, $c) = each @c) { + my ($x,$y) = (0,0); + + my %d; + push @dur, \%d; + my $d = 0; + + for (split ",", $c) { + for my $n (1..substr($_, 1)) { + if (/R/) { $x++ } + if (/L/) { $x-- } + if (/U/) { $y++ } + if (/D/) { $y-- } + + $f{$x,$y} |= $i+1; + $d{$x,$y} = ++$d; + } + } +} + +my @dup = grep { $f{$_} == 1+2 } keys %f; +say min map { my ($x,$y) = split($;, $_); abs($x)+abs($y) } @dup; +say min map { my $c = $_; sum(map { $_->{$c} } @dur) } @dup; + +__END__ +258 +12304 |