Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
384 lines (368 loc) Β· 17.2 KB
/
Copy pathmain.rs
File metadata and controls
384 lines (368 loc) Β· 17.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use std::{
fmt::{Display, Formatter},
fs,
};
#[derive(Clone, Copy, PartialEq, Eq)]
enum Input {
Value(i64),
Register(usize),
}
impl Display for Input {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Input::Value(v) => write!(f, "{v}"),
Input::Register(r) => write!(f, "{}", (b'a' + (*r as u8)) as char),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Instruction {
Cpy { input: Input, register: Input },
Inc { register: Input, offset: i64 },
Jnz { input: Input, offset: Input },
Tgl { input: Input },
Out { input: Input },
}
impl Display for Instruction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Instruction::Cpy { input, register } => write!(f, "cpy {input} {register}"),
Instruction::Inc { register, offset } => {
if *offset == 1 {
write!(f, "inc {register}")
} else if *offset == -1 {
write!(f, "dec {register}")
} else {
write!(f, "inc {register} {offset}")
}
}
Instruction::Jnz { input, offset } => write!(f, "jnz {input} {offset}"),
Instruction::Tgl { input } => write!(f, "tgl {input}"),
Instruction::Out { input } => write!(f, "out {input}"),
}
}
}
fn parse_input(s: &str) -> Input {
match s {
"a" => Input::Register(0),
"b" => Input::Register(1),
"c" => Input::Register(2),
"d" => Input::Register(3),
_ => Input::Value(s.parse().unwrap()),
}
}
fn main() {
let input = fs::read_to_string("input.txt").expect("Could not read file");
let instructions = input
.lines()
.map(|l| {
let mut parts = l.split_ascii_whitespace();
let instruction = parts.next().unwrap();
match instruction {
"cpy" => Instruction::Cpy {
input: parse_input(parts.next().unwrap()),
register: parse_input(parts.next().unwrap()),
},
"inc" => Instruction::Inc {
register: parse_input(parts.next().unwrap()),
offset: 1,
},
"dec" => Instruction::Inc {
register: parse_input(parts.next().unwrap()),
offset: -1,
},
"jnz" => Instruction::Jnz {
input: parse_input(parts.next().unwrap()),
offset: parse_input(parts.next().unwrap()),
},
"tgl" => Instruction::Tgl {
input: parse_input(parts.next().unwrap()),
},
"out" => Instruction::Out {
input: parse_input(parts.next().unwrap()),
},
_ => panic!("Unknown instruction: {instruction}"),
}
})
.collect::<Vec<_>>();
// we assume 100 is enough to check if the sequence is infinite
let max_steps = 100;
for i in 0..i64::MAX {
let mut instructions = instructions.clone();
let mut registers: [i64; 4] = [0; 4];
registers[0] = i;
let mut ip = 0;
let mut steps = 0;
let mut current = 1;
while steps < max_steps && ip < instructions.len() {
match instructions[ip] {
Instruction::Cpy { input, register } => {
if let Input::Register(register) = register {
match input {
Input::Value(v) => registers[register] = v,
Input::Register(r) => registers[register] = registers[r],
}
}
ip += 1;
}
Instruction::Inc { register, offset } => {
if let Input::Register(register) = register {
registers[register] += offset;
}
ip += 1;
}
Instruction::Jnz { input, offset } => {
let v = match input {
Input::Value(v) => v,
Input::Register(r) => registers[r],
};
if v != 0 {
let offset = match offset {
Input::Value(value) => value,
Input::Register(register) => registers[register],
} as isize;
// JIT-optimization:
if offset == -2
&& ip > 1
&& let Instruction::Inc {
register: Input::Register(r1),
offset: inc_offset1,
} = instructions[ip - 2]
&& let Instruction::Inc {
register: Input::Register(r2),
offset: inc_offset2,
} = instructions[ip - 1]
{
// quick inc
registers[r1] += v * inc_offset1;
registers[r2] += v * inc_offset2;
ip += 1;
} else if offset == -4
&& ip > 3
// our input must be a non-zero value
&& let Input::Value(my_input) = input
&& my_input != 0
// the first instruction must be a jnz comparing a
// register and its offset must be 2
&& let Instruction::Jnz {
input: Input::Register(jnz1_r),
offset: Input::Value(jnz1_offset),
} = instructions[ip - 4]
&& jnz1_offset == 2
// the second instruction must be a jnz with a
// non-zero input and an offset of 4
&& let Instruction::Jnz {
input: Input::Value(jnz2_v),
offset: Input::Value(jnz2_offset),
} = instructions[ip - 3]
&& jnz2_v != 0
&& jnz2_offset == 4
// the third and fourth instructions must be inc or
// dec
&& let Instruction::Inc {
register: Input::Register(inc1_r),
offset: inc1_offset,
} = instructions[ip - 2]
&& let Instruction::Inc {
register: Input::Register(inc2_r),
offset: inc2_offset,
} = instructions[ip - 1]
// at least one of them must decrement the register
// from the first jnz instruction
&& ((inc1_r == jnz1_r && inc1_offset == -1)
|| (inc2_r == jnz1_r && inc2_offset == -1))
{
// quick add/subtract
if inc1_r == jnz1_r {
registers[inc2_r] += registers[inc1_r] * inc2_offset;
registers[inc1_r] = 0;
} else {
registers[inc1_r] += registers[inc2_r] * inc1_offset;
registers[inc2_r] = 0;
}
ip += 1;
} else if offset == -7
&& ip > 6
// our input must be a non-zero value
&& let Input::Value(my_input) = input
&& my_input != 0
// the first instruction must be a cpy
&& let Instruction::Cpy {
input: cpy_input,
register: Input::Register(cpy_dest),
} = instructions[ip - 7]
// the second instruction must be a jnz comparing a
// register and its offset must be 2
&& let Instruction::Jnz {
input: Input::Register(jnz1_r),
offset: Input::Value(jnz1_offset),
} = instructions[ip - 6]
&& jnz1_offset == 2
// the third instruction must be a jnz with a
// non-zero input and an offset of 6
&& let Instruction::Jnz {
input: Input::Value(jnz2_v),
offset: Input::Value(jnz2_offset),
} = instructions[ip - 5]
&& jnz2_v != 0
&& jnz2_offset == 6
// the fourth and fifth instructions must be decs
&& let Instruction::Inc {
register: Input::Register(inc1_r),
offset: inc1_offset,
} = instructions[ip - 4]
&& inc1_offset == -1
&& let Instruction::Inc {
register: Input::Register(inc2_r),
offset: inc2_offset,
} = instructions[ip - 3]
&& inc2_offset == -1
// one of them must decrement the register from the
// first jnz instruction and the other one
// decrement the register from the cpy instruction
&& ((inc1_r == jnz1_r && inc2_r == cpy_dest) || (inc2_r == jnz1_r && inc1_r == cpy_dest))
// the sixth instruction must be a jnz comparing the
// other register and its offset must be -4
&& let Instruction::Jnz {
input: Input::Register(jnz3_r),
offset: Input::Value(jnz3_offset),
} = instructions[ip - 2]
&& jnz3_offset == -4
&& ((inc1_r == jnz1_r && jnz3_r == inc2_r) || (inc2_r == jnz1_r && jnz3_r == inc1_r))
// the 7th instruction must increment a register
&& let Instruction::Inc {
register: Input::Register(inc3_r),
offset: inc3_offset,
} = instructions[ip - 1]
&& inc3_offset == 1
{
// quick division
let cpy_source = match cpy_input {
Input::Value(v) => v,
Input::Register(r) => registers[r],
};
let orig_input = registers[jnz1_r] + cpy_source;
registers[inc3_r] = orig_input / cpy_source;
let remainder = orig_input % cpy_source;
registers[jnz3_r] = if remainder == 0 {
cpy_source
} else {
remainder
};
registers[jnz1_r] = 0;
ip += 1;
} else if offset == -5
&& ip > 4
// first instruction must be a cpy
&& let Instruction::Cpy {
input: cpy_input,
register: Input::Register(cpy_dest),
} = instructions[ip - 5]
// second and third instructions must be inc/dec
&& let Instruction::Inc {
register: Input::Register(inc1_r),
offset: inc_offset1,
} = instructions[ip - 4]
&& let Instruction::Inc {
register: Input::Register(inc2_r),
offset: inc_offset2,
} = instructions[ip - 3]
// one of these two must be an inc and the other
// must be a dec
&& inc_offset1 == -inc_offset2
// fourth instruction must be a jnz
&& let Instruction::Jnz {
input: Input::Register(jnz_r),
offset: Input::Value(jnz_v),
} = instructions[ip - 2]
// this jnz must use the register that is decremented
&& ((inc_offset1 == -1 && jnz_r == inc1_r)
|| (inc_offset2 == -1 && jnz_r == inc2_r))
// and the jnz must have an offset of -2
&& jnz_v == -2
// fifth instruction must be a dec
&& let Instruction::Inc {
register: Input::Register(dec2_r),
offset: inc_offset3,
} = instructions[ip - 1]
&& inc_offset3 == -1
// and this dec must affect our input
&& let Input::Register(my_r) = input
&& dec2_r == my_r
{
// quick multiplication as hinted in the problem
// statement
let cpy_source = match cpy_input {
Input::Value(v) => v,
Input::Register(r) => registers[r],
};
if inc_offset1 == -1 {
registers[inc2_r] += cpy_source * registers[my_r];
} else {
registers[inc1_r] += cpy_source * registers[my_r];
}
registers[cpy_dest] = 0;
registers[my_r] = 0;
ip += 1;
} else {
// execute the instruction normally
ip = ip.checked_add_signed(offset).unwrap();
}
} else {
ip += 1;
}
}
Instruction::Tgl { input } => {
let j = ip
+ match input {
Input::Value(value) => value,
Input::Register(register) => registers[register],
} as usize;
if j < instructions.len() {
let target_instr = &mut instructions[j];
*target_instr = match target_instr {
Instruction::Cpy { input, register } => Instruction::Jnz {
input: *input,
offset: *register,
},
Instruction::Inc { register, offset } => Instruction::Inc {
register: *register,
offset: -*offset,
},
Instruction::Jnz { input, offset } => Instruction::Cpy {
input: *input,
register: *offset,
},
Instruction::Tgl { input } => Instruction::Inc {
register: *input,
offset: 1,
},
Instruction::Out { input } => Instruction::Inc {
register: *input,
offset: 1,
},
};
}
ip += 1;
}
Instruction::Out { input } => {
let v = match input {
Input::Value(v) => v,
Input::Register(r) => registers[r],
};
if (current == 0 && v == 1) || (current == 1 && v == 0) {
current = v;
steps += 1;
} else {
break;
}
ip += 1;
}
}
}
if steps == max_steps {
println!("{i}");
break;
}
}
}