advent_of_code/year2019/
day02.rs1use super::int_code::Program;
2use super::int_code::Word;
3use crate::input::Input;
4
5pub fn solve(input: &Input) -> Result<Word, String> {
6 const DESIRED_OUTPUT: Word = 19_690_720;
7
8 let initial_program = Program::parse(input.text)?;
9
10 for noun in input.part_values(12..=12, 0..=99) {
11 for verb in input.part_values(2..=2, 0..=99) {
12 let mut program = initial_program.clone();
13 program.write_memory(1, noun);
14 program.write_memory(2, verb);
15 program.run_until_halt_or_input(10_000)?;
16 let memory_value = program.read_memory(0);
17 if input.is_part_one() {
18 return Ok(memory_value);
19 } else if memory_value == DESIRED_OUTPUT {
20 return Ok(100 * noun + verb);
21 }
22 }
23 }
24
25 Err(format!("Desired output {DESIRED_OUTPUT} is never produced"))
26}
27
28#[test]
29pub fn tests() {
30 use crate::input::{test_part_one, test_part_one_error, test_part_two};
31
32 let solution = include_str!("day02_input.txt");
33 test_part_one!(solution => 4_570_637);
34 test_part_two!(solution => 5485);
35
36 test_part_one_error!( "hi" => "Unable to parse program word (invalid digit found in string)");
37}