Added Solution for 2019 day 19

This commit is contained in:
Burnus 2023-03-25 22:22:40 +01:00
parent a7f88129cf
commit ada8f731ed
5 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,9 @@
[package]
name = "day19_tractor_beam"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
intcode_processor = { path = "../common/intcode_processor" }

View file

@ -0,0 +1,91 @@
Unsure of the state of Santa's ship, you borrowed the tractor beam technology from Triton. Time to test it out.
When you're safely away from anything else, you activate the tractor beam, but nothing happens. It's hard to tell whether it's working if there's nothing to use it on. Fortunately, your ship's drone system can be configured to deploy a drone to specific coordinates and then check whether it's being pulled. There's even an [Intcode](9) program (your puzzle input) that gives you access to the drone system.
The program uses two input instructions to request the *X and Y position* to which the drone should be deployed. Negative numbers are invalid and will confuse the drone; all numbers should be *zero or positive*.
Then, the program will output whether the drone is *stationary* (`0`) or *being pulled by something* (`1`). For example, the coordinate X=`0`, Y=`0` is directly in front of the tractor beam emitter, so the drone control program will always report `1` at that location.
To better understand the tractor beam, it is important to *get a good picture* of the beam itself. For example, suppose you scan the 10x10 grid of points closest to the emitter:
```
X
0-> 9
0#.........
|.#........
v..##......
...###....
....###...
Y .....####.
......####
......####
.......###
9........##
```
In this example, the *number of points affected by the tractor beam* in the 10x10 area closest to the emitter is `*27*`.
However, you'll need to scan a larger area to *understand the shape* of the beam. *How many points are affected by the tractor beam in the 50x50 area closest to the emitter?* (For each of X and Y, this will be `0` through `49`.)
Your puzzle answer was `226`.
\--- Part Two ---
----------
You aren't sure how large Santa's ship is. You aren't even sure if you'll need to use this thing on Santa's ship, but it doesn't hurt to be prepared. You figure Santa's ship might fit in a *100x100* square.
The beam gets wider as it travels away from the emitter; you'll need to be a minimum distance away to fit a square of that size into the beam fully. (Don't rotate the square; it should be aligned to the same axes as the drone grid.)
For example, suppose you have the following tractor beam readings:
```
#.......................................
.#......................................
..##....................................
...###..................................
....###.................................
.....####...............................
......#####.............................
......######............................
.......#######..........................
........########........................
.........#########......................
..........#########.....................
...........##########...................
...........############.................
............############................
.............#############..............
..............##############............
...............###############..........
................###############.........
................#################.......
.................########OOOOOOOOOO.....
..................#######OOOOOOOOOO#....
...................######OOOOOOOOOO###..
....................#####OOOOOOOOOO#####
.....................####OOOOOOOOOO#####
.....................####OOOOOOOOOO#####
......................###OOOOOOOOOO#####
.......................##OOOOOOOOOO#####
........................#OOOOOOOOOO#####
.........................OOOOOOOOOO#####
..........................##############
..........................##############
...........................#############
............................############
.............................###########
```
In this example, the *10x10* square closest to the emitter that fits entirely within the tractor beam has been marked `O`. Within it, the point closest to the emitter (the only highlighted `*O*`) is at X=`25`, Y=`20`.
Find the *100x100* square closest to the emitter that fits entirely within the tractor beam; within that square, find the point closest to the emitter. *What value do you get if you take that point's X coordinate, multiply it by `10000`, then add the point's Y coordinate?* (In the example above, this would be `250020`.)
Your puzzle answer was `7900946`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, you should [return to your Advent calendar](/2019) and try another puzzle.
If you still want to see it, you can [get your puzzle input](19/input).

View file

@ -0,0 +1,47 @@
use intcode_processor::intcode_processor::{Cpu, OutputState};
use std::{num::ParseIntError, collections::{HashMap, HashSet}};
pub fn run(input: &str) -> Result<(usize, isize), ParseIntError> {
let cpu = Cpu::try_with_memory_from_str(input)?;
let first = (0..50).map(|x| (0..50).filter(|y| is_pulled(&cpu, x, *y)).count()).sum();
let mut x = 0;
let mut y = 0;
while !is_pulled(&cpu, x, y+99) {
while !is_pulled(&cpu, x, y+99) {
x += 1;
}
while !is_pulled(&cpu, x+99, y) {
y += 1;
}
}
let second = 10_000*x + y;
Ok((first, second))
}
fn is_pulled(cpu: &Cpu, x: isize, y: isize) -> bool {
let mut scan = cpu.clone();
scan.set_input(x);
scan.set_input(y);
match scan.run() {
OutputState::DiagnosticCode(1) => true,
OutputState::DiagnosticCode(0) => false,
other => panic!("Unexpected Output State: {other:?}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), Ok((226, 7900946)));
}
}

View file

@ -0,0 +1 @@
109,424,203,1,21101,11,0,0,1105,1,282,21102,18,1,0,1106,0,259,1201,1,0,221,203,1,21102,1,31,0,1105,1,282,21101,38,0,0,1106,0,259,20102,1,23,2,21201,1,0,3,21101,1,0,1,21102,57,1,0,1105,1,303,1201,1,0,222,21001,221,0,3,20101,0,221,2,21102,1,259,1,21101,0,80,0,1105,1,225,21101,76,0,2,21102,1,91,0,1106,0,303,2102,1,1,223,21002,222,1,4,21102,1,259,3,21101,0,225,2,21102,225,1,1,21102,1,118,0,1105,1,225,21001,222,0,3,21102,1,54,2,21102,1,133,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21101,148,0,0,1106,0,259,1202,1,1,223,21001,221,0,4,20101,0,222,3,21101,14,0,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21101,0,195,0,106,0,108,20207,1,223,2,20101,0,23,1,21101,0,-1,3,21102,1,214,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1202,-4,1,249,22102,1,-3,1,21201,-2,0,2,21202,-1,1,3,21101,0,250,0,1106,0,225,22101,0,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2105,1,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21201,-2,0,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22101,0,-2,3,21102,1,343,0,1106,0,303,1106,0,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,22102,1,-4,1,21101,0,384,0,1105,1,303,1106,0,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21202,1,1,-4,109,-5,2106,0,0