From b342b3783a74a39633ad77757f91a371c06695e0 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 25 Feb 2018 13:58:41 -0500 Subject: [PATCH] 2016-17 --- 2016/17/1.pl | 43 +++++++++++++++++++++++++++++++++++++++++++ 2016/17/2.pl | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 2016/17/1.pl create mode 100644 2016/17/2.pl diff --git a/2016/17/1.pl b/2016/17/1.pl new file mode 100644 index 0000000..d5708dd --- /dev/null +++ b/2016/17/1.pl @@ -0,0 +1,43 @@ +use 5.20.0; + +use Digest::MD5 qw/ md5_hex /; +use Math::Vector::Real; +use List::AllUtils qw/ min max minmax /; + +my @directions = ( + [ 'U', V(0,-1) ], + [ 'D', V(0,1) ], + [ 'L', V(-1,0) ], + [ 'R', V(1,0) ], +); + +my $best_path; + +wander( 'bwnlcvfs', V(0,0) ); + +sub wander { + my( $hash, $c ) = @_; + + if( $best_path and length($hash) >= length($best_path) ) { + return; + } + + my( $min,$max ) = minmax @$c; + + return if 0 > $min; + return if 3 < $max; + + if( $c == V(3,3) ) { + $best_path = $hash; + say $best_path; + return; + } + + my @md5 = split '', md5_hex($hash), 5; + + my @d = grep { (shift @md5) =~ /[b-f]/ } @directions; + + for my $d ( @d ) { + wander( $hash . $d->[0], $c + $d->[1] ); + } +} diff --git a/2016/17/2.pl b/2016/17/2.pl new file mode 100644 index 0000000..012a6e2 --- /dev/null +++ b/2016/17/2.pl @@ -0,0 +1,43 @@ +use 5.20.0; + +use Digest::MD5 qw/ md5_hex /; +use Math::Vector::Real; +use List::AllUtils qw/ min max minmax /; + +my @directions = ( + [ 'U', V(0,-1) ], + [ 'D', V(0,1) ], + [ 'L', V(-1,0) ], + [ 'R', V(1,0) ], +); + +my $best_path; + +wander( 'bwnlcvfs', V(0,0) ); + +say length($best_path) - 8; + +sub wander { + my( $hash, $c ) = @_; + + my( $min,$max ) = minmax @$c; + + return if 0 > $min; + return if 3 < $max; + + if( $c == V(3,3) ) { + say $hash; + if( length($hash) >= length($best_path) ) { + $best_path = $hash; + } + return; + } + + my @md5 = split '', md5_hex($hash), 5; + + my @d = grep { (shift @md5) =~ /[b-f]/ } @directions; + + for my $d ( @d ) { + wander( $hash . $d->[0], $c + $d->[1] ); + } +}