Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.32;
- use warnings;
- use List::Util qw(min);
- # Paragraph mode, array of sections
- $/ = '';
- my @section = map {[split /\n/]} <>;
- # Read in seeds from first line of first section
- my @seeds = ($section[0][0] =~ m#(\d+)#g);
- # Read in sections, building maps
- my %Map; # hash of table -> array of struct (src, dst, len) hash
- shift @section;
- foreach my $sect (@section) {
- shift(@$sect) =~ m#-(\w+) map#;
- my $type = $1;
- foreach my $line (@$sect) {
- my ($dst, $src, $len) = split( ' ', $line );
- push( $Map{$type}->@*, {src => $src, dst => $dst, len => $len} );
- }
- }
- my @order = qw(soil fertilizer water light temperature humidity location);
- sub transform_seed {
- my $val = shift;
- foreach my $step (@order) {
- foreach my $range ($Map{$step}->@*) {
- if ($range->{src} <= $val <= $range->{src} + $range->{len}) {
- $val = $range->{dst} + ($val - $range->{src});
- last;
- }
- }
- }
- return ($val);
- }
- say "Part 1: ", min map { &transform_seed($_) } @seeds;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement