~cdv/aoc-2019-rs

ref: 73e77dc3f376f3be920e49b0f9eb3197745dd5f0 aoc-2019-rs/src/bin/day25/main.rs -rw-r--r-- 1.2 KiB
73e77dc3Chris Vittal Day25: Remove superflous commands 1 year, 9 months ago
                                                                                
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
use aoc2019::intcode::*;

// hardcoded and inefficient, whee!
static COMMANDS: &str = r#"west
take dark matter
east
north
take tambourine
south
west
west
north
west
take coin
east
south
east
east
south
south
take hypercube
north
north
west
west
north
west
south
south
"#;

fn main() -> aoc2019::Result<()> {
    let prg = ichomp(true)?;
    let mut ic = Intcode::new(prg);

    let mut pos = 0;
    let mut out = String::new();

    loop {
        match ic.run()? {
            IntcodeStatus::Output(o) => {
                if o >= 0 && o < 128 {
                    out.push(o as u8 as char);
                } else {
                    println!("large output: {}", o)
                }
            }
            IntcodeStatus::NeedsInput => {
                let b = COMMANDS.as_bytes()[pos];
                ic.next_input(b as i64)?;
                pos += 1;
            }
            IntcodeStatus::Halted => break,
            _ => unreachable!(),
        }
    }

    println!(
        "1: {:?}",
        out.split_whitespace()
            .map(|w| w.parse::<i64>())
            .filter(Result::is_ok)
            .nth(0)
            .ok_or("no result")??,
    );
    println!("2: Merr Chrismas!");
    Ok(())
}