def findO2System() -> Scanner: init = icp.Thread(icp.loadProgram(PROGRAM_PATH)) drones = [Scanner(init.clone(), DM_N, 0, IntPoint(0,0)), Scanner(init.clone(), DM_E, 0, IntPoint(0,0)), Scanner(init.clone(), DM_S, 0, IntPoint(0,0)), Scanner(init.clone(), DM_W, 0, IntPoint(0,0))] status = -1 drone = drones.pop(0) while not status == DS_ONO2: drone.program.process() if drone.program.needInput(): # Drone waiting for input. #print("----------s%d %s" % (drone.program.id, drone.position)) #printMap(droneMap, drone.position) direction = drone.direction """Bulletproof Input print(drone.position, DS_P[status], end=" ") newDirection = -1 while newDirection == -1: try: newDirection = S_D[input("<=")] except: newDirection = -1 direction = newDirection #""" drone.program.istream.append(direction) continue if drone.program.hasOutput(): status = drone.program.readOutput() if status == DS_MOVE or status == DS_ONO2: drone.steps += 1 drone.position += MD[direction] addPosition(droneMap, drone.position, status) for d in MC: if d == drone.direction: continue if not hasBeenVisited(droneMap, drone.position + MD[d]): #print("Making scanner facing %s" % DM_P[d]) drones.append(Scanner(drone.program.clone(), d, drone.steps, drone.position)) elif status == DS_WALL: wallPosition = drone.position + MD[direction] addPosition(droneMap, wallPosition, status) if len(drones): drone = drones.pop(0) else: #print("-------") #printMap(droneMap,drone.position) return drone continue return drone
def fold_page(initial: list[IntPoint], fold: IntPoint) -> list[IntPoint]: print(f'Folding along {fold}.') folded: list[IntPoint] = [] for p in initial: if fold.y == 0: if p.x < fold.x: folded.append(p) if p.x > fold.x: folded.append(IntPoint(fold.x + fold.x - p.x, p.y)) if fold.x == 0: if p.y < fold.y: folded.append(p) if p.y > fold.y: folded.append(IntPoint(p.x, fold.y + fold.y - p.y)) return folded
def part2(inputs): vents: dict[str, int] = dict() for input in inputs: points = input.split(' -> ') p1 = IntPoint.from_str(points[0]) p2 = IntPoint.from_str(points[1]) step = (p2 - p1).unit() while p1 != (p2 + step): vents[str(p1)] = vents.get(str(p1), 0) + 1 p1 += step overlaps = 0 for vent in vents.values(): if vent > 1: overlaps += 1 print(overlaps)
def part1() -> Tuple[Set[IntPoint], IntPoint]: ip = icp.Thread(icp.loadProgram(PROGRAM_PATH)) output: List[str] = [] x, y = 0, 0 scaffolds: Set[IntPoint] = set() position: IntPoint = IntPoint(0, 0) while not ip.didSucceed(): ip.process() if ip.hasOutput(): value = chr(ip.readOutput()) currentLocation = IntPoint(x, y) if value == '#': # A scaffold exists at this location. scaffolds.add(currentLocation) elif value == '^': # The robot exists at this location. # It is on a scaffold. scaffolds.add(currentLocation) position = currentLocation if value == '\n': x = 0 y += 1 else: x += 1 output.append(value) alignment_sum = 0 #print(''.join(map(str,output))) for s in scaffolds: checks = { s + DIRECTIONS['n'], s + DIRECTIONS['e'], s + DIRECTIONS['s'], s + DIRECTIONS['w'] } if len(scaffolds.intersection(checks)) == 4: alignment_sum += s.x * s.y print(alignment_sum) return scaffolds, position
def print_page(page: list[IntPoint]): mx: int = 0 my: int = 0 for p in page: if p.x > mx: mx = p.x if p.y > my: my = p.y for y in range(my + 1): for x in range(mx + 1): if IntPoint(x, y) in page: print('# ', end='') else: print('. ', end='') print('')
def printMap(m: DroneMap, p: IntPoint = None, file: str = "") -> None: """Prints a map to an optional file.""" x_min, y_min, x_max, y_max = getMapExtents(m) ys = range(y_max, y_min, -1) for y in ys: if m.get(y, None) == None: print("") continue for x in range(x_min,x_max): if m[y].get(x, None) == None: print("░", end="") continue if p != None and IntPoint(x, y) == p: print("D", end="") continue print(MAP_P[m[y][x]], end="") print("")
DM_S = 2 # Move South DM_W = 3 # Move West DM_E = 4 # Move East DM_P = {DM_N: 'north', DM_E: 'east', DM_S: 'south', DM_W: 'west'} S_D = {'n': DM_N, 'e': DM_E, 's':DM_S, 'w':DM_W} # Movement Cycle MC = {DM_N: DM_E, DM_E: DM_S, DM_S: DM_W, DM_W: DM_N} # Movement Deltas MD = {DM_N: IntPoint(0,1), DM_E: IntPoint(1, 0), DM_S: IntPoint(0,-1), DM_W: IntPoint(-1,0)} class Scanner: def __init__(self, program: icp.Thread, direction: int, steps: int, position: IntPoint): self.program = program self.direction = direction self.steps = steps self.position = position
adventofcode.com Day 13 https://adventofcode.com/2021/day/13 """ import fr from intpoint import IntPoint inputs: list[str] = fr.read_as_list('input13') points: list[IntPoint] = [] folds: list[IntPoint] = [] for input in inputs: if input.startswith('fold along '): fold = input[11:].split('=') if fold[0] == 'y': folds.append(IntPoint(0, int(fold[1]))) if fold[0] == 'x': folds.append(IntPoint(int(fold[1]), 0)) elif input != '': points.append(IntPoint.from_str(input)) def fold_page(initial: list[IntPoint], fold: IntPoint) -> list[IntPoint]: print(f'Folding along {fold}.') folded: list[IntPoint] = [] for p in initial: if fold.y == 0: if p.x < fold.x: folded.append(p) if p.x > fold.x: folded.append(IntPoint(fold.x + fold.x - p.x, p.y)) if fold.x == 0: if p.y < fold.y:
# adventofcode.com # Day 20 # https://adventofcode.com/2019/day/20 from common import getFilePath from intpoint import IntPoint import array import time from typing import Dict, Tuple, Set, List import re t_start = time.time() named = re.compile('\w\w') U, D, L, R = IntPoint(0, -1), IntPoint(0, 1), IntPoint(-1, 0), IntPoint(1, 0) FILE_PATH = getFilePath('input20_sample.txt') def idx(inP: IntPoint, inW: int, inH: int) -> int: return inP.x + inP.y * inW a: array = array.array('u') f = open(FILE_PATH, 'r') input = list(f.read()) w, h = 0, 0 x, y = 0, 0 for c in input: #print(c, end='', flush=True)
# adventofcode.com # Day 17 # https://adventofcode.com/2019/day/17 import common, math import intcode as icp from intpoint import IntPoint from typing import Dict, Tuple, Set, List PROGRAM_PATH = common.getFilePath('input17.txt') DIRECTIONS = { 'n': IntPoint(0, -1), 'e': IntPoint(1, 0), 's': IntPoint(0, 1), 'w': IntPoint(-1, 0) } DIRECTIONS_S = { IntPoint(0, -1): 'n', IntPoint(1, 0): 'e', IntPoint(0, 1): 's', IntPoint(-1, 0): 'w' } TURN_RIGHT = { 'n': DIRECTIONS['e'], 'e': DIRECTIONS['s'], 's': DIRECTIONS['w'], 'w': DIRECTIONS['n'] }