Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use List::AllUtils qw(max);
- $/ = '';
- $; = ',';
- my %Grid;
- my ($x_max, $y_max) = (0,0);
- # Read in grid
- foreach my $line (split /\n/, <>) {
- my ($x,$y) = split( /,/, $line );
- $Grid{$x,$y} = 1;
- }
- # read and execute folds
- my $part1;
- foreach (split /\n/, <>) {
- my ($axis,$line) = m#([xy])=(\d+)#;
- foreach my $pt (keys %Grid) {
- my ($x,$y) = split( /,/, $pt );
- if ($axis eq 'y') {
- if ($y > $line) {
- $Grid{$x, 2 * $line - $y}++;
- delete $Grid{$x,$y};
- }
- } else {
- if ($x > $line) {
- $Grid{2 * $line - $x, $y}++;
- delete $Grid{$x,$y};
- }
- }
- }
- $part1 //= keys %Grid;
- if ($axis eq 'y') {
- $y_max = $line - 1;
- } else {
- $x_max = $line - 1;
- }
- }
- print "Part 1: $part1\n";
- print "Part 2:\n";
- foreach my $y (0 .. $y_max) {
- foreach my $x (0 .. $x_max) {
- print (exists $Grid{$x,$y} ? '#' : ' ');
- }
- print "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement