-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21.rs
More file actions
228 lines (198 loc) · 8.44 KB
/
Copy pathday21.rs
File metadata and controls
228 lines (198 loc) · 8.44 KB
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
use itertools::Itertools;
use std::collections::{HashMap, VecDeque};
use std::fs;
pub(crate) fn day21() {
let input = fs::read_to_string("input/2019/day21/input.txt").unwrap();
let code: Vec<i64> = input.trim().split(',').map(|c| c.parse().unwrap()).collect();
part_a(&code);
part_b(&code);
}
fn part_a(code: &Vec<i64>) {
// patterns
// #####.###########
// #####..#.########
// #####...#########
let mut input = VecDeque::new();
"NOT C J\n".chars().for_each(|c| input.push_back(c as i64)); // If there is a gap on the 3rd
"AND D J\n".chars().for_each(|c| input.push_back(c as i64)); // but hull on 4th place => we can jump
"NOT A T\n".chars().for_each(|c| input.push_back(c as i64)); // Or if there is a gap immediately in front of us
"OR T J\n".chars().for_each(|c| input.push_back(c as i64)); // => we can jump
"WALK\n".chars().for_each(|c| input.push_back(c as i64));
let mut intcode = intcode_instance(&code);
intcode.run(&mut input);
println!("{}", intcode.output.iter().map(|c| *c as u8 as char).join(""));
println!("{}", *intcode.output.last().unwrap() as i64);
}
fn part_b(code: &Vec<i64>) {
// patterns
// #####.##.#.##.###
// #####..#.########
// #####...#########
let mut input = VecDeque::new();
"NOT C T\n".chars().for_each(|c| input.push_back(c as i64)); // If there is a gap on 3rd place
"AND D T\n".chars().for_each(|c| input.push_back(c as i64)); // but a hull on 4th place
"AND H J\n".chars().for_each(|c| input.push_back(c as i64)); // and on 8th place,
"OR T J\n".chars().for_each(|c| input.push_back(c as i64)); // we can jump
"NOT B T\n".chars().for_each(|c| input.push_back(c as i64)); // If there is a gap on 2nd place
"AND D T\n".chars().for_each(|c| input.push_back(c as i64)); // but a hull on 4th place
"AND H J\n".chars().for_each(|c| input.push_back(c as i64)); // and on 8th place,
"OR T J\n".chars().for_each(|c| input.push_back(c as i64)); // we can jump
"NOT A T\n".chars().for_each(|c| input.push_back(c as i64)); // Or if there is a gap immediately in front of us
"OR T J\n".chars().for_each(|c| input.push_back(c as i64)); // => we can jump
"RUN\n".chars().for_each(|c| input.push_back(c as i64));
let mut intcode = intcode_instance(&code);
intcode.run(&mut input);
println!("{}", intcode.output.iter().map(|c| *c as u8 as char).join(""));
println!("{}", *intcode.output.last().unwrap() as i64);
}
struct IntcodeProcessor {
instruction_ptr: i64,
memory: HashMap<i64, i64>,
relative_base: i64,
output: Vec<i64>,
}
impl IntcodeProcessor {
fn run(&mut self, input: &mut VecDeque<i64>) {
loop {
let _instr = self.memory[&self.instruction_ptr];
let opcode = self.memory[&self.instruction_ptr] % 100;
let a_mode = (self.memory[&self.instruction_ptr] / 100 % 10) as i32;
let b_mode = (self.memory[&self.instruction_ptr] / 1000 % 10) as i32;
let c_mode = (self.memory[&self.instruction_ptr] / 10000 % 10) as i32;
match opcode {
1 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let b = self.memory[&(self.instruction_ptr + 2)];
let c = self.memory[&(self.instruction_ptr + 3)];
let a_val = self.mem_read(a_mode, a);
let b_val = self.mem_read(b_mode, b);
self.mem_write(c_mode, c, a_val + b_val);
self.instruction_ptr += 4;
}
2 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let b = self.memory[&(self.instruction_ptr + 2)];
let c = self.memory[&(self.instruction_ptr + 3)];
let a_val = self.mem_read(a_mode, a);
let b_val = self.mem_read(b_mode, b);
self.mem_write(c_mode, c, a_val * b_val);
self.instruction_ptr += 4;
}
3 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let inp_val = input.pop_front().unwrap();
self.mem_write(a_mode, a, inp_val);
self.instruction_ptr += 2;
}
4 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let a_val = self.mem_read(a_mode, a);
self.output.push(a_val);
self.instruction_ptr += 2;
}
5 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let b = self.memory[&(self.instruction_ptr + 2)];
let a_val = self.mem_read(a_mode, a);
if a_val != 0 {
let b_val = self.mem_read(b_mode, b);
self.instruction_ptr = b_val;
} else {
self.instruction_ptr += 3;
}
}
6 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let b = self.memory[&(self.instruction_ptr + 2)];
let a_val = self.mem_read(a_mode, a);
if a_val == 0 {
let b_val = self.mem_read(b_mode, b);
self.instruction_ptr = b_val;
} else {
self.instruction_ptr += 3;
}
}
7 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let b = self.memory[&(self.instruction_ptr + 2)];
let c = self.memory[&(self.instruction_ptr + 3)];
let a_val = self.mem_read(a_mode, a);
let b_val = self.mem_read(b_mode, b);
self.mem_write(c_mode, c, if a_val < b_val { 1 } else { 0 });
self.instruction_ptr += 4;
}
8 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let b = self.memory[&(self.instruction_ptr + 2)];
let c = self.memory[&(self.instruction_ptr + 3)];
let a_val = self.mem_read(a_mode, a);
let b_val = self.mem_read(b_mode, b);
self.mem_write(c_mode, c, if a_val == b_val { 1 } else { 0 });
self.instruction_ptr += 4;
}
9 => {
let a = self.memory[&(self.instruction_ptr + 1)];
let a_val = self.mem_read(a_mode, a);
self.relative_base += a_val;
self.instruction_ptr += 2;
}
99 => {
break;
}
_ => {
panic!("Unknown opcode {} at pos {}", opcode, self.instruction_ptr);
}
}
}
}
fn mem_read(&mut self, mode: i32, val: i64) -> i64 {
match mode {
0 => {
if val < 0 {
panic!("Invalid memory address: {}", val)
}
*self.memory.entry(val).or_insert(0)
}
1 => val,
2 => {
let addr = val + self.relative_base;
if addr < 0 {
panic!("Invalid memory address: {}", addr)
}
*self.memory.entry(addr).or_insert(0)
}
_ => panic!("Unknown mode: {}", mode),
}
}
fn mem_write(&mut self, mode: i32, addr: i64, val: i64) {
match mode {
0 => {
if addr < 0 {
panic!("Invalid memory address: {}", addr);
}
self.memory.insert(addr, val);
}
1 => panic!("How to write in mode 1?"),
2 => {
let i = addr + self.relative_base;
if i < 0 {
panic!("Invalid memory address: {}", i)
}
self.memory.insert(i, val);
}
_ => panic!("Unknown mode: {}", mode),
};
}
}
fn intcode_instance(code: &Vec<i64>) -> IntcodeProcessor {
let mut memory = HashMap::new();
for i in 0..code.len() {
memory.insert(i as i64, code[i]);
}
IntcodeProcessor {
instruction_ptr: 0,
memory,
relative_base: 0,
output: vec![],
}
}