From b1013a2e688ed347a542b7da6ee143e5307485d4 Mon Sep 17 00:00:00 2001 From: rpidanny Date: Wed, 3 Jan 2024 07:38:26 +0100 Subject: [PATCH] =?UTF-8?q?feat(2019):=20=F0=9F=93=B9=F0=9F=A4=96=20day=20?= =?UTF-8?q?17=20-=20Set=20and=20Forget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2019/Day17/README.md | 134 +++++++++++++++++++++ 2019/Day17/direction.py | 47 ++++++++ 2019/Day17/input.txt | 1 + 2019/Day17/main.py | 18 +++ 2019/Day17/robot.py | 218 +++++++++++++++++++++++++++++++++++ 2019/Day17/solutions.py | 15 +++ 2019/Day17/test_solutions.py | 19 +++ 2019/utils/output.py | 6 +- 8 files changed, 456 insertions(+), 2 deletions(-) create mode 100644 2019/Day17/README.md create mode 100644 2019/Day17/direction.py create mode 100644 2019/Day17/input.txt create mode 100644 2019/Day17/main.py create mode 100644 2019/Day17/robot.py create mode 100644 2019/Day17/solutions.py create mode 100644 2019/Day17/test_solutions.py diff --git a/2019/Day17/README.md b/2019/Day17/README.md new file mode 100644 index 0000000..d3047f6 --- /dev/null +++ b/2019/Day17/README.md @@ -0,0 +1,134 @@ +# [--- Day 17: Set and Forget ---](https://adventofcode.com/2019/day/17) + +An early warning system detects an incoming solar flare and automatically activates the ship's electromagnetic shield. Unfortunately, this has cut off the Wi-Fi for many small robots that, unaware of the impending danger, are now trapped on exterior scaffolding on the unsafe side of the shield. To rescue them, you'll have to act quickly! + +The only tools at your disposal are some wired cameras and a small vacuum robot currently asleep at its charging station. The video quality is poor, but the vacuum robot has a needlessly bright LED that makes it easy to spot no matter where it is. + +An Intcode program, the Aft Scaffolding Control and Information Interface (ASCII, your puzzle input), provides access to the cameras and the vacuum robot. Currently, because the vacuum robot is asleep, you can only access the cameras. + +Running the ASCII program on your Intcode computer will provide the current view of the scaffolds. This is output, purely coincidentally, as ASCII code: `35` means `#`, `46` means `.`, `10` starts a new line of output below the current one, and so on. (Within a line, characters are drawn left-to-right.) + +In the camera output, # represents a scaffold and . represents open space. The vacuum robot is visible as `^`, `v`, `<`, or `>` depending on whether it is facing up, down, left, or right respectively. When drawn like this, the vacuum robot is always on a scaffold; if the vacuum robot ever walks off of a scaffold and begins tumbling through space uncontrollably, it will instead be visible as X. + +In general, the scaffold forms a path, but it sometimes loops back onto itself. For example, suppose you can see the following view from the cameras: + +```txt +..#.......... +..#.......... +#######...### +#.#...#...#.# +############# +..#...#...#.. +..#####...^.. +``` + +Here, the vacuum robot, `^` is facing up and sitting at one end of the scaffold near the bottom-right of the image. The scaffold continues up, loops across itself several times, and ends at the top-left of the image. + +The first step is to calibrate the cameras by getting the alignment parameters of some well-defined points. Locate all scaffold intersections; for each, its alignment parameter is the distance between its left edge and the left edge of the view multiplied by the distance between its top edge and the top edge of the view. Here, the intersections from the above image are marked `O`: + +```txt +..#.......... +..#.......... +##O####...### +#.#...#...#.# +##O###O###O## +..#...#...#.. +..#####...^.. +``` + +For these intersections: + +- The top-left intersection is `2` units from the left of the image and `2` units from the top of the image, so its alignment parameter is `2 * 2 = 4`. +- The bottom-left intersection is `2` units from the left and `4` units from the top, so its alignment parameter is `2 * 4 = 8`. +- The bottom-middle intersection is `6` from the left and `4` from the top, so its alignment parameter is `24`. +- The bottom-right intersection's alignment parameter is `40`. + +To calibrate the cameras, you need the sum of the alignment parameters. In the above example, this is `76`. + +Run your ASCII program. **What is the sum of the alignment parameters for the scaffold intersections?** + +> Your puzzle answer was `3448`. + +## --- Part Two --- + +Now for the tricky part: notifying all the other robots about the solar flare. The vacuum robot can do this automatically if it gets into range of a robot. However, you can't see the other robots on the camera, so you need to be thorough instead: you need to make the vacuum robot visit every part of the scaffold at least once. + +The vacuum robot normally wanders randomly, but there isn't time for that today. Instead, you can override its movement logic with new rules. + +Force the vacuum robot to wake up by changing the value in your ASCII program at address `0` from `1` to `2`. When you do this, you will be automatically prompted for the new movement rules that the vacuum robot should use. The ASCII program will use input instructions to receive them, but they need to be provided as ASCII code; end each line of logic with a single newline, ASCII code `10`. + +First, you will be prompted for the main movement routine. The main routine may only call the movement functions: A, B, or C. Supply the movement functions to use as ASCII text, separating them with commas (`,`, ASCII code `44`), and ending the list with a newline (ASCII code `10`). For example, to call A twice, then alternate between B and C three times, provide the string `A,A,B,C,B,C,B,C` and then a newline. + +Then, you will be prompted for each movement function. Movement functions may use `L` to turn left, `R` to turn right, or a number to move forward that many units. Movement functions may not call other movement functions. Again, separate the actions with commas and end the list with a newline. For example, to move forward `10` units, turn left, move forward `8` units, turn right, and finally move forward `6` units, provide the string `10,L,8,R,6` and then a newline. + +Finally, you will be asked whether you want to see a continuous video feed; provide either `y` or `n` and a newline. Enabling the continuous video feed can help you see what's going on, but it also requires a significant amount of processing power, and may even cause your Intcode computer to overheat. + +Due to the limited amount of memory in the vacuum robot, the ASCII definitions of the main routine and the movement functions may each contain at most `20` characters, not counting the newline. + +For example, consider the following camera feed: + +```txt +#######...##### +#.....#...#...# +#.....#...#...# +......#...#...# +......#...###.# +......#.....#.# +^########...#.# +......#.#...#.# +......######### +........#...#.. +....#########.. +....#...#...... +....#...#...... +....#...#...... +....#####...... +``` + +In order for the vacuum robot to visit every part of the scaffold at least once, one path it could take is: + +`R,8,R,8,R,4,R,4,R,8,L,6,L,2,R,4,R,4,R,8,R,8,R,8,L,6,L,2` + +Without the memory limit, you could just supply this whole string to function A and have the main routine call A once. However, you'll need to split it into smaller parts. + +One approach is: + +- **Main** routine: `A,B,C,B,A,C` + (ASCII input: `65, 44, 66, 44, 67, 44, 66, 44, 65, 44, 67, 10`) +- Function **A**: `R,8,R,8` + (ASCII input: `82, 44, 56, 44, 82, 44, 56, 10`) +- Function **B**: `R,4,R,4,R,8` + (ASCII input: `82, 44, 52, 44, 82, 44, 52, 44, 82, 44, 56, 10`) +- Function **C**: `L,6,L,2` + (ASCII input: `76, 44, 54, 44, 76, 44, 50, 10`) + +Visually, this would break the desired path into the following parts: + +```txt +A, B, C, B, A, C +R,8,R,8, R,4,R,4,R,8, L,6,L,2, R,4,R,4,R,8, R,8,R,8, L,6,L,2 + +CCCCCCA...BBBBB +C.....A...B...B +C.....A...B...B +......A...B...B +......A...CCC.B +......A.....C.B +^AAAAAAAA...C.B +......A.A...C.B +......AAAAAA#AB +........A...C.. +....BBBB#BBBB.. +....B...A...... +....B...A...... +....B...A...... +....BBBBA...... +``` + +Of course, the scaffolding outside your ship is much more complex. + +As the vacuum robot finds other robots and notifies them of the impending solar flare, it also can't help but leave them squeaky clean, collecting any space dust it finds. Once it finishes the programmed set of movements, assuming it hasn't drifted off into space, the cleaning robot will return to its docking station and report the amount of space dust it collected as a large, non-ASCII value in a single output instruction. + +After visiting every part of the scaffold at least once, **how much dust does the vacuum robot report it has collected?** + +> Your puzzle answer was `762405`. diff --git a/2019/Day17/direction.py b/2019/Day17/direction.py new file mode 100644 index 0000000..7edee63 --- /dev/null +++ b/2019/Day17/direction.py @@ -0,0 +1,47 @@ +from enum import Enum + + +class Direction(Enum): + UP = "^" + DOWN = "v" + LEFT = "<" + RIGHT = ">" + + def __repr__(self) -> str: + return self.name + + def __str__(self) -> str: + return self.__repr__() + + def move(self, pos: tuple) -> tuple: + x, y = pos + return { + Direction.UP: (x, y - 1), + Direction.DOWN: (x, y + 1), + Direction.LEFT: (x - 1, y), + Direction.RIGHT: (x + 1, y), + }[self] + + def right(self): + return { + Direction.UP: Direction.RIGHT, + Direction.RIGHT: Direction.DOWN, + Direction.DOWN: Direction.LEFT, + Direction.LEFT: Direction.UP, + }[self] + + def left(self): + return { + Direction.UP: Direction.LEFT, + Direction.LEFT: Direction.DOWN, + Direction.DOWN: Direction.RIGHT, + Direction.RIGHT: Direction.UP, + }[self] + + def reverse(self): + return { + Direction.UP: Direction.DOWN, + Direction.DOWN: Direction.UP, + Direction.LEFT: Direction.RIGHT, + Direction.RIGHT: Direction.LEFT, + }[self] diff --git a/2019/Day17/input.txt b/2019/Day17/input.txt new file mode 100644 index 0000000..ca7fbb9 --- /dev/null +++ b/2019/Day17/input.txt @@ -0,0 +1 @@ +1,330,331,332,109,3016,1101,1182,0,16,1101,1441,0,24,102,1,0,570,1006,570,36,1002,571,1,0,1001,570,-1,570,1001,24,1,24,1106,0,18,1008,571,0,571,1001,16,1,16,1008,16,1441,570,1006,570,14,21101,58,0,0,1105,1,786,1006,332,62,99,21101,333,0,1,21101,73,0,0,1105,1,579,1101,0,0,572,1101,0,0,573,3,574,101,1,573,573,1007,574,65,570,1005,570,151,107,67,574,570,1005,570,151,1001,574,-64,574,1002,574,-1,574,1001,572,1,572,1007,572,11,570,1006,570,165,101,1182,572,127,102,1,574,0,3,574,101,1,573,573,1008,574,10,570,1005,570,189,1008,574,44,570,1006,570,158,1106,0,81,21101,340,0,1,1106,0,177,21102,1,477,1,1106,0,177,21101,514,0,1,21102,176,1,0,1105,1,579,99,21102,1,184,0,1105,1,579,4,574,104,10,99,1007,573,22,570,1006,570,165,1002,572,1,1182,21101,0,375,1,21101,211,0,0,1105,1,579,21101,1182,11,1,21101,222,0,0,1105,1,979,21102,1,388,1,21102,1,233,0,1105,1,579,21101,1182,22,1,21102,244,1,0,1106,0,979,21102,401,1,1,21101,0,255,0,1106,0,579,21101,1182,33,1,21101,266,0,0,1106,0,979,21102,414,1,1,21101,0,277,0,1105,1,579,3,575,1008,575,89,570,1008,575,121,575,1,575,570,575,3,574,1008,574,10,570,1006,570,291,104,10,21102,1,1182,1,21101,0,313,0,1106,0,622,1005,575,327,1102,1,1,575,21101,327,0,0,1106,0,786,4,438,99,0,1,1,6,77,97,105,110,58,10,33,10,69,120,112,101,99,116,101,100,32,102,117,110,99,116,105,111,110,32,110,97,109,101,32,98,117,116,32,103,111,116,58,32,0,12,70,117,110,99,116,105,111,110,32,65,58,10,12,70,117,110,99,116,105,111,110,32,66,58,10,12,70,117,110,99,116,105,111,110,32,67,58,10,23,67,111,110,116,105,110,117,111,117,115,32,118,105,100,101,111,32,102,101,101,100,63,10,0,37,10,69,120,112,101,99,116,101,100,32,82,44,32,76,44,32,111,114,32,100,105,115,116,97,110,99,101,32,98,117,116,32,103,111,116,58,32,36,10,69,120,112,101,99,116,101,100,32,99,111,109,109,97,32,111,114,32,110,101,119,108,105,110,101,32,98,117,116,32,103,111,116,58,32,43,10,68,101,102,105,110,105,116,105,111,110,115,32,109,97,121,32,98,101,32,97,116,32,109,111,115,116,32,50,48,32,99,104,97,114,97,99,116,101,114,115,33,10,94,62,118,60,0,1,0,-1,-1,0,1,0,0,0,0,0,0,1,14,0,0,109,4,1202,-3,1,587,20102,1,0,-1,22101,1,-3,-3,21102,0,1,-2,2208,-2,-1,570,1005,570,617,2201,-3,-2,609,4,0,21201,-2,1,-2,1105,1,597,109,-4,2105,1,0,109,5,1202,-4,1,630,20101,0,0,-2,22101,1,-4,-4,21102,0,1,-3,2208,-3,-2,570,1005,570,781,2201,-4,-3,652,21002,0,1,-1,1208,-1,-4,570,1005,570,709,1208,-1,-5,570,1005,570,734,1207,-1,0,570,1005,570,759,1206,-1,774,1001,578,562,684,1,0,576,576,1001,578,566,692,1,0,577,577,21101,0,702,0,1106,0,786,21201,-1,-1,-1,1105,1,676,1001,578,1,578,1008,578,4,570,1006,570,724,1001,578,-4,578,21101,0,731,0,1105,1,786,1105,1,774,1001,578,-1,578,1008,578,-1,570,1006,570,749,1001,578,4,578,21101,756,0,0,1106,0,786,1105,1,774,21202,-1,-11,1,22101,1182,1,1,21101,774,0,0,1106,0,622,21201,-3,1,-3,1105,1,640,109,-5,2106,0,0,109,7,1005,575,802,20101,0,576,-6,20101,0,577,-5,1106,0,814,21101,0,0,-1,21101,0,0,-5,21102,1,0,-6,20208,-6,576,-2,208,-5,577,570,22002,570,-2,-2,21202,-5,45,-3,22201,-6,-3,-3,22101,1441,-3,-3,2101,0,-3,843,1005,0,863,21202,-2,42,-4,22101,46,-4,-4,1206,-2,924,21102,1,1,-1,1105,1,924,1205,-2,873,21101,35,0,-4,1106,0,924,1201,-3,0,878,1008,0,1,570,1006,570,916,1001,374,1,374,1202,-3,1,895,1101,2,0,0,1201,-3,0,902,1001,438,0,438,2202,-6,-5,570,1,570,374,570,1,570,438,438,1001,578,558,922,20101,0,0,-4,1006,575,959,204,-4,22101,1,-6,-6,1208,-6,45,570,1006,570,814,104,10,22101,1,-5,-5,1208,-5,35,570,1006,570,810,104,10,1206,-1,974,99,1206,-1,974,1101,0,1,575,21101,973,0,0,1105,1,786,99,109,-7,2106,0,0,109,6,21102,0,1,-4,21102,1,0,-3,203,-2,22101,1,-3,-3,21208,-2,82,-1,1205,-1,1030,21208,-2,76,-1,1205,-1,1037,21207,-2,48,-1,1205,-1,1124,22107,57,-2,-1,1205,-1,1124,21201,-2,-48,-2,1105,1,1041,21102,-4,1,-2,1105,1,1041,21101,-5,0,-2,21201,-4,1,-4,21207,-4,11,-1,1206,-1,1138,2201,-5,-4,1059,1202,-2,1,0,203,-2,22101,1,-3,-3,21207,-2,48,-1,1205,-1,1107,22107,57,-2,-1,1205,-1,1107,21201,-2,-48,-2,2201,-5,-4,1090,20102,10,0,-1,22201,-2,-1,-2,2201,-5,-4,1103,2101,0,-2,0,1106,0,1060,21208,-2,10,-1,1205,-1,1162,21208,-2,44,-1,1206,-1,1131,1106,0,989,21102,439,1,1,1105,1,1150,21102,477,1,1,1106,0,1150,21101,514,0,1,21102,1149,1,0,1105,1,579,99,21101,1157,0,0,1105,1,579,204,-2,104,10,99,21207,-3,22,-1,1206,-1,1138,2101,0,-5,1176,2101,0,-4,0,109,-6,2106,0,0,10,5,40,1,44,1,44,1,44,7,44,1,44,1,7,13,24,1,7,1,11,1,8,7,9,1,7,1,11,1,8,1,5,1,9,1,7,1,11,1,8,1,5,1,9,1,1,5,1,1,5,9,6,1,5,1,9,1,1,1,3,1,1,1,5,1,5,1,1,1,6,1,5,1,7,11,1,11,1,1,6,1,5,1,7,1,1,1,1,1,3,1,3,1,3,1,7,1,6,1,5,1,7,1,1,7,3,1,3,1,7,1,6,1,5,1,7,1,3,1,7,1,3,1,7,1,6,1,5,1,1,11,1,11,7,1,6,1,5,1,1,1,5,1,5,1,5,1,11,1,6,1,5,9,5,1,5,1,11,1,6,1,7,1,11,1,5,1,11,1,6,7,1,1,11,1,5,7,5,7,6,1,1,1,11,1,11,1,11,1,6,1,1,13,11,1,11,1,6,1,25,1,11,1,6,1,25,1,11,1,6,1,25,1,11,1,6,1,25,1,11,1,6,1,25,1,11,1,6,1,25,1,1,11,6,1,25,1,1,1,16,7,19,7,40,1,3,1,40,1,3,1,40,1,3,1,40,5,6 \ No newline at end of file diff --git a/2019/Day17/main.py b/2019/Day17/main.py new file mode 100644 index 0000000..0209bbf --- /dev/null +++ b/2019/Day17/main.py @@ -0,0 +1,18 @@ +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) + + visualize = os.environ.get("VISUALIZE", False) + + profile_run("Part 1", lambda: part1(inputs, visualize)) + profile_run("Part 2", lambda: part2(inputs, visualize)) diff --git a/2019/Day17/robot.py b/2019/Day17/robot.py new file mode 100644 index 0000000..41ec87c --- /dev/null +++ b/2019/Day17/robot.py @@ -0,0 +1,218 @@ +from itertools import combinations + +from direction import Direction +from intcode import IntCode, get_program +from output import print_grid +from termcolor import colored + + +class Robot: + def __init__(self, input: str, visualize: bool = False): + self.__prog = input + self.__inputs = [] + self.__visualize = visualize + + def __get_input(self) -> int: + return self.__inputs.pop(0) + + def __render_char(self, char: str) -> str: + if char == "#": + return "#" + if char == ".": + return " " + return colored(char, "red", attrs=["bold"]) + + def __is_intersection(self, pos, frame: list[tuple[int, int]]) -> bool: + x, y = pos + + if not (0 < y < len(frame) - 1 and 0 < x < len(frame[y]) - 1): + return False + + return all( + frame[y + dy][x + dx] == "#" + for dx, dy in [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1)] + ) + + def __print_frame(self, frame: list[tuple[int, int]]): + if self.__visualize: + print_grid(frame, self.__render_char, delay=0.0001) + + def __get_frame(self): + mem = get_program(self.__prog) + intcode = IntCode(mem, self.__get_input) + frame = [] + line = [] + while True: + res = intcode.run() + + if res.output == 10 and len(line) > 0: + frame.append(line) + line = [] + else: + line.append(chr(res.output)) + + if res.halted: + break + return frame + + def __get_start_pos( + self, frame: list[tuple[int, int]] + ) -> tuple[tuple[int, int], Direction]: + for y, row in enumerate(frame): + for x, cell in enumerate(row): + if cell in ["^", "v", "<", ">"]: + return ((x, y), Direction(cell)) + return None + + def __group_paths(self, path: list[tuple[int, int]]) -> dict[tuple, str]: + # Find all substrings of path + substrings = [ + tuple(path[i:j]) for i, j in combinations(range(len(path) + 1), 2) + ] + + # Remove substrings that are too short, too long or duplicates + patterns = {} + for item in substrings: + if 3 <= len(item) <= 10 and item not in patterns: + patterns[item] = 1 + + # Find the combination of 3 patterns that covers the whole path + for pattern_combination in combinations(patterns, 3): + string = "".join(path) + for pattern in pattern_combination: + string = string.replace("".join(pattern), "") + + if not string: + return { + pattern: chr(ord("A") + idx) + for idx, pattern in enumerate(pattern_combination) + } + + def __compress_path( + self, path: list[tuple[int, int]], groups: tuple[tuple] + ) -> list[tuple[int, int]]: + compressed_path = [ + groups[tuple(path[i:j])] + for i, j in combinations(range(len(path) + 1), 2) + if tuple(path[i:j]) in groups + ] + return compressed_path + + # Returns a list of actions to take to get to the end + # L4,L4,L6,R10,L6,L4,L4,L6,R10,L6,L12,L6,R10,L6,R8,R10,L6,R8,R10,L6,L4,L4,L6,R10,L6,R8,R10,L6,L12,L6,R10,L6,R8,R10,L6,L12,L6,R10,L6 + # Direction and number of steps to take are joined together to reduce the number of combinations to find the shortest combination later on + def __get_path(self, frame: list[tuple[int, int]]) -> list[tuple[int, int]]: + path = [] + pos, dir = self.__get_start_pos(frame) + + stack = [(pos, dir)] + + cur_step_count = 0 + while len(stack): + p, d = stack.pop() + + # First check if we can go straight + # If we can't, check if we can turn left or right + # If we can't, we have reached the end + # This should move the robot along the entire path + for idx, nd in enumerate([d, d.left(), d.right()]): + nx, ny = nd.move(p) + if ( + 0 <= ny < len(frame) + and 0 <= nx < len(frame[ny]) + and frame[ny][nx] == "#" + ): + if nd != d: + if path: + path[-1] += str(cur_step_count) + cur_step_count = 0 + path.append("L" if idx == 1 else "R") + stack.append((p, nd)) + else: + cur_step_count += 1 + stack.append((nd.move(p), nd)) + break + + if cur_step_count: + path[-1] += str(cur_step_count) + + return path + + def __update_robot_input(self, frame: list[tuple[int, int]]) -> int: + path = self.__get_path(frame) + groups = self.__group_paths(path) + compressed_path = self.__compress_path(path, groups) + + movements = "\n".join( + [ + ",".join(f"{item[:1]},{item[1:]}" for item in movement) + for movement in groups.keys() + ] + ) + + self.__inputs = list( + map( + ord, + list( + f"{','.join(compressed_path)}\n" + + f"{movements}\n" + + f"{ 'y' if self.__visualize else 'n'}\n" + ), + ) + ) + + def get_alignment_param(self) -> int: + frame = self.__get_frame() + + return sum( + x * y + for y, row in enumerate(frame) + for x, cell in enumerate(row) + if cell == "#" and self.__is_intersection((x, y), frame) + ) + + def visit_scaffold(self): + prog = get_program(self.__prog) + prog[0] = 2 + intcode = IntCode(prog, self.__get_input) + frame = [] + line = [] + first_frame = True + + while True: + res = intcode.run() + + if res.halted: + break + + if res.output == 10: + # Skip empty lines and the input prompts + if len(line) > 0 and "".join(line) not in [ + "Continuous video feed?", + "Function C:", + "Function B:", + "Function A:", + "Main:", + ]: + frame.append(line) + + line = [] + elif ( + res.output > 255 + ): # The output greater than 255 (8bit ASCII) is the final answer + return res.output + else: + line.append(chr(res.output)) + + # The frame is 35 lines long + if len(frame) == 35: + # The first frame is the map of the scaffolding + # We need to find the path to the end and compress it into a list of actions + # and then update the robot input with the compressed path + if first_frame: + first_frame = False + self.__update_robot_input(frame) + + self.__print_frame(frame) + frame = [] + line = [] diff --git a/2019/Day17/solutions.py b/2019/Day17/solutions.py new file mode 100644 index 0000000..4da1972 --- /dev/null +++ b/2019/Day17/solutions.py @@ -0,0 +1,15 @@ +import sys + +sys.path.append("utils") + +from robot import Robot + + +def part1(inputs: list[str], visualize=False) -> int: + r = Robot(inputs, visualize) + return r.get_alignment_param() + + +def part2(inputs: list[str], visualize=False) -> int: + r = Robot(inputs, visualize) + return r.visit_scaffold() diff --git a/2019/Day17/test_solutions.py b/2019/Day17/test_solutions.py new file mode 100644 index 0000000..403a190 --- /dev/null +++ b/2019/Day17/test_solutions.py @@ -0,0 +1,19 @@ +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) == 3448 + + +class TestPart2: + def test_with_real_data(self): + assert part2(input) == 762405 diff --git a/2019/utils/output.py b/2019/utils/output.py index 59e49fb..9e1aa07 100644 --- a/2019/utils/output.py +++ b/2019/utils/output.py @@ -5,14 +5,16 @@ from utils.grid import get_min_max_xy -def print_grid(grid, mapper=lambda v: str(v), delay=0): +def print_grid(grid, mapper=lambda v: str(v), delay=0, padding=False): + # os.system("cls" if os.name == "nt" else "clear") 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.write((" " if padding else "").join(map(mapper, row)) + "\n") sys.stdout.flush() + # print("".join(map(mapper, row))) if delay: time.sleep(delay)