-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.rs
190 lines (165 loc) · 5.63 KB
/
2.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
#[macro_use]
extern crate itertools;
use std::error::Error;
use std::fmt::{self, Debug, Display};
use std::fs::File;
use std::hint::unreachable_unchecked;
use std::io::{self, prelude::*};
type IntcodeVal = usize;
type IntcodeMemState = Vec<IntcodeVal>;
type IntcodeResult<T> = Result<T, IntcodeError>;
type IntcodeValResult = IntcodeResult<IntcodeVal>;
struct IntcodeError {
kind: ErrorKind,
}
impl Debug for IntcodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.kind, f)
}
}
impl Display for IntcodeError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{}", self.kind.as_str())
}
}
impl Error for IntcodeError {}
impl From<IntcodeError> for io::Error {
fn from(e: IntcodeError) -> Self {
Self::new(io::ErrorKind::Other, e)
}
}
#[derive(Debug)]
enum ErrorKind {
InvalidOpcode,
AccessViolation
}
impl ErrorKind {
fn as_str(&self) -> &'static str {
match *self {
ErrorKind::InvalidOpcode => "invalid opcode",
ErrorKind::AccessViolation => "access violation",
}
}
}
#[derive(PartialEq, Eq, Debug)]
enum Op { Add, Mul, Exit, Unknown }
impl Op {
fn instr_size(self) -> Option<usize> {
match self {
Op::Add | Op::Mul => Some(4),
Op::Exit => Some(1),
Op::Unknown => None,
}
}
}
// for converting from intcode opcode to Op
impl From<IntcodeVal> for Op {
fn from(x: IntcodeVal) -> Self {
match x {
1 => Op::Add,
2 => Op::Mul,
99 => Op::Exit,
_ => Op::Unknown,
}
}
}
struct IntcodeComputer {
mem_state: Vec<IntcodeVal>,
}
impl IntcodeComputer {
fn get_val(&self, addr: IntcodeVal) -> IntcodeValResult {
match self.mem_state.get(addr) {
Some(x) => Ok(*x),
None => Err(IntcodeError { kind: ErrorKind::AccessViolation }),
}
}
fn get_mut(&mut self, addr: IntcodeVal) -> IntcodeResult<&mut IntcodeVal> {
match self.mem_state.get_mut(addr) {
Some(x) => Ok(x),
None => Err(IntcodeError { kind: ErrorKind::AccessViolation }),
}
}
fn _return(&self) -> IntcodeValResult {
self.get_val(0)
}
fn execute(&mut self) -> IntcodeValResult {
let mut instr_ptr: usize = 0;
while instr_ptr < self.mem_state.len() {
let operation = Op::from(self.get_val(instr_ptr)?);
match operation {
Op::Unknown => {
return Err(IntcodeError {
kind: ErrorKind::InvalidOpcode
});
},
Op::Exit => { break; },
_ => {
let exec_op = |x: IntcodeVal, y: IntcodeVal| -> IntcodeVal
{
match operation {
Op::Add => x + y,
Op::Mul => x * y,
// this arm will never be reached because
// we're already in a match arm of the same
// variable, and all other possible values
// of the enum have already been accounted
// for
_ => unsafe { unreachable_unchecked() },
}
};
let input1_ptr = instr_ptr + 1;
let input2_ptr = instr_ptr + 2;
let output_ptr = instr_ptr + 3;
let input1 = self.get_val(input1_ptr)?;
let input2 = self.get_val(input2_ptr)?;
let output = self.get_val(output_ptr)?;
let input1_val = self.get_val(input1)?;
let input2_val = self.get_val(input2)?;
let output_val = self.get_mut(output)?;
let result = exec_op(input1_val, input2_val);
*output_val = result;
}
}
instr_ptr += operation.instr_size().unwrap();
}
self._return()
}
}
impl From<Vec<IntcodeVal>> for IntcodeComputer {
fn from(p: Vec<IntcodeVal>) -> Self {
Self {
mem_state: p,
}
}
}
fn solve_part1(p: &IntcodeMemState) -> IntcodeValResult {
let mut program = p.to_owned();
program[1] = 12;
program[2] = 2;
let mut computer = IntcodeComputer::from(program);
computer.execute()
}
fn solve_part2(p: &IntcodeMemState) -> IntcodeValResult {
for (noun, verb) in iproduct!(0..=99, 0..=99) {
let mut program = p.to_owned();
program[1] = noun;
program[2] = verb;
let mut computer = IntcodeComputer::from(program);
if computer.execute()? == 19690720 { return Ok(100*noun + verb); }
}
// if this statement is reached, no solution was found
panic!("No solution found for Part 2");
}
fn main() -> io::Result<()> {
let mut f = File::open("2.txt")?;
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
buffer = buffer.trim().to_string();
let program: IntcodeMemState = buffer.split(',')
.map(|s| s.parse::<IntcodeVal>()
.unwrap())
.collect();
println!("Part 1 answer: {}", solve_part1(&program)?);
println!("Part 2 answer: {}", solve_part2(&program)?);
Ok(())
}