Advertisement
musifter

AoC 2024, day 14, part 1 (Perl)

Dec 14th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.67 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7. use List::Util      qw(product all);
  8.  
  9. my @DIM = (101, 103);
  10.  
  11. $; = ',';
  12. my @robots = map { [m#([-0-9]+,[-0-9]+)#g] } <>;
  13. my %moved;
  14.  
  15. foreach my $robot (@robots) {
  16.     my ($pos, $vel) = map {[split /,/]} @$robot;
  17.  
  18.     @$pos = map { ($pos->[$_] + 100 * $vel->[$_]) % $DIM[$_] } (0,1);
  19.     $moved{ $pos->[0], $pos->[1] }++;
  20. }
  21.  
  22. my %quads;
  23. foreach my $robot (keys %moved) {
  24.     my @pos  = split( $;, $robot );
  25.     my @quad = map { $pos[$_] <=> int($DIM[$_] / 2) } (0,1);
  26.  
  27.     $quads{ $quad[0], $quad[1] } += $moved{$robot}  if (all {$_} @quad);
  28. }
  29.  
  30. say "Part 1: ", product values %quads;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement