def setup_graph(): memory = [int(x) for x in read_file("input17.txt").split(',')] output_queue = Queue() C = Computer(memory, Queue(), output_queue) C.run() full_map = reduce(lambda a, b: a + chr(b), output_queue.queue, "") graph = {} for y, line in enumerate(full_map.split()): for x, c in enumerate(line): graph[x, y] = c return graph
from aocutil import read_file from computer import Computer import itertools from queue import Queue from threading import Thread memory = [int(x) for x in read_file("input7.txt").split(',')] max_value = 0 for (a, b, c, d, e) in itertools.permutations([5, 6, 7, 8, 9]): EA_queue = Queue() AB_queue = Queue() BC_queue = Queue() CD_queue = Queue() DE_queue = Queue() EA_queue.put(a) EA_queue.put(0) AB_queue.put(b) BC_queue.put(c) CD_queue.put(d) DE_queue.put(e) A = Computer(memory.copy(), EA_queue, AB_queue) B = Computer(memory.copy(), AB_queue, BC_queue) C = Computer(memory.copy(), BC_queue, CD_queue) D = Computer(memory.copy(), CD_queue, DE_queue) E = Computer(memory.copy(), DE_queue, EA_queue) threads = [ Thread(target=A.run),
from aocutil import read_file import numpy as np from math import gcd from collections import defaultdict from math import atan2 lines = read_file('input10.txt').replace('\n', '').replace('#', '1').replace( '.', '0').replace('X', '1') asteroid_map = np.fromiter(lines, int).reshape(34, 34) def visible_count(tar_index): count = 0 for ind in np.argwhere(asteroid_map): if np.array_equal(ind, tar_index): continue delta = tar_index - ind k = gcd(*delta) minimum_delta = delta // k test_indices = [ind + i * minimum_delta for i in range(1, k)] if not any(asteroid_map[x, y] for (x, y) in test_indices): count += 1 if count == 292: print(tar_index) return count # Part 1 #best = max(visible_count(ind) for ind in np.argwhere(asteroid_map)) # print(best)
import numpy as np from aocutil import read_file import matplotlib.pyplot as plt data = [int(c) for c in read_file("input8.txt").strip()] img = np.array(data).reshape(-1, 6, 25) layer = np.argmin(np.count_nonzero(img == 0, axis=(1, 2))) print(np.count_nonzero(img[layer,:] == 1) * np.count_nonzero(img[layer,:] == 2)) final_image = np.full((6, 25), -1) for sub_image in img: indicies_to_update = np.logical_and(sub_image != 2, final_image == -1) final_image[indicies_to_update] = sub_image[indicies_to_update] plt.imshow(final_image, cmap='gray') plt.show()