Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- $| = 1;
- # Read grid:
- my @Grid = map { [split(//)] } <>;
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- # Vector stuff:
- my @Dirs = ([0,1], [1,0], [0,-1], [-1,0]);
- sub vec_sum ($$) { my ($v,$w) = @_; return [$v->[0] + $w->[0], $v->[1] + $w->[1]] }
- sub vec_eq ($$) { my ($v,$w) = @_; return (($v->[0] == $w->[0]) and ($v->[1] == $w->[1])) }
- sub dist ($$) { my ($v,$w) = @_; return (abs($v->[0] - $w->[0]) + abs($v->[1] - $w->[1])) }
- # Find start and end:
- my ($start, $end);
- foreach my $y (0 .. $#Grid) {
- foreach my $x (0 .. $Grid[0]->$#*) {
- $start = [$y,$x] if ($Grid[$y][$x] eq 'S');
- $end = [$y,$x] if ($Grid[$y][$x] eq 'E');
- }
- }
- # Get path of race course
- my @path = ($start);
- my $pos = $start;
- my $back = -1;
- while (!&vec_eq( $pos, $end )) {
- DIR:
- for (my $d = 0; $d < @Dirs; $d++) {
- my $move = &vec_sum( $pos, $Dirs[$d] );
- if (grid_at($move) ne '#' and $back != $d) {
- $back = ($d + 2) % 4;
- $pos = $move;
- push( @path, $move );
- last DIR;
- }
- }
- }
- # Done with grid, now using just the path:
- my $part1 = 0;
- my $part2 = 0;
- my $targ = 100;
- # Compare points that are far enough apart on the path to potentially gain target.
- foreach (my $i = 0; $i <= $#path - ($targ + 2); $i++) {
- print ::stderr "[$i/$#path] $part2\r" if ($i % 250 == 0);
- foreach (my $j = $i + ($targ + 2); $j <= $#path; $j++) {
- my $dist = &dist( $path[$i], $path[$j] );
- # Check allowed distance, and if we didn't lose too much time.
- if (($dist <= 20) and ($j - $i - $dist >= $targ)) {
- $part2++;
- $part1++ if ($dist == 2);
- }
- }
- }
- say "\nPart 1: $part1";
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement