Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- $| = 1;
- my @Grid = map { chomp; [map {($_ eq '.') ? 0 :
- ($_ eq '>') ? 1 : 2} split //] } <>;
- my $Y_SIZE = @Grid;
- my $X_SIZE = $Grid[0]->@*;
- my $moved;
- my $time = 0;
- do {
- $moved = 0;
- my @new_grid = ();
- for (my $y = 0; $y < $Y_SIZE; $y++) {
- my $ahead = $Grid[$y][0];
- for (my $x = $X_SIZE - 1; $x >= 0; $x--) {
- if (!$ahead and $Grid[$y][$x] == 1) {
- $new_grid[$y][($x + 1) % $X_SIZE] = 1;
- $new_grid[$y][$x] = 0;
- $moved++;
- }
- $ahead = $Grid[$y][$x];
- $new_grid[$y][$x] //= $ahead;
- }
- }
- @Grid = @new_grid;
- @new_grid = ();
- for (my $x = 0; $x < $X_SIZE; $x++) {
- my $ahead = $Grid[0][$x];
- for (my $y = $Y_SIZE - 1; $y >= 0; $y--) {
- if (!$ahead and $Grid[$y][$x] == 2) {
- $new_grid[($y + 1) % $Y_SIZE][$x] = 2;
- $new_grid[$y][$x] = 0;
- $moved++;
- }
- $ahead = $Grid[$y][$x];
- $new_grid[$y][$x] //= $ahead;
- }
- }
- @Grid = @new_grid;
- print ::stderr "Time: ", ++$time, " moved: $moved \r";
- } until (not $moved);
- print "\nPart 1: $time\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement