Advertisement
musifter

AoC 2021 day 13 (original fast Perl version)

Dec 13th, 2021
1,740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.08 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use List::AllUtils  qw(max);
  7.  
  8. $/ = '';
  9. $; = ',';
  10.  
  11. my %Grid;
  12. my ($x_max, $y_max) = (0,0);
  13.  
  14. # Read in grid
  15. foreach my $line (split /\n/, <>) {
  16.     my ($x,$y) = split( /,/, $line );
  17.     $Grid{$x,$y} = 1;
  18. }
  19.  
  20. # read and execute folds
  21. my $part1;
  22. foreach (split /\n/, <>) {
  23.     my ($axis,$line) = m#([xy])=(\d+)#;
  24.  
  25.     foreach my $pt (keys %Grid) {
  26.         my ($x,$y) = split( /,/, $pt );
  27.  
  28.         if ($axis eq 'y') {
  29.             if ($y > $line) {
  30.                 $Grid{$x, 2 * $line - $y}++;
  31.                 delete $Grid{$x,$y};
  32.             }
  33.         } else {
  34.             if ($x > $line) {
  35.                 $Grid{2 * $line - $x, $y}++;
  36.                 delete $Grid{$x,$y};
  37.             }
  38.         }
  39.     }
  40.  
  41.     $part1 //= keys %Grid;
  42.  
  43.     if ($axis eq 'y') {
  44.         $y_max = $line - 1;
  45.     } else {
  46.         $x_max = $line - 1;
  47.     }
  48. }
  49.  
  50. print "Part 1: $part1\n";
  51. print "Part 2:\n";
  52. foreach my $y (0 .. $y_max) {
  53.     foreach my $x (0 .. $x_max) {
  54.         print (exists $Grid{$x,$y} ? '#' : ' ');
  55.     }
  56.     print "\n";
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement