summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-12-11 16:31:14 -0500
committerJune McEnroe <june@causal.agency>2023-12-11 16:31:14 -0500
commit3c5e99ed933286637a7c893a4e29b790310a9694 (patch)
tree06d60bb70798ba9ac214e8ce083be987625b944d
parentSolve day 4 part 2 (diff)
downloadaoc-3c5e99ed933286637a7c893a4e29b790310a9694.tar.gz
aoc-3c5e99ed933286637a7c893a4e29b790310a9694.zip
Solve day 5 part 1
-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";