-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchal8B.pl
68 lines (50 loc) · 1.05 KB
/
chal8B.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
#!/usr/bin/perl
use strict;
open my $FILE, "chal8Input.txt" or die $!;
chomp(my @realInput = <$FILE>);
close $FILE;
my @input = @realInput;
print "Content is " . @input . "\n";
tryInstructions(@input);
my @input = @realInput;
my $index = 0;
while ($index < $#input) {
print "run $index\n";
print "Content is " . @input . "\n";
$input[$index] =~ s/jmp/nop/;
$input[$index] =~ s/acc/nop/;
tryInstructions(@input);
$index++;
@input = @realInput;
}
sub tryInstructions() {
my @input = @_;
my $accumulator = 0;
my $index = 0;
while($input[$index] ne "DONE" && $index < $#input) {
my $newIndex = $index;
if($input[$index] =~ /^acc ([\+\-])(.+)$/) {
if($1 eq "+") {
$accumulator += $2;
} else {
$accumulator -= $2;
}
}
if($input[$index] =~ /^jmp ([\+\-])(.+)$/) {
if($1 eq "+") {
$newIndex += $2;
} else {
$newIndex -= $2;
}
} else {
$newIndex++;
}
$input[$index] = "DONE";
$index = $newIndex;
}
if($input[$index] eq "DONE") {
print "Infinite Loop\n";
return;
}
die $accumulator;
}