-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# [--- Day 11: Space Police ---](https://adventofcode.com/2019/day/11) | ||
|
||
On the way to Jupiter, you're pulled over 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!" | ||
|
||
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 (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: | ||
|
||
```txt | ||
..... | ||
..... | ||
..^.. | ||
..... | ||
..... | ||
``` | ||
|
||
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: | ||
|
||
```txt | ||
..... | ||
..... | ||
.<#.. | ||
..... | ||
..... | ||
``` | ||
|
||
Input instructions should still be provided `0`. Next, the robot might output `0` (paint black) and then `0` _(turn left)_: | ||
|
||
```txt | ||
..... | ||
..... | ||
..#.. | ||
.v... | ||
..... | ||
``` | ||
|
||
After more outputs `(1,0, 1,0)`: | ||
|
||
```txt | ||
..... | ||
..... | ||
..^.. | ||
.##.. | ||
..... | ||
``` | ||
|
||
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: | ||
|
||
```txt | ||
..... | ||
..<#. | ||
...#. | ||
.##.. | ||
..... | ||
``` | ||
|
||
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; 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 `2064`. | ||
## --- 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 `LPZKLGHR`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3,8,1005,8,290,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,28,1006,0,59,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,53,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,101,0,8,76,1006,0,81,1,1005,2,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,105,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,126,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,148,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,171,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,193,1,1008,8,10,1,106,3,10,1006,0,18,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,225,1,1009,9,10,1006,0,92,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,254,2,1001,8,10,1,106,11,10,2,102,13,10,1006,0,78,101,1,9,9,1007,9,987,10,1005,10,15,99,109,612,104,0,104,1,21102,1,825594852136,1,21101,0,307,0,1106,0,411,21101,0,825326580628,1,21101,0,318,0,1105,1,411,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,21102,179557207043,1,1,21101,0,365,0,1106,0,411,21101,0,46213012483,1,21102,376,1,0,1106,0,411,3,10,104,0,104,0,3,10,104,0,104,0,21101,988648727316,0,1,21102,399,1,0,1105,1,411,21102,988224959252,1,1,21101,0,410,0,1106,0,411,99,109,2,21201,-1,0,1,21101,0,40,2,21102,1,442,3,21101,432,0,0,1105,1,475,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,437,438,453,4,0,1001,437,1,437,108,4,437,10,1006,10,469,1102,0,1,437,109,-2,2105,1,0,0,109,4,2102,1,-1,474,1207,-3,0,10,1006,10,492,21101,0,0,-3,21202,-3,1,1,22102,1,-2,2,21101,0,1,3,21102,511,1,0,1105,1,516,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,539,2207,-4,-2,10,1006,10,539,21201,-4,0,-4,1106,0,607,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21101,558,0,0,1106,0,516,22101,0,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,577,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,599,21201,-1,0,1,21101,0,599,0,105,1,474,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
import sys | ||
|
||
sys.path.append(".") | ||
|
||
from solutions import part1, part2 | ||
|
||
from utils.inputs import get_inputs | ||
from utils.timings import profile_run | ||
|
||
if __name__ == "__main__": | ||
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" | ||
inputs = get_inputs(input_path) | ||
|
||
profile_run("Part 1", lambda: part1(inputs)) | ||
profile_run("Part 2", lambda: part2(inputs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from collections import defaultdict | ||
from time import sleep | ||
|
||
from intcode import IntCode | ||
|
||
|
||
class Robot: | ||
def __init__(self, prog: dict[int, int], base_color=0) -> None: | ||
self.__base_color = base_color | ||
self.__pos = (0, 0) | ||
self.__dir = (0, 1) | ||
self.__canvas = defaultdict(int) | ||
self.__inputs = [] | ||
self.__intcode = IntCode(prog, 0, self.__inputs) | ||
|
||
def __move(self, direction: int) -> None: | ||
dx, dy = self.__dir | ||
self.__dir = (-dy, dx) if direction == 0 else (dy, -dx) | ||
self.__pos = (self.__pos[0] + self.__dir[0], self.__pos[1] + self.__dir[1]) | ||
|
||
def __paint(self, color: int) -> None: | ||
self.__canvas[self.__pos] = color | ||
|
||
def __get_color(self, pos: [int, int], default=True) -> int: | ||
return self.__canvas.get(pos, self.__base_color if default else None) | ||
|
||
def num_painted_positions(self) -> int: | ||
return len(self.__canvas) | ||
|
||
def run(self) -> None: | ||
while True: | ||
self.__inputs.append(self.__get_color(self.__pos)) | ||
state = self.__intcode.run() | ||
|
||
if state.halted: | ||
break | ||
|
||
self.__paint(state.output) | ||
|
||
state = self.__intcode.run() | ||
|
||
if state.halted: | ||
break | ||
|
||
self.__move(state.output) | ||
|
||
def print_canvas(self) -> None: | ||
min_x = min(self.__canvas.keys(), key=lambda p: p[0])[0] | ||
max_x = max(self.__canvas.keys(), key=lambda p: p[0])[0] | ||
min_y = min(self.__canvas.keys(), key=lambda p: p[1])[1] | ||
max_y = max(self.__canvas.keys(), key=lambda p: p[1])[1] | ||
|
||
print() | ||
for y in range(max_y, min_y - 1, -1): | ||
for x in range(min_x, max_x + 1): | ||
print("β" if self.__get_color((x, y), False) == 1 else " ", end="") | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import sys | ||
|
||
sys.path.append("utils") | ||
|
||
from intcode import get_program | ||
from robot import Robot | ||
|
||
|
||
def part1(inputs: list[str]) -> int: | ||
robot = Robot(get_program(inputs)) | ||
robot.run() | ||
|
||
return robot.num_painted_positions() | ||
|
||
|
||
def part2(inputs: list[str]) -> int: | ||
robot = Robot(get_program(inputs), 1) | ||
robot.run() | ||
|
||
return robot.print_canvas() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
|
||
from solutions import part1, part2 | ||
from utils.inputs import get_inputs | ||
|
||
current_dir = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
input = get_inputs(f"{current_dir}/input.txt") | ||
|
||
|
||
class TestPart1: | ||
def test_with_real_data(self): | ||
assert part1(input) == 2064 | ||
|
||
|
||
class TestPart2: | ||
def test_with_real_data(self, capsys): | ||
assert part2(input) == None | ||
captured = capsys.readouterr() | ||
assert ( | ||
captured.out | ||
== """ | ||
β βββ ββββ β β β ββ β β βββ | ||
β β β β β β β β β β β β β | ||
β β β β ββ β β ββββ β β | ||
β βββ β β β β β ββ β β βββ | ||
β β β β β β β β β β β β | ||
ββββ β ββββ β β ββββ βββ β β β β | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import sys | ||
import time | ||
|
||
|
||
def print_grid(grid, mapper=lambda v: str(v), delay=0): | ||
for i, row in enumerate(grid): | ||
sys.stdout.write("\033[K") # Clear the current line | ||
sys.stdout.write( | ||
"\033[{};{}H".format(i + 1, 0) | ||
) # Move the cursor to the beginning of the line | ||
sys.stdout.write("".join(map(mapper, row)) + "\n") | ||
sys.stdout.flush() | ||
|
||
if delay: | ||
time.sleep(delay) |