Advertisement
musifter

AoC 2021 day 13 (Perl non-SIF sif version)

Dec 13th, 2021 (edited)
1,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.77 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.14;
  4. use strict;
  5. use warnings;
  6.  
  7. use List::Util  qw(all);
  8.  
  9. $/ = '';
  10.  
  11. # Read in points and folds
  12. my @Points = map { my @p = split /,/; {x => $p[0], y => $p[1]} } split(/\n/, <>);
  13. my @Folds  = map { [m#([xy])=(\d+)#] } split(/\n/, <>);
  14.  
  15. my %SIZE = (x => 40, y => 6);
  16.  
  17. # Prepare (NOT) Space Image Format data
  18. my @sif = map { [(' ') x $SIZE{x}] } (1 .. $SIZE{y});
  19.  
  20. foreach my $pt (@Points) {
  21.     # Transform point until in final image box
  22.     foreach my $fold (@Folds) {
  23.         last if (all { $pt->{$_} < $SIZE{$_} } ('x','y'));
  24.  
  25.         my ($axis, $line) = @$fold;
  26.         $pt->{$axis} = 2 * $line - $pt->{$axis}  if ($pt->{$axis} > $line);
  27.     }
  28.  
  29.     # Set pixel in SIF file
  30.     $sif[$pt->{y}][$pt->{x}] = '#';
  31. }
  32.  
  33. say @$_ foreach (@sif);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement