summary refs log tree commit diff homepage
path: root/2023/day05.pl
blob: 38188f584fbeed505754fb6f1c101239ce896ccb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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";