|
| 1 | +#![feature(label_break_value)] |
| 2 | +use std::io::Result; |
| 3 | +use std::option::Option; |
| 4 | + |
| 5 | +struct Program { |
| 6 | + memory: Vec<i32>, |
| 7 | + pointer: usize, |
| 8 | + input: i32, |
| 9 | + output: Vec<i32>, |
| 10 | +} |
| 11 | + |
| 12 | +impl Program { |
| 13 | + fn new(memory: &Vec<i32>) -> Program { |
| 14 | + Program { |
| 15 | + memory: memory.clone(), |
| 16 | + pointer: 0, |
| 17 | + input: 0, |
| 18 | + output: Vec::new(), |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + fn mode_1_2(&mut self, opcode: i32, position_mode_noun: bool, position_mode_verb: bool, position_mode_pos: bool) { |
| 23 | + let mut noun_index: usize = self.pointer + 1; |
| 24 | + let mut verb_index: usize = self.pointer + 2; |
| 25 | + let mut out_index: usize = self.pointer + 3; |
| 26 | + |
| 27 | + if position_mode_noun { |
| 28 | + noun_index = self.memory[noun_index] as usize; |
| 29 | + } |
| 30 | + if position_mode_verb { |
| 31 | + verb_index = self.memory[verb_index] as usize; |
| 32 | + }; |
| 33 | + if position_mode_pos { |
| 34 | + out_index = self.memory[out_index] as usize; |
| 35 | + }; |
| 36 | + |
| 37 | + let noun = self.memory[noun_index]; |
| 38 | + let verb = self.memory[verb_index]; |
| 39 | + |
| 40 | + self.memory[out_index] = if opcode == 1 { |
| 41 | + noun + verb |
| 42 | + } else { |
| 43 | + noun * verb |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + fn mode_3_4(&mut self, opcode: i32, position_mode_noun: bool) { |
| 48 | + let index = if position_mode_noun { |
| 49 | + self.memory[self.pointer + 1] as usize |
| 50 | + } else { |
| 51 | + self.pointer + 1 |
| 52 | + }; |
| 53 | + |
| 54 | + if opcode == 3 { |
| 55 | + self.memory[index] = self.input.clone(); |
| 56 | + } else { |
| 57 | + self.output.push(self.memory[index].clone()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fn to_int(&mut self, input: &char) -> i32 { |
| 62 | + (&input.to_string()).parse::<i32>().unwrap() |
| 63 | + } |
| 64 | + |
| 65 | + fn position_mode(&mut self, input: &Option<char>) -> bool { |
| 66 | + input.unwrap_or('0') == '0' |
| 67 | + } |
| 68 | + |
| 69 | + fn run(&mut self) -> i32 { |
| 70 | + self.pointer = 0; |
| 71 | + |
| 72 | + loop { |
| 73 | + let opcode = self.memory[self.pointer]; |
| 74 | + |
| 75 | + if opcode == 99 { |
| 76 | + break self.memory[0]; |
| 77 | + } |
| 78 | + |
| 79 | + match opcode { |
| 80 | + 1 | 2 => { |
| 81 | + self.mode_1_2(opcode, true, true, true); |
| 82 | + self.pointer += 4; |
| 83 | + } |
| 84 | + 3 | 4 => { |
| 85 | + self.mode_3_4(opcode, true); |
| 86 | + self.pointer += 2; |
| 87 | + } |
| 88 | + _ => { |
| 89 | + let string = opcode.to_string(); |
| 90 | + let mut instuction = string.chars().rev(); |
| 91 | + let opcode = self.to_int(&instuction.next().unwrap()); |
| 92 | + instuction.next(); |
| 93 | + match opcode { |
| 94 | + 1 | 2 => { |
| 95 | + let mut pointer = instuction.clone(); |
| 96 | + let position_mode_noun = self.position_mode(&pointer.next()); |
| 97 | + let position_mode_verb = self.position_mode(&pointer.next()); |
| 98 | + let position_mode_pos = self.position_mode(&pointer.next()); |
| 99 | + self.mode_1_2(opcode, position_mode_noun, position_mode_verb, position_mode_pos); |
| 100 | + self.pointer += 4 |
| 101 | + } |
| 102 | + 3 | 4 => { |
| 103 | + let position_mode = self.position_mode(&instuction.next()); |
| 104 | + self.mode_3_4(opcode, position_mode); |
| 105 | + self.pointer += 2 |
| 106 | + } |
| 107 | + _ => { |
| 108 | + panic!("opcode: {}", opcode) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +fn parse_input(string: &str) -> Vec<Vec<i32>> { |
| 118 | + string |
| 119 | + .lines() |
| 120 | + .map(|string| string.trim()) |
| 121 | + .filter(|string| !string.is_empty()) |
| 122 | + .map(|string| { |
| 123 | + string |
| 124 | + .split(',') |
| 125 | + .map(|part| part.parse::<i32>().unwrap()) |
| 126 | + .collect::<Vec<i32>>() |
| 127 | + }) |
| 128 | + .collect() |
| 129 | +} |
| 130 | + |
| 131 | +fn main() -> Result<()> { |
| 132 | + let input = parse_input(include_str!("../../input/day_05")); |
| 133 | + |
| 134 | + let mut program = Program::new(&input[0]); |
| 135 | + |
| 136 | + program.input = 1; |
| 137 | + program.run(); |
| 138 | + |
| 139 | + println!("part_01: {}", program.run()); |
| 140 | + println!("part_01: {:?}", program.output); |
| 141 | + |
| 142 | + Ok(()) |
| 143 | +} |
| 144 | + |
| 145 | +#[cfg(test)] |
| 146 | +mod tests { |
| 147 | + use super::*; |
| 148 | + |
| 149 | + const EXAMPLE_DATA_FROM_DAY_02: &'static str = " |
| 150 | + 1,0,0,0,99 |
| 151 | + 2,3,0,3,99 |
| 152 | + 2,4,4,5,99,0 |
| 153 | + 1,1,1,4,99,5,6,0,99 |
| 154 | + "; |
| 155 | + |
| 156 | + const EXAMPLE_DATA_01: &'static str = " |
| 157 | + 1101,100,-1,4,0 |
| 158 | + 1002,4,3,4,33 |
| 159 | + "; |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn it_still_works_with_example_data_from_day_02() { |
| 163 | + println!("\n"); |
| 164 | + let input = parse_input(EXAMPLE_DATA_FROM_DAY_02); |
| 165 | + |
| 166 | + let results = input |
| 167 | + .iter() |
| 168 | + .map(|row| { |
| 169 | + let mut program = Program::new(&row); |
| 170 | + program.run(); |
| 171 | + |
| 172 | + program.memory |
| 173 | + }) |
| 174 | + .collect::<Vec<Vec<i32>>>(); |
| 175 | + |
| 176 | + assert_eq!(results[0], [2, 0, 0, 0, 99]); |
| 177 | + assert_eq!(results[1], [2, 3, 0, 6, 99]); |
| 178 | + assert_eq!(results[2], [2, 4, 4, 5, 99, 9801]); |
| 179 | + assert_eq!(results[3], [30, 1, 1, 4, 2, 5, 6, 0, 99]); |
| 180 | + println!("\n"); |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn examples_for_part_1() { |
| 185 | + let input = parse_input(EXAMPLE_DATA_01); |
| 186 | + |
| 187 | + let results = input |
| 188 | + .iter() |
| 189 | + .map(|row| { |
| 190 | + let mut program = Program::new(&row); |
| 191 | + program.run(); |
| 192 | + program.memory |
| 193 | + }) |
| 194 | + .collect::<Vec<Vec<i32>>>(); |
| 195 | + |
| 196 | + assert_eq!(results[0], [1101, 100, -1, 4, 99]); |
| 197 | + assert_eq!(results[1], [1002, 4, 3, 4, 99]); |
| 198 | + } |
| 199 | +} |
0 commit comments