advent-of-code/2019/21/src/main.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2019-12-26 15:28:43 +01:00
use intcode::*;
2019-12-27 12:41:59 +01:00
use itertools::Itertools;
2019-12-26 15:28:43 +01:00
2019-12-27 12:41:59 +01:00
// If any of the next 3 are a gap, jump
const PART1_PROGRAM: &str = "NOT A J
2019-12-26 15:28:43 +01:00
NOT B T
OR T J
NOT C T
OR T J
2019-12-27 12:41:59 +01:00
// unless 4 is also a gap (otherwise we’d jump right into the hole)
2019-12-26 15:28:43 +01:00
AND D J
WALK
2019-12-27 12:41:59 +01:00
";
const PART2_PROGRAM: &str = "NOT T T
// if any of [A, B, C] are false, T will be false as well
AND A T
AND B T
AND C T
// jump if that is the case
NOT T J
// don’t jump if D (where we would land) is false
AND D J
// don’t jump if H is false (because that would prevent us from jumping again after we land)
OR H T
// unless E is true (which gives us the option to walk another step after landing)
OR E T
AND T J
RUN
";
/// Remove comments and convert the source code to ASCII for the springbot.
fn compile(program: &str) -> Vec<i64> {
(program.lines().filter(|l| !l.starts_with("//")).join("\n") + "\n")
.chars()
.map(|c| c as i64)
.rev()
.collect()
}
fn run(input: &Vec<i64>, program: &str) -> i64 {
IntComputer::new(input.clone(), 0, compile(program))
2019-12-26 15:28:43 +01:00
.get_all_outputs()
.pop()
2019-12-27 12:41:59 +01:00
.unwrap()
}
fn main() {
let input = read_input();
println!("Part 1: {}", run(&input, PART1_PROGRAM));
println!("Part 2: {}", run(&input, PART2_PROGRAM));
2019-12-26 15:28:43 +01:00
}