summaryrefslogtreecommitdiff
path: root/2020/rust/day08/src/main.rs
blob: 884ba544c66dda47622d84af458a726196df1d26 (plain)
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
use anyhow::Result;
use std::convert::TryFrom;
use std::env::args;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

use day08::intcode::{Instruction, Intcode};
use day08::intcode;

fn read_input<T: AsRef<Path>>(filename: T) -> Result<Vec<Instruction>> {
    let f = File::open(filename)?;
    let reader = BufReader::new(f);

    reader.lines()
        .map(|v| Ok(Instruction::try_from(v?)?))
        .collect()
}

fn part1(program: &[Instruction]) -> Result<intcode::Wordsize> {
    let mut visited = vec![];
    let mut intcode = Intcode::new();
    intcode.load(program);

    loop {
        let registers = intcode.regdump();
        if visited.contains(&registers.ip) {
            return Ok(registers.accumulator);
        }
        visited.push(registers.ip);
        intcode.step()?
    }
}

fn part2(program: &[Instruction]) -> Option<intcode::Wordsize> {
    'reset: for i in 0..program.len() {
        let mut modified = program.to_vec();
        match modified.get_mut(i) {
            Some(instruction) => match instruction {
                Instruction::Jmp(arg) => *instruction = Instruction::Nop(*arg),
                Instruction::Nop(arg) => *instruction = Instruction::Jmp(*arg),
                _ => {},
            },
            None => panic!("Invalid loop index"),
        }

        let mut visited = vec![];
        let mut intcode = Intcode::new();
        intcode.load(&modified);

        loop {
            let registers = intcode.regdump();
            if visited.contains(&registers.ip) {
                continue 'reset;
            }
            visited.push(registers.ip);
            match intcode.step() {
                Ok(_) => {},
                Err(_) => {
                    // Weird as it might seem, Error is Success in this aoc case.
                    let registers = intcode.regdump();
                    return Some(registers.accumulator);
                }
            }
        }
    }
    None
}

fn main() {
    let (do_part_1, do_part_2) = aoc::do_parts();

    let filename = match args().nth(1) {
        Some(f) => f,
        None => {
            eprintln!("Missing input filename");
            std::process::exit(1);
        },
    };
    match read_input(filename) {
        Ok(input) => {
            if do_part_1 {
                match part1(&input) {
                    Ok(solution) => println!("Part1, {}", solution),
                    Err(err) => eprintln!("Part1, {}", err),
                }
            }
            if do_part_2 {
                match part2(&input) {
                    Some(solution) => println!("Part2, {}", solution),
                    None => eprintln!("Part2, no solution found"),
                }
            }
        },
        Err(err) => eprintln!("Could not read input: {}", err),
    }
}