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";