~cdv/aoc-2019-rs

ref: 90a8ccd052a1af9c11d73ec810b7ab05fbff192a aoc-2019-rs/src/bin/day25/main.rs -rw-r--r-- 1.4 KiB
90a8ccd0Chris Vittal Day25: output more like other parts 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
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
98
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 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(())
}