musifter

AoC 2021 day 13 (Perl-SIF output)

Dec 13th, 2021 (edited)
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.96 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. # Read in points and folds
  11. my @Points = map {[split /,/]} split( /\n/, <> );
  12. my @Folds  = map {[m#([xy])=(\d+)#]} split( /\n/, <> );
  13.  
  14. my ($X_SIZE, $Y_SIZE) = (40, 6);
  15. my $LAYER_SIZE = $X_SIZE * $Y_SIZE;
  16. my $LAYERS = @Folds;
  17.  
  18. # Prepare Space Image Format data
  19. my @sif = (('2') x ($LAYER_SIZE * $LAYERS), ('0') x $LAYER_SIZE);
  20.  
  21. foreach my $pt (@Points) {
  22.     my ($x,$y) = ($pt->[0], $pt->[1]);
  23.  
  24.     # Find layer that brings point into final image box
  25.     my $layer = 0;
  26.     foreach my $fold (@Folds) {
  27.         last if ($x < $X_SIZE and $y < $Y_SIZE);
  28.         if ($fold->[0] eq 'y') {
  29.             $y = 2 * $fold->[1] - $y  if ($y > $fold->[1]);
  30.         } else {
  31.             $x = 2 * $fold->[1] - $x  if ($x > $fold->[1]);
  32.         }
  33.  
  34.         $layer++;
  35.     }
  36.  
  37.     # Set pixel in SIF file
  38.     $sif[$layer * $LAYER_SIZE + $y * $X_SIZE + $x] = 1;
  39. }
  40.  
  41. print @sif, "\n";
Add Comment
Please, Sign In to add comment