From 7525dd71683f386a03c05ba9362ec011c4b8c24a Mon Sep 17 00:00:00 2001 From: Chris Alge Date: Sun, 12 Mar 2023 15:59:12 +0100 Subject: [PATCH] Added Solution for 2019 day 11 --- 2019/day11_space_police/Cargo.toml | 9 ++ 2019/day11_space_police/challenge.txt | 96 ++++++++++++++++ 2019/day11_space_police/src/lib.rs | 105 ++++++++++++++++++ 2019/day11_space_police/tests/challenge_input | 1 + 2019/day11_space_police/tests/sample_input | 0 5 files changed, 211 insertions(+) create mode 100644 2019/day11_space_police/Cargo.toml create mode 100644 2019/day11_space_police/challenge.txt create mode 100644 2019/day11_space_police/src/lib.rs create mode 100644 2019/day11_space_police/tests/challenge_input create mode 100644 2019/day11_space_police/tests/sample_input diff --git a/2019/day11_space_police/Cargo.toml b/2019/day11_space_police/Cargo.toml new file mode 100644 index 0000000..d275dfc --- /dev/null +++ b/2019/day11_space_police/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day11_space_police" +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" } diff --git a/2019/day11_space_police/challenge.txt b/2019/day11_space_police/challenge.txt new file mode 100644 index 0000000..9a39454 --- /dev/null +++ b/2019/day11_space_police/challenge.txt @@ -0,0 +1,96 @@ +On the way to Jupiter, you're [pulled over](https://www.youtube.com/watch?v=KwY28rpyKDE) by the *Space Police*. + +"Attention, unmarked spacecraft! You are in violation of Space Law! All spacecraft must have a clearly visible *registration identifier*! You have 24 hours to comply or be sent to [Space Jail](https://www.youtube.com/watch?v=BVn1oQL9sWg&t=5)!" + +Not wanting to be sent to Space Jail, you radio back to the Elves on Earth for help. Although it takes almost three hours for their reply signal to reach you, they send instructions for how to power up the *emergency hull painting robot* and even provide a small [Intcode program](9) (your puzzle input) that will cause it to paint your ship appropriately. + +There's just one problem: you don't have an emergency hull painting robot. + +You'll need to build a new emergency hull painting robot. The robot needs to be able to move around on the grid of square panels on the side of your ship, detect the color of its current panel, and paint its current panel *black* or *white*. (All of the panels are currently *black*.) + +The Intcode program will serve as the brain of the robot. The program uses input instructions to access the robot's camera: provide `0` if the robot is over a *black* panel or `1` if the robot is over a *white* panel. Then, the program will output two values: + +* First, it will output a value indicating the *color to paint the panel* the robot is over: `0` means to paint the panel *black*, and `1` means to paint the panel *white*. +* Second, it will output a value indicating the *direction the robot should turn*: `0` means it should turn *left 90 degrees*, and `1` means it should turn *right 90 degrees*. + +After the robot turns, it should always move *forward exactly one panel*. The robot starts facing *up*. + +The robot will continue running for a while like this and halt when it is finished drawing. Do not restart the Intcode computer inside the robot during this process. + +For example, suppose the robot is about to start running. Drawing black panels as `.`, white panels as `#`, and the robot pointing the direction it is facing (`< ^ > v`), the initial state and region near the robot looks like this: + +``` +..... +..... +..^.. +..... +..... + +``` + +The panel under the robot (not visible here because a `^` is shown instead) is also black, and so any input instructions at this point should be provided `0`. Suppose the robot eventually outputs `1` (paint white) and then `0` (turn left). After taking these actions and moving forward one panel, the region now looks like this: + +``` +..... +..... +.<#.. +..... +..... + +``` + +Input instructions should still be provided `0`. Next, the robot might output `0` (paint black) and then `0` (turn left): + +``` +..... +..... +..#.. +.v... +..... + +``` + +After more outputs (`1,0`, `1,0`): + +``` +..... +..... +..^.. +.##.. +..... + +``` + +The robot is now back where it started, but because it is now on a white panel, input instructions should be provided `1`. After several more outputs (`0,1`, `1,0`, `1,0`), the area looks like this: + +``` +..... +..<#. +...#. +.##.. +..... + +``` + +Before you deploy the robot, you should probably have an estimate of the area it will cover: specifically, you need to know the *number of panels it paints at least once*, regardless of color. In the example above, the robot painted *`6` panels* at least once. (It painted its starting panel twice, but that panel is [still only counted once](https://www.youtube.com/watch?v=KjsSvjA5TuE); it also never painted the panel it ended on.) + +Build a new emergency hull painting robot and run the Intcode program on it. *How many panels does it paint at least once?* + +Your puzzle answer was `2539`. + +\--- Part Two --- +---------- + +You're not sure what it's trying to paint, but it's definitely not a *registration identifier*. The Space Police are getting impatient. + +Checking your external ship cameras again, you notice a white panel marked "emergency hull painting robot starting panel". The rest of the panels are *still black*, but it looks like the robot was expecting to *start on a white panel*, not a black one. + +Based on the Space Law Space Brochure that the Space Police attached to one of your windows, a valid registration identifier is always *eight capital letters*. After starting the robot on a single *white panel* instead, *what registration identifier does it paint* on your hull? + +Your puzzle answer was `ZLEBKJRA`. + +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](11/input). \ No newline at end of file diff --git a/2019/day11_space_police/src/lib.rs b/2019/day11_space_police/src/lib.rs new file mode 100644 index 0000000..1043232 --- /dev/null +++ b/2019/day11_space_police/src/lib.rs @@ -0,0 +1,105 @@ +use std::{collections::HashMap, isize}; +use intcode_processor::intcode_processor::{Cpu, OutputState}; + +enum Direction { Up, Left, Down, Right } + +impl Direction { + fn turn_left(&self) -> Self { + match self { + Self::Up => Self::Left, + Self::Left => Self::Down, + Self::Down => Self::Right, + Self::Right => Self::Up, + } + } + + fn turn_right(&self) -> Self { + match self { + Self::Up => Self::Right, + Self::Left => Self::Up, + Self::Down => Self::Left, + Self::Right => Self::Down, + } + } + + fn x(&self) -> isize { + match self { + Self::Left => -1, + Self::Right => 1, + _ => 0, + } + } + + fn y(&self) -> isize { + match self { + Self::Up => -1, + Self::Down => 1, + _ => 0, + } + } +} + +pub fn run(input: &str) -> (usize, String) { + let mut cpu_1 = Cpu::try_with_memory_from_str(input).unwrap(); + let mut cpu_2 = cpu_1.clone(); + let mut panels_1 = HashMap::new(); + let mut panels_2 = HashMap::from([((0, 0), 1)]); + paint(&mut cpu_1, &mut panels_1); + paint(&mut cpu_2, &mut panels_2); + let first = panels_1.len(); + let second = print(&panels_2); + (first, second) +} + +fn paint(cpu: &mut Cpu, panels: &mut HashMap<(isize, isize), isize>) { + let mut position = (0, 0); + let mut direction = Direction::Up; + loop { + cpu.set_input(*panels.get(&position).unwrap_or(&0)); + if let OutputState::Output(colour) = cpu.run() { + panels.insert(position, colour); + } else { + return; + } + match cpu.run() { + OutputState::Output(0) => direction = direction.turn_left(), + OutputState::Output(1) => direction = direction.turn_right(), + _ => return, + } + position.0 += direction.x(); + position.1 += direction.y(); + } +} + +fn print(panels: &HashMap<(isize, isize), isize>) -> String { + let x_min = *panels.iter().map(|((x, _y), _colour)| x).min().unwrap(); + let x_max = *panels.iter().map(|((x, _y), _colour)| x).max().unwrap(); + let y_min = *panels.iter().map(|((_x, y), _colour)| y).min().unwrap(); + let y_max = *panels.iter().map(|((_x, y), _colour)| y).max().unwrap(); + + (y_min..=y_max).map(|y| (x_min..=x_max).map(|x| match panels.get(&(x, y)) { Some(1) => '#', _ => ' ', }).chain(['\n'].into_iter()).collect::()).collect() +} + +#[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"); + let expected = r#" + #### # #### ### # # ## ### ## + # # # # # # # # # # # # + # # ### ### ## # # # # # + # # # # # # # # ### #### + # # # # # # # # # # # # # + #### #### #### ### # # ## # # # # +"#; + assert_eq!(run(&challenge_input), (2539, expected[1..].to_string())); + } +} diff --git a/2019/day11_space_police/tests/challenge_input b/2019/day11_space_police/tests/challenge_input new file mode 100644 index 0000000..df179f0 --- /dev/null +++ b/2019/day11_space_police/tests/challenge_input @@ -0,0 +1 @@ +3,8,1005,8,284,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,28,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,50,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,72,1006,0,24,1,1106,12,10,1006,0,96,1,1008,15,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,108,1006,0,54,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,134,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,155,1006,0,60,1006,0,64,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,183,1006,0,6,1006,0,62,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,211,1,108,0,10,2,1002,15,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,242,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,263,101,1,9,9,1007,9,1010,10,1005,10,15,99,109,606,104,0,104,1,21101,0,666526126996,1,21101,301,0,0,1105,1,405,21101,846138811028,0,1,21101,312,0,0,1106,0,405,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,248129978391,1,21101,359,0,0,1105,1,405,21101,97751403560,0,1,21102,1,370,0,1106,0,405,3,10,104,0,104,0,3,10,104,0,104,0,21101,988753585000,0,1,21101,393,0,0,1105,1,405,21102,867961709324,1,1,21102,404,1,0,1106,0,405,99,109,2,22102,1,-1,1,21102,40,1,2,21101,436,0,3,21102,1,426,0,1105,1,469,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,431,432,447,4,0,1001,431,1,431,108,4,431,10,1006,10,463,1102,0,1,431,109,-2,2106,0,0,0,109,4,1202,-1,1,468,1207,-3,0,10,1006,10,486,21102,1,0,-3,22101,0,-3,1,21202,-2,1,2,21102,1,1,3,21101,505,0,0,1106,0,510,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,533,2207,-4,-2,10,1006,10,533,22101,0,-4,-4,1105,1,601,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,1,552,0,1105,1,510,21202,1,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,571,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,593,21202,-1,1,1,21102,1,593,0,106,0,468,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 diff --git a/2019/day11_space_police/tests/sample_input b/2019/day11_space_police/tests/sample_input new file mode 100644 index 0000000..e69de29