Add D21P2

This commit is contained in:
kageru 2019-12-27 12:41:59 +01:00
parent ec7bb8f408
commit 5c7ada174a
2 changed files with 42 additions and 10 deletions

View File

@ -8,3 +8,4 @@ edition = "2018"
[dependencies] [dependencies]
intcode = { path = "../intcode" } intcode = { path = "../intcode" }
itertools = "0.8.2"

View File

@ -1,21 +1,52 @@
use intcode::*; use intcode::*;
use itertools::Itertools;
fn main() { // If any of the next 3 are a gap, jump
let program = "NOT A J const PART1_PROGRAM: &str = "NOT A J
NOT B T NOT B T
OR T J OR T J
NOT C T NOT C T
OR T J OR T J
// unless 4 is also a gap (otherwise we’d jump right into the hole)
AND D J AND D J
WALK WALK
" ";
.chars()
.map(|c| c as i64) const PART2_PROGRAM: &str = "NOT T T
.rev() // if any of [A, B, C] are false, T will be false as well
.collect(); AND A T
let part1 = IntComputer::new(read_input(), 0, program) 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))
.get_all_outputs() .get_all_outputs()
.pop() .pop()
.unwrap(); .unwrap()
println!("{}", part1); }
fn main() {
let input = read_input();
println!("Part 1: {}", run(&input, PART1_PROGRAM));
println!("Part 2: {}", run(&input, PART2_PROGRAM));
} }