use intcode_computer;
use std::fs::read_to_string;

fn main() {
    println!("loading initial state:");
    let code = intcode_computer::parse_program_str(&read_to_string("input.txt").unwrap());
    let basic_program = intcode_computer::Program::init(&code);
    'outer: for noun in 0..100 {
        for verb in 0..100 {
            let mut altered_program = basic_program.clone();
            altered_program.set_memory(1, noun);
            altered_program.set_memory(2, verb);
            //println!("Altered state:\n{:?}", altered_state);
            altered_program.run(Vec::new());
            //println!("Final state:\n{:?}", altered_state);
            let result = altered_program.read_memory(0);
            println!(
                "With noun {} and verb {}: result is {}.",
                noun, verb, result
            );
            if result == 19690720 {
                println!(
                    "Found a correct noun, verb combination! 100 * noun + verb = {}",
                    100 * noun + verb
                );
                break 'outer;
            }
        }
    }
}

#[test]
fn day2_works() {
    let program_state = [
        1, 53, 79, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 6, 19, 1, 9, 19, 23, 1, 6, 23, 27,
        1, 10, 27, 31, 1, 5, 31, 35, 2, 6, 35, 39, 1, 5, 39, 43, 1, 5, 43, 47, 2, 47, 6, 51, 1, 51,
        5, 55, 1, 13, 55, 59, 2, 9, 59, 63, 1, 5, 63, 67, 2, 67, 9, 71, 1, 5, 71, 75, 2, 10, 75,
        79, 1, 6, 79, 83, 1, 13, 83, 87, 1, 10, 87, 91, 1, 91, 5, 95, 2, 95, 10, 99, 2, 9, 99, 103,
        1, 103, 6, 107, 1, 107, 10, 111, 2, 111, 10, 115, 1, 115, 6, 119, 2, 119, 9, 123, 1, 123,
        6, 127, 2, 127, 10, 131, 1, 131, 6, 135, 2, 6, 135, 139, 1, 139, 5, 143, 1, 9, 143, 147, 1,
        13, 147, 151, 1, 2, 151, 155, 1, 10, 155, 0, 99, 2, 14, 0, 0,
    ];
    let mut program = intcode_computer::Program::init(&program_state.to_vec());
    program.run(Vec::new());
    assert_eq!(program.read_memory(0), 19690720);
}
