-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.pl
executable file
·182 lines (139 loc) · 3.09 KB
/
day13.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl
#
# $Id: pl.template,v 1.2 2014/07/18 15:01:38 caran Exp $
#
use strict;
use warnings;
use Path::Tiny;
{ package Scanner;
sub reset {
my $self = shift;
$self->{ pos } = 0;
$self->{ step } = 1;
return $self;
}
sub caught {
my $self = shift;
return ($self->{ pos } == 0) ? $self->{ depth } : 0;
}
sub scan {
my $self = shift;
$self->{ pos } += $self->{ step };
$self->{ step } = 1 if ($self->{ pos } == 0);
$self->{ step } = -1 if ($self->{ pos } == $self->{ depth } - 1);
return $self;
}
sub clone {
my $self = shift;
my $copy = bless { %{ $self } }, ref $self;
return $copy;
}
sub new {
my $class = shift;
my $depth = shift;
my $self = {
depth => $depth,
pos => 0,
step => 1,
};
bless $self, $class;
return $self;
}
}
{ package Firewall;
sub reset {
my ($self) = @_;
for my $s (@{ $self->{ levels } }) {
$s->reset() if ($s);
}
}
sub next_pico {
my ($self) = @_;
for my $s (@{ $self->{ levels } }) {
next unless $s;
$s->scan();
}
}
sub is_caught {
my ($self) = @_;
my $pos = 0;
while ($pos < @{ $self->{ levels } }) {
my $scanner = $self->{ levels }[$pos];
return 1 if ($scanner && $scanner->caught());
$self->next_pico();
$pos++;
}
return 0;
}
sub traverse {
my ($self) = @_;
my $score = 0;
my $pos = 0;
while ($pos < @{ $self->{ levels } }) {
my $scanner = $self->{ levels }[$pos];
$score += $pos * $scanner->caught if ($scanner);
$self->next_pico();
$pos++;
}
return $score;
}
sub escape {
my ($self) = @_;
my $delay = 0;
while (1) {
$self->reset();
my $wait = $delay;
$self->next_pico() while ($wait--);
return $delay if (!$self->is_caught());
$delay++;
};
return;
}
sub clone {
my $self = shift;
my $copy = {
levels => [],
};
bless $copy, ref $self;
for (my $i = 0; $i < @{ $self->{ levels } }; $i++) {
my $scanner = $self->{ levels }[$i];
$copy->{ levels }[$i] = $scanner->clone() if ($scanner);
}
return $copy;
}
sub new {
my $class = shift;
my $scanners = shift;
my $self = {
levels => [],
};
bless $self, $class;
for my $s (@{ $scanners }) {
my ($loc, $depth) = $s =~ /^(\d+)\D*(\d+)/;
$self->{ levels }[$loc] = Scanner->new( $depth );
}
return $self;
}
}
sub escape
{
my $firewall = shift;
$firewall->reset();
my $delay = 0;
while (1) {
if ($firewall->{ levels }[0]{ pos } != 0 && $firewall->{ levels }[1]{ pos } != 1) {
my $orig = $firewall->clone();
return $delay if (!$firewall->is_caught());
$firewall = $orig;
}
$firewall->next_pico();
$delay++;
};
return;
}
my $input_file = $ARGV[0] || 'input13.txt';
my @input = path( $input_file )->lines_utf8();
my $firewall = Firewall->new( \@input );
print "The score is ", $firewall->traverse(), "\n";
print "The minimum delay is ", escape( $firewall ), "\n";
exit;