def build_forest(path): points = [ tuple(common.extract_numbers(line)) for line in common.read_file(path).splitlines() ] direct_connections = {} def manhattan_distance(p1, p2): return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) + abs( p1[2] - p2[2]) + abs(p1[3] - p2[3]) for p in points: if p not in direct_connections: direct_connections[p] = [] for p2 in points: if p == p2: continue if p2 not in direct_connections: direct_connections[p2] = [] if manhattan_distance(p, p2) <= 3: direct_connections[p].append(p2) direct_connections[p2].append(p) unused_points = list(points) constellations = [] while len(unused_points) > 0: start = unused_points[0] seen_points = set() q = deque([start]) while len(q) > 0: p = q.popleft() if p in seen_points: continue seen_points.add(p) unused_points.remove(p) for rel in direct_connections[p]: q.append(rel) constellations.append(list(seen_points)) return constellations
import common year_common = common.import_year_common(2019) tape = common.extract_numbers(common.read_file('2019/25/data.txt')) moves = [ 'south', 'take monolith', 'east', 'take asterisk', 'west', 'north', # 'west', # 'take coin', # 'north', # 'east', # 'take astronaut ice cream', # 'west', # 'south', # 'east', 'north', 'north', # 'take mutex', 'west', 'take astrolabe', 'west', # 'take dehydrated water', 'west', 'take wreath', 'east', 'south',
import common import numpy as np lines = common.read_file('2018/03/data.txt').splitlines() parsed = [common.extract_numbers(x) for x in lines] fabric = np.zeros((1000, 1000)) for idd, x, y, w, h in parsed: fabric[x:x + w, y:y + h] += 1 print(np.sum(fabric > 1)) for idd, x, y, w, h in parsed: if np.all(fabric[x:x + w, y:y + h] == 1): print(idd)
import common import collections as coll data = common.read_file('2018/06/data.txt').splitlines() parsed = [tuple(common.extract_numbers(x)) for x in data] minx = min(map(lambda x: x[0], parsed)) miny = min(map(lambda x: x[1], parsed)) maxx = max(map(lambda x: x[0], parsed)) maxy = max(map(lambda x: x[1], parsed)) dx = maxx - minx + 1 dy = maxy - miny + 1 print(minx, maxx, miny, maxy) def diffs(x, y): for row in parsed: rx, ry = row diff = abs(rx - x) + abs(ry - y) yield diff def min_diff(x, y): min_d = 50000 min_i = None dfs = list(diffs(x, y)) for i in range(len(dfs)): diff = dfs[i] if diff < min_d: min_d = diff min_i = i
import re from queue import PriorityQueue import common lines = common.read_file('2018/23/data.txt').splitlines() rows = [] for line in lines: nums = common.extract_numbers(line) rows.append(tuple(nums)) X = 0 Y = 1 Z = 2 R = 3 def manhattan_dist(pos1, pos2): return abs(pos1[X]-pos2[X])+abs(pos1[Y]-pos2[Y])+abs(pos1[Z]-pos2[Z]) def sees(bot, point): return manhattan_dist(bot, point) <= bot[R] # part 1 max_range_bot = max(rows, key=lambda x: x[R]) max_range_bot_pos = max_range_bot[:R] max_range_bot_range = max_range_bot[R] in_range_count = 0 for row in rows: dist = manhattan_dist(max_range_bot_pos, row) if dist <= max_range_bot_range: in_range_count += 1