-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
176 lines (156 loc) · 3.94 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
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(|_| ()),
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum Instruction {
Cpy(Register, Register),
Inc(Register),
Dec(Register),
Jnz(Register, isize),
}
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()),
_ => 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 + offset) as usize;
} else {
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 41 a
inc a
inc a
dec a
jnz a 2
dec a";
const PUZZLE_INPUT: &'static str = "cpy 1 a
cpy 1 b
cpy 26 d
jnz c 2
jnz 1 5
cpy 7 c
inc d
dec c
jnz c -2
cpy a c
inc a
dec b
jnz b -2
cpy c b
dec d
jnz d -6
cpy 16 c
cpy 12 d
inc a
dec d
jnz d -2
dec 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.run();
println!("Challenge machine output: {}", cpu.a);
cpu = Cpu::new(PUZZLE_INPUT.lines().map(Instruction::from_str).collect());
cpu.c = 1;
cpu.run();
println!("Challenge machine output part 2: {}", cpu.a);
}