/
main.rs
234 lines (210 loc) · 5.49 KB
/
main.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
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)]
pub enum Register {
A,
B,
C,
D,
Literal(isize),
}
impl FromStr for Register {
type Err = ();
fn from_str(s: &str) -> Result<Register, ()> {
match s {
"a" => Ok(Register::A),
"b" => Ok(Register::B),
"c" => Ok(Register::C),
"d" => Ok(Register::D),
_ => s.parse::<isize>()
.map(Register::Literal)
.map_err(|_| ()),
}
}
}
impl Register {
pub fn is_reg(&self) -> bool {
use Register::*;
match *self {
A | B | C | D => true,
_ => false
}
}
pub fn is_lit(&self) -> bool {
use Register::*;
match *self {
A | B | C |D => false,
_ => true
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum Instruction {
Cpy(Register, Register),
Inc(Register),
Dec(Register),
Jnz(Register, Register),
Tgl(Register),
}
impl Instruction {
pub fn from_str(s: &str) -> Instruction {
let parts = s.split_whitespace().collect::<Vec<_>>();
match parts[0] {
"cpy" => Instruction::Cpy(parts[1].parse().unwrap(),
parts[2].parse().unwrap()),
"inc" => Instruction::Inc(parts[1].parse().unwrap()),
"dec" => Instruction::Dec(parts[1].parse().unwrap()),
"jnz" => Instruction::Jnz(parts[1].parse().unwrap(),
parts[2].parse().unwrap()),
"tgl" => Instruction::Tgl(parts[1].parse().unwrap()),
_ => unreachable!(),
}
}
}
#[derive(Debug)]
pub struct Cpu {
pub a: isize,
pub b: isize,
pub c: isize,
pub d: isize,
pub ptr: usize,
pub instructions: Vec<Instruction>,
}
impl Cpu {
pub fn new(instrs: Vec<Instruction>) -> Cpu {
Cpu {
a: 0,
b: 0,
c: 0,
d: 0,
ptr: 0,
instructions: instrs,
}
}
fn get_reg(&self, reg: Register) -> isize {
use Register::*;
match reg {
A => self.a,
B => self.b,
C => self.c,
D => self.d,
Literal(x) => x,
}
}
fn set_reg(&mut self, reg: Register, val: isize) {
match reg {
Register::A => self.a = val,
Register::B => self.b = val,
Register::C => self.c = val,
Register::D => self.d = val,
_ => ()
}
}
fn update_reg<F>(&mut self, reg: Register, f: F)
where F: FnOnce(isize) -> isize
{
let old_val = self.get_reg(reg);
self.set_reg(reg, f(old_val));
}
fn exec(&mut self, instr: Instruction) {
use Instruction::*;
match instr {
Cpy(src, dst) => {
let src_val = self.get_reg(src);
self.set_reg(dst, src_val);
self.ptr += 1
},
Inc(reg) => { self.update_reg(reg, |x| x + 1); self.ptr += 1 },
Dec(reg) => { self.update_reg(reg, |x| x - 1); self.ptr += 1},
Jnz(src, offset) => {
if self.get_reg(src) != 0 {
self.ptr = (self.ptr as isize + self.get_reg(offset)) as usize;
} else {
self.ptr += 1;
}
},
Tgl(offset) => {
let idx = (self.get_reg(offset) + (self.ptr as isize)) as usize;
if idx >= self.instructions.len() {
self.ptr += 1;
return;
}
let target_instr = self.instructions[idx];
let new_instr = match target_instr {
Inc(x) => Dec(x),
Dec(x) => Inc(x),
Tgl(x) => {
if x.is_reg() {
Inc(x)
} else {
Tgl(x)
}
},
Jnz(x, y) => {
if y.is_reg() {
Cpy(x, y)
} else {
Jnz(x, y)
}
},
Cpy(x, y) => {
Jnz(x, y)
}
};
self.instructions[idx] = new_instr;
self.ptr += 1;
},
}
}
pub fn run(&mut self) {
while self.ptr < self.instructions.len() {
let instr = self.instructions[self.ptr];
self.exec(instr);
}
}
}
const TEST_INPUT: &'static str = "cpy 2 a
tgl a
tgl a
tgl a
cpy 1 a
dec a
dec a";
const PUZZLE_INPUT: &'static str = "cpy a b
dec b
cpy a d
cpy 0 a
cpy b c
inc a
dec c
jnz c -2
dec d
jnz d -5
dec b
cpy b c
cpy c d
dec d
inc c
jnz d -2
tgl c
cpy -16 c
jnz 1 c
cpy 95 c
jnz 73 d
inc a
inc d
jnz d -2
inc c
jnz c -5";
fn main() {
let mut cpu = Cpu::new(TEST_INPUT.lines().map(Instruction::from_str).collect());
cpu.run();
println!("Test machine output: {}", cpu.a);
cpu = Cpu::new(PUZZLE_INPUT.lines().map(Instruction::from_str).collect());
cpu.a = 7;
cpu.run();
println!("Challenge machine output: {}", cpu.a);
cpu = Cpu::new(PUZZLE_INPUT.lines().map(Instruction::from_str).collect());
cpu.a = 12;
cpu.run();
println!("Challenge machine output part 2: {}", cpu.a);
}