-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.pl
123 lines (100 loc) · 2.37 KB
/
day25.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env perl
use feature ':5.16';
use strict;
use warnings;
use utf8;
use Path::Tiny;
{ package Cukes;
sub move {
my ($self) = @_;
my $moves = 0;
# First the east
my $map = [];
for my $row (@{ $self->{ map } }) {
my $new = [];
my $e = 0;
while ($e < @{ $row } - 1) {
if ($row->[$e] eq '>' && $row->[$e+1] eq '.') {
push @{ $new }, '.';
$e++;
push @{ $new }, '>';
$e++;
next;
}
push @{ $new }, $row->[$e];
$e++;
}
if ($e == @{ $row } - 1) {
if ($row->[$e] eq '>' && $row->[0] eq '.') {
push @{ $new }, '.';
$new->[0] = '>';
}
else {
push @{ $new }, $row->[$e];
}
}
push @{ $map }, $new;
}
# Now the south
my $vmap = [];
for my $idx (0 .. @{ $map } - 2) {
my $new = [];
for my $e (0 .. @{ $map->[$idx] } - 1) {
if ($map->[$idx][$e] eq 'v' && $map->[$idx+1][$e] eq '.') {
$vmap->[$idx][$e] = '.';
$vmap->[$idx+1][$e] = 'v';
}
else {
$vmap->[$idx][$e] = $map->[$idx][$e] unless defined( $vmap->[$idx][$e] );
}
$e++;
}
}
# Last line
my $idx = @{ $map } - 1;
for my $e (0 .. @{ $map->[-1] } - 1) {
if ($map->[$idx][$e] eq 'v' && $map->[0][$e] eq '.') {
$vmap->[$idx][$e] = '.';
$vmap->[0][$e] = 'v';
}
else {
$vmap->[$idx][$e] = $map->[$idx][$e] unless defined( $vmap->[$idx][$e] );
}
}
my $row = 0;
while (!$moves && $row < @{ $vmap }) {
$moves = 1 if (join( '', @{ $vmap->[$row] } ) ne join( '', @{ $self->{ map }[$row] } ));
$row++;
}
$self->{ map } = $vmap;
return $moves;
}
sub print {
my ($self) = @_;
my $p = "\n";
for my $row (@{ $self->{ map } }) {
$p .= join( '', @{ $row } ) . "\n";
}
return $p;
}
sub new {
my ($class, $input_file) = @_;
my $self = {
map => [],
};
for my $l (Path::Tiny::path( $input_file )->lines_utf8( { chomp => 1 } )) {
push @{ $self->{ map } }, [ split( '', $l ) ];
}
bless $self, $class;
return $self;
}
}
my $input_file = $ARGV[0] || 'input25.txt';
my $cukes = Cukes->new( $input_file );
my $steps = 1;
while ($cukes->move()) {
print "On step $steps\n" if ($steps % 10 == 0);
$steps++;
}
print "It took $steps steps for the cukes to stop\n";
exit;