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
|
extern crate regex;
use regex::Regex;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
enum Instruction {
CpyLit(i32, usize),
CpyReg(usize, usize),
Inc(usize),
Dec(usize),
Jnz(usize, i32),
Jmp(i32),
Noop
}
impl Instruction {
fn parse(line: &str) -> Instruction {
let cpy_lit: Regex = Regex::new(r"cpy ([-\d]+) (a|b|c|d)").unwrap();
let cpy_reg = Regex::new(r"cpy (a|b|c|d) (a|b|c|d)").unwrap();
let inc = Regex::new(r"inc (a|b|c|d)").unwrap();
let dec = Regex::new(r"dec (a|b|c|d)").unwrap();
let jnz_lit = Regex::new(r"jnz ([-\d]+) ([-\d]+)").unwrap();
let jnz_reg = Regex::new(r"jnz (a|b|c|d) ([-\d]+)").unwrap();
if cpy_lit.is_match(line) {
let cap = cpy_lit.captures(line).unwrap();
let src: i32 = cap.at(1).unwrap().parse().unwrap();
let dest = to_register_index(cap.at(2).unwrap());
Instruction::CpyLit(src, dest)
}
else if cpy_reg.is_match(line) {
let cap = cpy_reg.captures(line).unwrap();
let src = to_register_index(cap.at(1).unwrap());
let dest = to_register_index(cap.at(2).unwrap());
Instruction::CpyReg(src, dest)
}
else if inc.is_match(line) {
let cap = inc.captures(line).unwrap();
let dest = to_register_index(cap.at(1).unwrap());
Instruction::Inc(dest)
}
else if dec.is_match(line) {
let cap = dec.captures(line).unwrap();
let dest = to_register_index(cap.at(1).unwrap());
Instruction::Dec(dest)
}
else if jnz_lit.is_match(line) {
let cap = jnz_lit.captures(line).unwrap();
let condition: i32 = cap.at(1).unwrap().parse().unwrap();
let offset: i32 = cap.at(2).unwrap().parse().unwrap();
if condition != 0 {
Instruction::Jmp(offset)
}
else {
Instruction::Noop
}
}
else if jnz_reg.is_match(line) {
let cap = jnz_reg.captures(line).unwrap();
let condition = to_register_index(cap.at(1).unwrap());
let offset: i32 = cap.at(2).unwrap().parse().unwrap();
Instruction::Jnz(condition, offset)
}
else {
panic!("Invalid instruction line")
}
}
}
fn main() {
let program = read_file();
let mut registers: [i32; 4] = [0, 0, 1, 0];
let mut pc: usize = 0;
while pc < program.len() {
let mut pc_next: usize = pc+1;
match program[pc] {
Instruction::CpyLit(src, dest) => {
registers[dest] = src;
},
Instruction::CpyReg(src, dest) => {
registers[dest] = registers[src];
},
Instruction::Inc(dest) => {
registers[dest] += 1;
},
Instruction::Dec(dest) => {
registers[dest] -= 1;
},
Instruction::Jnz(condition, offset) => {
if registers[condition] != 0 {
pc_next = (pc as i32 + offset) as usize;
}
},
Instruction::Jmp(offset) => {
pc_next = (pc as i32 + offset) as usize
},
Instruction::Noop => {}
}
pc = pc_next;
}
println!("a: {}, b: {}, c: {}, d: {}", registers[0], registers[1], registers[2], registers[3]);
}
fn to_register_index(name: &str) -> usize {
match name {
"a" => 0,
"b" => 1,
"c" => 2,
"d" => 3,
_ => panic!("Invalid register provided")
}
}
fn read_file() -> Vec<Instruction> {
let file = BufReader::new(File::open("input.txt").unwrap());
file.lines()
.map(|line| line.unwrap())
.filter(|line| line.len() > 0)
.map(|line| Instruction::parse(line.trim()))
.collect()
}
|