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
|
use std::io;
use std::io::BufRead;
#[derive(Copy, Clone)]
enum Opcode {
Addr, Addi,
Mulr, Muli,
Banr, Bani,
Borr, Bori,
Setr, Seti,
Gtir, Gtri, Gtrr,
Eqir, Eqri, Eqrr,
}
impl Opcode {
fn from_id(id: u16) -> Opcode {
use self::Opcode::*;
static VALUES: [Opcode; 16] = [Addr, Addi, Mulr, Muli, Banr, Bani, Borr, Bori, Setr, Seti, Gtir, Gtri, Gtrr, Eqir, Eqri, Eqrr];
VALUES[id as usize]
}
}
struct Instr {
op: usize,
a: usize,
b: usize,
c: usize,
}
impl Instr {
fn from(line: &str) -> Self {
let mut iter = line.split(' ').map(|s| s.parse().unwrap());
let op = iter.next().unwrap();
let a = iter.next().unwrap();
let b = iter.next().unwrap();
let c = iter.next().unwrap();
Instr { op, a, b, c }
}
}
#[derive(PartialEq, Eq, Clone)]
struct Machine {
regs: [usize; 4],
}
impl Machine {
fn from(line: &str) -> Self {
let mut machine = Machine { regs: [0; 4] };
let mut i = 0;
for num in line.split(", ").map(|s| s.parse().unwrap()) {
machine.regs[i] = num;
i += 1;
}
machine
}
fn exec(&mut self, op: Opcode, a: usize, b: usize, c: usize) {
match op {
Opcode::Addr => self.regs[c] = self.regs[a] + self.regs[b],
Opcode::Addi => self.regs[c] = self.regs[a] + b,
Opcode::Mulr => self.regs[c] = self.regs[a] * self.regs[b],
Opcode::Muli => self.regs[c] = self.regs[a] * b,
Opcode::Banr => self.regs[c] = self.regs[a] & self.regs[b],
Opcode::Bani => self.regs[c] = self.regs[a] & b,
Opcode::Borr => self.regs[c] = self.regs[a] | self.regs[b],
Opcode::Bori => self.regs[c] = self.regs[a] | b,
Opcode::Setr => self.regs[c] = self.regs[a],
Opcode::Seti => self.regs[c] = a,
Opcode::Gtir => self.regs[c] = if a > self.regs[b] { 1 } else { 0 },
Opcode::Gtri => self.regs[c] = if self.regs[a] > b { 1 } else { 0 },
Opcode::Gtrr => self.regs[c] = if self.regs[a] > self.regs[b] { 1 } else { 0 },
Opcode::Eqir => self.regs[c] = if a == self.regs[b] { 1 } else { 0 },
Opcode::Eqri => self.regs[c] = if self.regs[a] == b { 1 } else { 0 },
Opcode::Eqrr => self.regs[c] = if self.regs[a] == self.regs[b] { 1 } else { 0 },
}
}
}
struct Sample {
before: Machine,
after: Machine,
instr: Instr,
}
impl Sample {
fn behaves_as(&self) -> u16 {
let mut r = 0;
for id in 0..16 {
let op = Opcode::from_id(id);
let mut m = self.before.clone();
m.exec(op, self.instr.a, self.instr.b, self.instr.c);
if m == self.after {
r |= 1 << id;
}
}
r
}
}
fn parse_input<T: BufRead>(reader: T) -> io::Result<(Vec<Sample>, Vec<Instr>)> {
let mut line_iter = reader.lines();
let mut samples = Vec::new();
let program;
loop {
let line = line_iter.next().unwrap()?;
if line.len() == 0 {
line_iter.next();
program = line_iter.map(|l| Instr::from(&l.unwrap())).collect::<Vec<_>>();
break;
} else if &line[..1] == "B" {
let before = Machine::from(&line[9..line.len()-1]);
let line = line_iter.next().unwrap()?;
let instr = Instr::from(&line);
let line = line_iter.next().unwrap()?;
let after = Machine::from(&line[9..line.len()-1]);
samples.push(Sample { before, after, instr });
line_iter.next();
} else {
assert!(false);
}
}
Ok((samples, program))
}
fn resolve_helper(masks: &[u16; 16], res: &mut [u16; 16], at: usize, available: u16) -> bool {
if at == 16 {
return true;
}
let mut walker = available;
while walker != 0 {
let n = walker.trailing_zeros();
let bit = 1 << n;
walker &= !bit;
if (masks[at] & bit) != 0 {
res[at] = n as u16;
if resolve_helper(masks, res, at + 1, available & !bit) {
return true;
}
}
}
false
}
fn resolve_mapping(masks: &[u16; 16]) -> [u16; 16] {
let mut res = [0; 16];
assert!(resolve_helper(masks, &mut res, 0, 0xffff));
res
}
pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> {
let (samples, program) = parse_input(reader)?;
let mut num_geq_3 = 0;
let mut code_masks = [0xffff; 16];
for sample in samples {
let mask = sample.behaves_as();
if mask.count_ones() >= 3 {
num_geq_3 += 1;
}
code_masks[sample.instr.op] &= mask;
}
let part1 = num_geq_3;
let mapping = resolve_mapping(&code_masks);
let mut m = Machine { regs: [0; 4] };
for instr in program {
m.exec(Opcode::from_id(mapping[instr.op]), instr.a, instr.b, instr.c);
}
let part2 = m.regs[0];
Ok((part1.to_string(), part2.to_string()))
}
|