From 5c7ada174a9db3a5ec98efd2d2b6840b7572e9fd Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 27 Dec 2019 12:41:59 +0100 Subject: [PATCH] Add D21P2 --- 2019/21/Cargo.toml | 1 + 2019/21/src/main.rs | 51 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/2019/21/Cargo.toml b/2019/21/Cargo.toml index d18cbfd..83b2c3e 100644 --- a/2019/21/Cargo.toml +++ b/2019/21/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] intcode = { path = "../intcode" } +itertools = "0.8.2" diff --git a/2019/21/src/main.rs b/2019/21/src/main.rs index 1214db2..7d368db 100644 --- a/2019/21/src/main.rs +++ b/2019/21/src/main.rs @@ -1,21 +1,52 @@ use intcode::*; +use itertools::Itertools; -fn main() { - let program = "NOT A J +// If any of the next 3 are a gap, jump +const PART1_PROGRAM: &str = "NOT A J NOT B T OR T J NOT C T OR T J +// unless 4 is also a gap (otherwise we’d jump right into the hole) AND D J WALK -" - .chars() - .map(|c| c as i64) - .rev() - .collect(); - let part1 = IntComputer::new(read_input(), 0, program) +"; + +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 { + (program.lines().filter(|l| !l.starts_with("//")).join("\n") + "\n") + .chars() + .map(|c| c as i64) + .rev() + .collect() +} + +fn run(input: &Vec, program: &str) -> i64 { + IntComputer::new(input.clone(), 0, compile(program)) .get_all_outputs() .pop() - .unwrap(); - println!("{}", part1); + .unwrap() +} + +fn main() { + let input = read_input(); + println!("Part 1: {}", run(&input, PART1_PROGRAM)); + println!("Part 2: {}", run(&input, PART2_PROGRAM)); }