-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.pl
executable file
·152 lines (130 loc) · 3.75 KB
/
day21.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
#!/usr/bin/perl
#
#
use strict;
use warnings;
use Path::Tiny;
#
# Note: For part a at line 28, register 0 has to match register 3:
#
# 28 eqrr 3 0 4
#
# Using the debug print statements, I determined the output of register 3
# the first time line 28 was reached.
#
my $part = defined( $ARGV[1] ) || 7216956;
my $debug = 1;
my %halt_vals;
{ package Program;
my $opcodes = {
'addr' => sub {
my ($self, $a, $b, $c) = @_;
return $self->{ regs }[$a] + $self->{ regs }[$b];
},
'addi' => sub {
my ($self, $a, $b, $c) = @_;
return $self->{ regs }[$a] + $b;
},
'mulr' => sub {
my ($self, $a, $b, $c) = @_;
return $self->{ regs }[$a] * $self->{ regs }[$b];
},
'muli' => sub {
my ($self, $a, $b, $c) = @_;
return $self->{ regs }[$a] * $b;
},
'banr' => sub {
my ($self, $a, $b, $c) = @_;
return int( $self->{ regs }[$a] ) & int( $self->{ regs }[$b] );
},
'bani' => sub {
my ($self, $a, $b, $c) = @_;
return int( $self->{ regs }[$a] ) & $b;
},
'borr' => sub {
my ($self, $a, $b, $c) = @_;
return int( $self->{ regs }[$a] ) | int( $self->{ regs }[$b] );
},
'bori' => sub {
my ($self, $a, $b, $c) = @_;
return int( $self->{ regs }[$a] ) | $b;
},
'setr' => sub {
my ($self, $a, $b, $c) = @_;
return $self->{ regs }[$a];
},
'seti' => sub {
my ($self, $a, $b, $c) = @_;
return $a;
},
'gtir' => sub {
my ($self, $a, $b, $c) = @_;
return ($a > $self->{ regs }[$b] ? 1 : 0);
},
'gtri' => sub {
my ($self, $a, $b, $c) = @_;
return ($self->{ regs }[$a] > $b ? 1 : 0);
},
'gtrr' => sub {
my ($self, $a, $b, $c) = @_;
return ($self->{ regs }[$a] > $self->{ regs }[$b] ? 1 : 0);
},
'eqir' => sub {
my ($self, $a, $b, $c) = @_;
return ($a == $self->{ regs }[$b] ? 1 : 0);
},
'eqri' => sub {
my ($self, $a, $b, $c) = @_;
return ($self->{ regs }[$a] == $b ? 1 : 0);
},
'eqrr' => sub {
my ($self, $a, $b, $c) = @_;
return ($self->{ regs }[$a] == $self->{ regs }[$b] ? 1 : 0);
},
};
sub execute {
my ($self, $code) = @_;
my ($opcode, $a, $b, $c) = split( /\s+/, $code );
$self->{ regs }[$c] = $opcodes->{ $opcode }->( $self, $a, $b );
}
sub run {
my ($self) = @_;
my $reg = $self->{ regs };
while ((my $line = $reg->[ $self->{ inst } ]) < @{ $self->{ code } }) {
print "[ ", join( ',', map { sprintf "%10x", $_ } @{ $self->{ regs } } ), " ]\n" if ($debug == 1 && $line == 28);
print "[ ", join( ',', map { sprintf "%10x", $_ } @{ $self->{ regs } } ), " ]\n" if ($debug > 1);
if ($line == 28) {
my $halt = $self->{ regs }[3];
die "A match was found. Use the previous value of register 4.\n" if ($halt_vals{ $halt });
$halt_vals{ $halt } = 1;
}
$self->execute( $self->{ code }[$line] );
# Short-circuit the loop!
if (1 && $line == 17) {
$self->{ regs }[4] = ($self->{ regs }[2] / 256) - 1;
}
$self->{ regs }[$self->{ inst }]++;
}
return;
}
sub new {
my ($class, $input_file) = @_;
my $self = {
code => [],
regs => [ $part, 0, 0, 0, 0, 0 ],
inst => 0,
};
my $data = Path::Tiny::path( $input_file )->slurp_utf8();
($self->{ inst }) = $data =~ /\#ip (\d+)/sm;
while ($data =~ /(\w+ \d+ \d+ \d+)\n/smg) {
push @{ $self->{ code } }, $1;
}
bless $self, $class;
return $self;
}
}
my $input_file = $ARGV[0] || 'input21.txt';
my $program = Program->new( $input_file );
$program->run();
print join( ' ', @{ $program->{ regs } } ), "\n";
exit;