From 2bdc480651cbd840abdd80c0acad5dbfa3d62aff Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 9 Dec 2022 17:00:35 +1030 Subject: [PATCH] Clean up python helpers and day9 --- 2022/day09.py | 38 ++++++++++++-------------------------- 2022/helpers.py | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/2022/day09.py b/2022/day09.py index 568e4ae..87dcc6d 100644 --- a/2022/day09.py +++ b/2022/day09.py @@ -1,39 +1,25 @@ -import numpy as np -with open('input/09', 'r') as file: - lines = file.read().strip().split('\n') -dtype = np.int32 +from helpers import * +lines = read_day(9).split('\n') -directions_array = np.array([[1,0], [-1,0], [0,1], [0,-1]], dtype=dtype) -directions_dict = {'R': directions_array[0], 'L': directions_array[1], 'D': directions_array[2], 'U': directions_array[3]} # Positive down for visualization - -def move_tail(rope: np.array): +def move_tail(rope: ArrayLike): for i in range(1, rope.shape[0]): - dx, dy = rope[i-1,:] - rope[i,:] - if abs(dx) > 1 and dy == 0: - rope[i,0] += 1 if dx > 0 else -1 - elif abs(dy) > 1 and dx == 0: - rope[i,1] += 1 if dy > 0 else -1 - elif abs(dx) + abs(dy) > 2: - rope[i,0] += 1 if dx > 0 else -1 - rope[i,1] += 1 if dy > 0 else -1 + delta = rope[i-1] - rope[i] + abs_delta = abs(delta) + if (abs_delta.max() > 1 and abs_delta.min() == 0) or (sum(abs_delta) > 2): + rope[i] += np.sign(delta) def simulate_rope_drag(length: int, lines: list[str]): rope = np.zeros([length,2], dtype=dtype) - visited = {(rope[-1,0], rope[-1,1])} + visited = {tuple(rope[-1])} for line in lines: dir_vector = directions_dict[line[0]] amount = int(line[2:]) for i in range(amount): - rope[0,:] += dir_vector + rope[0] += dir_vector move_tail(rope) - visited.add((rope[-1,0], rope[-1,1])) + visited.add(tuple(rope[-1])) return visited -def visualise_cells(visited: set, size=20): - vis_lol = [['#' if (x,y) in visited else '.' for x in range(-size, size)] for y in range(-size, size)] - vis_str = '\n'.join(''.join(line) for line in vis_lol) - print(vis_str) - visited_part_1 = simulate_rope_drag(2, lines) print(f'Part 1: String length of 2 - tail visits {len(visited_part_1)} cells') @@ -41,7 +27,7 @@ visited_part_2 = simulate_rope_drag(10, lines) print(f'Part 2: String length of 10 - tail visits {len(visited_part_2)} cells') if input('Visualise part 1 cells? [y/N]: ').lower() == 'y': - visualise_cells(visited_part_1, 70) + visualise_sparse_cells_set(visited_part_1, 70) print() if input('Visualise part 2 cells? [y/N]: ').lower() == 'y': - visualise_cells(visited_part_2, 70) + visualise_sparse_cells_set(visited_part_2, 70) diff --git a/2022/helpers.py b/2022/helpers.py index 195735d..4adec52 100644 --- a/2022/helpers.py +++ b/2022/helpers.py @@ -1,11 +1,27 @@ +import numpy as np +from numpy.typing import ArrayLike import re numbers_pattern = re.compile(r'((?:(?