summary refs log tree commit diff homepage
path: root/2023
diff options
context:
space:
mode:
Diffstat (limited to '2023')
-rw-r--r--2023/day05.pl37
1 files changed, 37 insertions, 0 deletions
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";