From 3c5e99ed933286637a7c893a4e29b790310a9694 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Mon, 11 Dec 2023 16:31:14 -0500 Subject: Solve day 5 part 1 --- 2023/day05.pl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2023/day05.pl (limited to '2023/day05.pl') diff --git a/2023/day05.pl b/2023/day05.pl new file mode 100644 index 0000000..38188f5 --- /dev/null +++ b/2023/day05.pl @@ -0,0 +1,37 @@ +use strict; +use warnings; +use List::Util qw(min); +my (@seeds, @soil, @fert, @water, @light, @temp, @humid, @locat); +while (<>) { + chomp; + @seeds = split(/ /, $1) if (/^seeds: (.*)/); + my $map; + $map = \@soil if /^seed-to-soil/ .. /^$/; + $map = \@fert if /^soil-to-fertilizer/ .. /^$/; + $map = \@water if /^fertilizer-to-water/ .. /^$/; + $map = \@light if /^water-to-light/ .. /^$/; + $map = \@temp if /^light-to-temperature/ .. /^$/; + $map = \@humid if /^temperature-to-humidity/ .. /^$/; + $map = \@locat if /^humidity-to-location/ .. /^$/; + push @$map, [split(/ /)] if /\d/; +} +sub lookup { + my ($map, $val) = @_; + for (@$map) { + my ($dst, $src, $len) = @$_; + if ($val >= $src && $val < $src + $len) { + return $dst + ($val - $src); + } + } + $val; +} +my @locs = map { + $_ = lookup(\@soil, $_); + $_ = lookup(\@fert, $_); + $_ = lookup(\@water, $_); + $_ = lookup(\@light, $_); + $_ = lookup(\@temp, $_); + $_ = lookup(\@humid, $_); + lookup(\@locat, $_); +} @seeds; +print min(@locs), "\n"; -- cgit 1.4.1