~srpablo/advent_of_code_2021

ec2ca06905b16178d3533eeed96b8c3b2db666af — Pablo Meier 1 year, 11 months ago c0e7ec6
Day 17 in Raku
3 files changed, 43 insertions(+), 0 deletions(-)

A day17/Makefile
A day17/day17.raku
A day17/input.txt
A day17/Makefile => day17/Makefile +3 -0
@@ 0,0 1,3 @@

run:
	raku day17.raku

A day17/day17.raku => day17/day17.raku +39 -0
@@ 0,0 1,39 @@
my $match = slurp("input.txt")[0] ~~ m:Perl5/target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)/;
my $ymin= $match[2];
my $part1 = ($ymin * ($ymin + 1)) / 2;
say "Part 1: $part1";

my $xmin= $match[0];
my $xmax = $match[1];
my $ymax = $match[3];

sub probe_lands_in_range($vel_x is copy, $vel_y is copy) {
  my $pos_x = 0;
  my $pos_y = 0;
  while $pos_x < ($xmax + 1) &&
    !($vel_x == 0 && $pos_x < $xmin) &&
    !($pos_x > $xmin && $pos_y < $ymin) {
      $pos_x += $vel_x;
      $pos_y += $vel_y;
      if $vel_x > 0 {
        $vel_x -= 1;
      }
      $vel_y -= 1;
      if $pos_x ~~ $xmin..$xmax && $pos_y ~~ $ymin..$ymax {
        return True
      }
  }
  return False
}

my $total_velocities = 0;

for 1..($xmax * 2) -> $x {
  for $ymin..$xmax -> $y {
    if probe_lands_in_range($x, $y) {
      $total_velocities++;
    }
  }
}

say "Part 2: $total_velocities";

A day17/input.txt => day17/input.txt +1 -0
@@ 0,0 1,1 @@
target area: x=128..160, y=-142..-88