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 WALK: &str = "\
26OR A J
27AND B J
28AND C J
29NOT J J
30AND D J
31WALK
32";
33
34const RUN: &str = "\
35OR A J
36AND B J
37AND C J
38NOT J J
39AND D J
40OR E T
41OR H T
42AND T J
43RUN
44";
45
46pub fn parse(input: &str) -> Vec<i64> {
47 input.iter_signed().collect()
48}
49
50pub fn part1(input: &[i64]) -> i64 {
51 survey(input, WALK)
52}
53
54pub fn part2(input: &[i64]) -> i64 {
55 survey(input, RUN)
56}
57
58fn survey(input: &[i64], springscript: &str) -> i64 {
59 let mut computer = Computer::new(input);
60 computer.input_ascii(springscript);
61
62 let mut result = 0;
63 while let State::Output(next) = computer.run() {
64 result = next;
65 }
66 result
67}