~cdv/aoc-2019-rs

ref: 7941acef00cd9da2dbcebc81aad7e843e2391a37 aoc-2019-rs/src/bin/day25/main.rs -rw-r--r-- 1.3 KiB
7941acefChris Vittal Solve day 25 1 year, 11 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
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
use aoc2019::intcode::*;

// hardcoded and inefficient, whee!
static COMMANDS: &str = r#"west
take dark matter
east
north
take tambourine
south
west
west
north
east
east
west
west
west
take coin
east
south
east
east
north
north
east
west
south
south
south
south
take hypercube
north
west
east
north
north
east
east
north
south
west
west
north
east
west
south
south
south
south
north
west
east
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 cmd = String::new();

    loop {
        match ic.run()? {
            IntcodeStatus::Output(o) => {
                if o >= 0 && o < 128 {
                    print!("{}", o as u8 as char);
                } else {
                    println!("large output: {}", o)
                }
            }
            IntcodeStatus::NeedsInput => {
                let b = COMMANDS.as_bytes()[pos];
                cmd.push(b as char);
                if b == b'\n' {
                    print!("{}", cmd);
                    cmd.clear();
                }

                ic.next_input(b as i64)?;
                pos += 1;
            }
            IntcodeStatus::Halted => break,
            _ => unreachable!(),
        }
    }

    Ok(())
}