aoc/year2019/
day21.rs

1//! # Springdroid Adventure
2//!
3//! Jumps are always 4 tiles wide landing on `D`. If needed we can jump again immediately
4//! landing on `H`.
5//!
6//! ## Part One
7//!
8//! We jump if any of `A`, `B` or `C` are holes and there is ground where we will land at `D`.
9//! This take 7 instructions:
10//!
11//! `J = (NOT A OR NOT B OR NOT C) AND D`
12//!
13//! Using [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) we can simplify
14//! to 5 instructions:
15//!
16//! `J = NOT (A AND B AND C) AND D`
17//!
18//! ## Part Two
19//!
20//! We add two rules, either `H` needs to be ground so that we double jump immediately or `E`
21//! needs to be ground, so that we can wait and not jump too early.
22use super::intcode::*;
23use crate::util::parse::*;
24
25const SLOW: &str = "\
26OR A J
27AND B J
28AND C J
29NOT J J
30AND D J";
31
32const FAST: &str = "\
33OR E T
34OR H T
35AND T J";
36
37pub fn parse(input: &str) -> Vec<i64> {
38    input.iter_signed().collect()
39}
40
41pub fn part1(input: &[i64]) -> i64 {
42    survey(input, &format!("{SLOW}\nWALK\n"))
43}
44
45pub fn part2(input: &[i64]) -> i64 {
46    survey(input, &format!("{SLOW}\n{FAST}\nRUN\n"))
47}
48
49fn survey(input: &[i64], springscript: &str) -> i64 {
50    let mut computer = Computer::new(input);
51    computer.input_ascii(springscript);
52
53    let mut result = 0;
54    while let State::Output(next) = computer.run() {
55        result = next;
56    }
57    result
58}