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
|
use std::env;
use std::io::{self, BufRead, BufReader};
use std::fs::File;
type BusWidth = i128;
type IOType = i128;
type IOData = Vec<IOType>;
fn read_program(filename: &str) -> Vec<BusWidth> {
let f = File::open(filename).expect(filename);
let f = BufReader::new(f);
f.lines()
.filter(|line| match line {
Ok(s) => {
let b = s.as_bytes();
if b.len() == 0 || b.len() > 0 && b[0] == b'#' { false } else { true }
},
_ => true,
})
.map(|line| match line {
Ok(line) => {
let data:Vec<BusWidth> = line.split(',').map(|e| { e.trim_start().trim_end() })
.filter(|v| match v.parse::<BusWidth>() { Ok(_) => true, _ => {
eprintln!("Could not parse: {:?}", v);
false
},
})
.map(|r| r.parse().unwrap())
.collect();
data
},
Err(_) => panic!("Could not parse {}.", filename),
}).flatten().collect()
}
#[derive(Clone, Debug)]
struct Memory {
mem: Vec<BusWidth>,
}
impl Memory {
fn new() -> Memory {
Memory { mem: Vec::new(), }
}
fn init(&mut self, mem:Vec<BusWidth>) {
self.mem = mem;
}
fn read(&self, address: usize) -> BusWidth {
match self.mem.get(address) {
Some(value) => *value,
None => 0,
}
}
fn write(&mut self, address: usize, value: BusWidth) {
if self.len() < address {
self.mem.resize(address*2, 0);
}
self.mem[address] = value;
}
fn len(&self) -> usize {
self.mem.len()
}
}
#[derive(Debug)]
struct Registers {
ip: usize,
base: usize,
}
#[derive(Debug)]
enum Arithm {
Add,
Mul,
LessThan,
Equals,
}
#[derive(Debug)]
enum IO {
In,
Out,
}
#[derive(Debug)]
enum Jmp {
JmpTrue,
JmpFalse,
}
#[derive(Debug)]
enum RegOp {
BaseMod,
}
#[derive(Debug, PartialEq)]
enum ParameterMode {
Position,
Immediate,
Relative,
}
#[derive(Debug, PartialEq)]
enum DataDirection {
Read,
Write,
}
#[derive(PartialEq)]
enum IOMethod {
StdIO,
Variables,
}
struct Intcode {
r: Registers,
io_method: IOMethod,
input: IOData,
output: IOData,
}
impl Intcode {
fn new() -> Intcode {
Intcode {
r: Registers { ip: 0, base: 0, },
io_method: IOMethod::Variables,
input: vec![],
output: vec![],
}
}
fn param_to_argument(&self, mode: &ParameterMode, mem: &Memory, offset: usize,
dir: &DataDirection) -> usize
{
match mode {
ParameterMode::Position => {
if mem.read(offset) < 0 {
panic!("Attempt to address negative memory at IP: {}");
}
mem.read(offset) as usize
},
ParameterMode::Immediate => {
if *dir == DataDirection::Write {
panic!("Immediate parameter mode at IP: {}", self.r.ip);
}
offset
},
ParameterMode::Relative => {
if (mem.read(offset) + self.r.base as BusWidth) < 0 {
panic!("Attempt to address negative memory at IP: {}");
}
(mem.read(offset) + self.r.base as BusWidth) as usize
},
}
}
fn arithm(&self, op: Arithm, modes: Vec<ParameterMode>, memory: &mut Memory) {
let parameter_directions =
[DataDirection::Read, DataDirection::Read, DataDirection::Write];
let params:Vec<usize> = (0..parameter_directions.len())
.zip(parameter_directions.iter())
.map(|(p, d)| {
self.param_to_argument(&modes[p], memory, self.r.ip + 1 + p, d)
}).collect();
let left = params[0];
let right = params[1];
let result = params[2];
match op {
Arithm::Add => memory.write(result, memory.read(left) + memory.read(right)),
Arithm::Mul => memory.write(result, memory.read(left) * memory.read(right)),
Arithm::LessThan => memory.write(result, if memory.read(left) < memory.read(right) {
1 } else { 0 }),
Arithm::Equals => memory.write(result, if memory.read(left) == memory.read(right) {
1 } else { 0 }),
}
}
fn jmp(&mut self, op: Jmp, modes: Vec<ParameterMode>, memory: &mut Memory)
{
let parameter_directions = [DataDirection::Read, DataDirection::Read];
let params:Vec<usize> = (0..parameter_directions.len())
.zip(parameter_directions.iter())
.map(|(p, d)| {
self.param_to_argument(&modes[p], memory, self.r.ip + 1 + p, d)
}).collect();
let value = params[0];
let address = params[1];
self.r.ip = match op {
Jmp::JmpTrue => (if memory.read(value) != 0 {
memory.read(address) as usize } else { self.r.ip + 3 }),
Jmp::JmpFalse => (if memory.read(value) == 0 {
memory.read(address) as usize } else { self.r.ip + 3 }),
};
}
fn regop(&mut self, op: RegOp, modes: Vec<ParameterMode>, memory: &mut Memory)
{
let parameter_directions = [DataDirection::Read];
let params:Vec<usize> = (0..parameter_directions.len())
.zip(parameter_directions.iter())
.map(|(p, d)| {
self.param_to_argument(&modes[p], memory, self.r.ip + 1 + p, d)
}).collect();
let value = memory.read(params[0]);
match op {
RegOp::BaseMod => {
if value < 0 {
self.r.base -= (-1 * value) as usize;
} else {
self.r.base += value as usize;
}
},
};
}
fn io_in(&mut self, _op: IO, modes: Vec<ParameterMode>, memory:&mut Memory)
{
let parameter_directions = [DataDirection::Write];
let params:Vec<usize> = (0..parameter_directions.len())
.zip(parameter_directions.iter())
.map(|(p, d)| {
self.param_to_argument(&modes[p], memory, self.r.ip + 1 + p, d)
}).collect();
let address = params[0];
let stdin = io::stdin();
match &self.io_method {
IOMethod::StdIO => {
let mut value: Option<BusWidth> = None;
while {
stdin.lock().lines().next().map(|s| {
match s {
Ok(v) => {
let parsed_string = v.trim_end().trim_start().parse::<BusWidth>();
match parsed_string {
Ok(v) => { value = Some(v); value },
_ => None,
};
value
},
_ => { None },
}
});
if value == None {
eprintln!("Could not parse input, please retry.");
true
} else {
false
}
} {}
match value {
Some(v) => memory.write(address, v),
_ => {},
};
},
IOMethod::Variables => {
memory.write(address, self.input.remove(0) as BusWidth);
},
}
}
fn io_out(&mut self, _op: IO, modes: Vec<ParameterMode>, memory:&mut Memory )
{
let parameter_directions = [DataDirection::Read];
let params:Vec<usize> = (0..parameter_directions.len())
.zip(parameter_directions.iter())
.map(|(p, d)| {
self.param_to_argument(&modes[p], memory, self.r.ip + 1 + p, d)
}).collect();
let address = params[0];
let o = memory.read(address);
println!("{}", o);
match &self.io_method {
IOMethod::Variables => self.output.push(o as IOType),
_ => {},
}
}
fn intcode(&mut self, mut memory:Memory)
{
while self.r.ip < memory.len() {
let op = memory.read(self.r.ip) % 100;
let modes:Vec<ParameterMode> = (2..=4)
.map(|e| memory.read(self.r.ip) / 10i32.pow(e) as BusWidth % 10)
.map(|n| match n {
0 => ParameterMode::Position,
1 => ParameterMode::Immediate,
2 => ParameterMode::Relative,
p => panic!("Invalid parameter mode {} at IP: {}", p, self.r.ip),
}).collect();
match op {
1 => { self.arithm(Arithm::Add, modes, &mut memory); self.r.ip += 4 },
2 => { self.arithm(Arithm::Mul, modes, &mut memory); self.r.ip += 4 },
7 => { self.arithm(Arithm::LessThan, modes, &mut memory); self.r.ip += 4 },
8 => { self.arithm(Arithm::Equals, modes, &mut memory); self.r.ip += 4 },
3 => { self.io_in( IO::In, modes, &mut memory); self.r.ip += 2 },
4 => { self.io_out(IO::Out, modes, &mut memory); self.r.ip += 2 },
5 => { self.jmp( Jmp::JmpTrue, modes, &mut memory); },
6 => { self.jmp( Jmp::JmpFalse, modes, &mut memory); },
9 => { self.regop(RegOp::BaseMod, modes, &mut memory); self.r.ip += 2},
99 => break,
op => panic!("Invalid operation {} at IP: {}", op, self.r.ip),
}
}
}
}
fn main() {
let args: Vec<String> = env::args().collect();
let mut opts = getopts::Options::new();
opts.reqopt("m", "mode", "Mode of IO operations", "[arg|stdio]");
opts.reqopt("p", "program", "File to load program from", "FILE");
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => { panic!(f.to_string()) }
};
let program:String = matches.opt_str("program").unwrap();
let mode = matches.opt_str("mode").unwrap();
let mut memory = Memory::new();
memory.init(read_program(&program));
let mut cpu = Intcode::new();
if mode == "arg" {
cpu.io_method = IOMethod::Variables;
let input:IOData = matches.free[0].split(",").map(|v| {
let i = v.trim_end().parse().unwrap();
i
}).collect();
cpu.input = input;
while cpu.input.len() > 1 {
cpu.intcode(memory.clone());
}
} else if mode == "stdio" {
cpu.io_method = IOMethod::StdIO;
cpu.intcode(memory.clone());
} else {
panic!("Unknown mode: {}", mode);
}
}
|