/
24.rs
269 lines (244 loc) · 8.83 KB
/
24.rs
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
use std::io::{self, BufRead};
#[derive(Debug)]
enum Opcode {
Inp,
Add,
Mul,
Div,
Mod,
Eql,
}
use Opcode::*;
// variable index: wxyz = 0123
type Var = usize;
#[derive(Debug, PartialEq, Eq)]
enum Arg {
Var(Var),
Num(i64),
}
const VAR_W: Var = 0;
const VAR_X: Var = 1;
const VAR_Y: Var = 2;
const VAR_Z: Var = 3;
const AVAR_W: Arg = Arg::Var(VAR_W);
const AVAR_X: Arg = Arg::Var(VAR_X);
const AVAR_Y: Arg = Arg::Var(VAR_Y);
const AVAR_Z: Arg = Arg::Var(VAR_Z);
#[derive(Debug)]
struct Instruction {
opcode: Opcode,
a: Var,
b: Arg,
}
#[derive(Debug, PartialEq)]
struct Machine {
vars: [i64; 4],
input: Vec<i64>,
}
fn step_instruction(machine: &mut Machine, inst: &Instruction) {
let aval = machine.vars[inst.a];
let bval = match inst.b {
Arg::Var(v) => machine.vars[v],
Arg::Num(n) => n
};
let dest = &mut machine.vars[inst.a];
*dest = match inst.opcode {
Inp => machine.input.pop().expect("out of input"),
Add => aval + bval,
Mul => aval * bval,
Div => aval / bval,
Mod => aval % bval,
Eql => (aval == bval) as i64,
};
}
fn execute(mach: &mut Machine, program: &[Instruction]) {
for inst in program {
step_instruction(mach, inst);
}
}
fn reg_z(program: &[Instruction], input: Vec<i64>) -> i64 {
let mut mach = Machine { vars: [0; 4], input };
execute(&mut mach, program);
mach.vars[3]
}
const MAX_DIGIT: i64 = 9;
const MIN_DIGIT: i64 = 1;
struct MonadLogic {
// from digit to digit and offset, e.g. digit9 == digit0 + 2
// offset is never negative, so (da, db, o) means da == db + o, o >= 0
rules: [(usize, usize, i64); 7],
}
impl MonadLogic {
fn from_slice(rules: &[(usize, usize, i64)]) -> MonadLogic {
assert_eq!(rules.len(), 7);
let mut r = [(0, 0, 0); 7];
r.copy_from_slice(&rules);
// positive offset
assert!(r.iter().all(|r| r.2 >= 0));
// all digits present
assert_eq!(r.iter().map(|r| r.0 + r.1).sum::<usize>(), (0..14).sum());
MonadLogic { rules: r }
}
fn decode_max(&self) -> Vec<i64> {
let mut digits = vec![0; 14];
for (da, db, offset) in self.rules {
digits[da] = MAX_DIGIT;
digits[db] = MAX_DIGIT - offset;
}
digits
}
fn decode_min(&self) -> Vec<i64> {
let mut digits = vec![0; 14];
for (da, db, offset) in self.rules {
digits[da] = MIN_DIGIT + offset;
digits[db] = MIN_DIGIT;
}
digits
}
}
fn validate_monad_number(program: &[Instruction], mut digits: Vec<i64>) -> i64 {
let digits_as_number = digits.iter().fold(0, |acc, x| acc * 10 + x);
digits.reverse();
assert_eq!(reg_z(program, digits), 0);
digits_as_number
}
fn largest_accepted_monad_number(program: &[Instruction], magic: &MonadLogic) -> i64 {
validate_monad_number(program, magic.decode_max())
}
fn smallest_accepted_monad_number(program: &[Instruction], magic: &MonadLogic) -> i64 {
validate_monad_number(program, magic.decode_min())
}
fn analyze_monad_program(program: &[Instruction]) -> MonadLogic {
let disasm = false;
if disasm {
println!("fn execute_native(mach: &mut Machine) {{");
println!("let mut z = 0;");
println!();
}
let mut stack = Vec::new();
let mut rules = Vec::new();
for (i, chunk) in program.chunks(18).enumerate() {
match chunk {
&[
Instruction { opcode: Inp, a: VAR_W, b: Arg::Num(0) },
Instruction { opcode: Mul, a: VAR_X, b: Arg::Num(0) },
Instruction { opcode: Add, a: VAR_X, b: AVAR_Z },
Instruction { opcode: Mod, a: VAR_X, b: Arg::Num(26) },
Instruction { opcode: Div, a: VAR_Z, b: Arg::Num(division) },
Instruction { opcode: Add, a: VAR_X, b: Arg::Num(compare) },
Instruction { opcode: Eql, a: VAR_X, b: AVAR_W },
Instruction { opcode: Eql, a: VAR_X, b: Arg::Num(0) },
Instruction { opcode: Mul, a: VAR_Y, b: Arg::Num(0) },
Instruction { opcode: Add, a: VAR_Y, b: Arg::Num(25) },
Instruction { opcode: Mul, a: VAR_Y, b: AVAR_X },
Instruction { opcode: Add, a: VAR_Y, b: Arg::Num(1) },
Instruction { opcode: Mul, a: VAR_Z, b: AVAR_Y },
Instruction { opcode: Mul, a: VAR_Y, b: Arg::Num(0) },
Instruction { opcode: Add, a: VAR_Y, b: AVAR_W },
Instruction { opcode: Add, a: VAR_Y, b: Arg::Num(offset) },
Instruction { opcode: Mul, a: VAR_Y, b: AVAR_X },
Instruction { opcode: Add, a: VAR_Z, b: AVAR_Y },
] => {
if division == 1 {
/*
* this looks like:
* if peek26() + 11 != digit0 {
* put26(14 + digit0);
* }
*
* Only put, no get. The condition is not suitable for 1..9 digits so branch is
* always taken in accepted direction
*/
stack.push((i, offset));
// not possible to take the branch because this holds
assert!(compare >= 9);
} else if division == 26 {
/*
* this looks like:
* if get26() + -3 != digit4 {
* put26(14 + digit4);
* }
* These must always pop one number so the comparisons must be made to hold, or
* else the stack (z) would hold at least one nonzero number at the end, making
* the model number invalid.
*/
let (j, joffset) = stack.pop().expect("pop always has space in this");
let total_offset = joffset + compare;
if disasm {
println!("// digit{} + {} + {} == digit{} == digit{} + {}",
j, joffset, compare, i,
j, total_offset);
}
let (smaller, bigger) = if total_offset >= 0 { (i, j) } else { (j, i) };
rules.push((smaller, bigger, total_offset.abs()));
} else {
panic!("what div");
}
if disasm {
println!("let digit = mach.input.pop().unwrap();");
match division {
1 => {
println!("if (z % 26) + {compare} != digit {{", compare=compare);
println!(" z *= 26;");
println!(" z += {offset} + digit;", offset=offset);
println!("}}");
},
_ => {
println!("if (z % 26) + {compare} != digit {{", compare=compare);
println!(" z /= {division};", division=division);
println!(" z *= 26;");
println!(" z += {offset} + digit;", offset=offset);
println!("}} else {{");
println!(" z /= {division};", division=division);
println!("}}");
},
};
println!();
}
}
// this is also good for printing the template for the above match before it existed
other => panic!("non-conformant chunk: {:#?}", other)
}
}
if disasm {
println!("mach.vars[3] = z;");
println!("}}");
}
MonadLogic::from_slice(&rules)
}
fn parse_instruction(input: &str) -> Instruction {
let mut sp = input.split(' ');
let opcode = match sp.next().unwrap() {
"inp" => Inp,
"add" => Add,
"mul" => Mul,
"div" => Div,
"mod" => Mod,
"eql" => Eql,
_ => panic!("bad opcode"),
};
let a = match sp.next().unwrap() {
"w" => 0,
"x" => 1,
"y" => 2,
"z" => 3,
_ => panic!("bad a"),
};
// note: placeholder "0" for inp that does not have a second parameter
let b = match sp.next().unwrap_or("0") {
"w" => Arg::Var(0),
"x" => Arg::Var(1),
"y" => Arg::Var(2),
"z" => Arg::Var(3),
n => Arg::Num(n.parse().unwrap())
};
Instruction { opcode, a, b }
}
fn main() {
let program: Vec<_> = io::stdin().lock().lines()
.map(|line| parse_instruction(&line.unwrap()))
.collect();
let magic = analyze_monad_program(&program);
println!("{}", largest_accepted_monad_number(&program, &magic));
println!("{}", smallest_accepted_monad_number(&program, &magic));
}