def test_addition(self): p1 = Point(1, 2) p2 = Point(2, 4) p3 = p1 + p2 self.assertEqual(p3.x, 3) self.assertEqual(p3.y, 6) self.assertEqual(p3.xy, (3, 6))
def test_subtraction(self): p1 = Point(1, 2) p2 = Point(2, 4) p3 = p1 - p2 self.assertEqual(p3.x, -1) self.assertEqual(p3.y, -2) self.assertEqual(p3.xy, (-1, -2))
def get_connected_tiles(pos: Point, map): return [ Point(pos.x, pos.y + 1), Point(pos.x, pos.y - 1), Point(pos.x + 1, pos.y), Point(pos.x - 1, pos.y) ]
def get_target_pos(pos: Point, direction: int): if direction == 1: return Point(pos.x, pos.y - 1) elif direction == 2: return Point(pos.x, pos.y + 1) elif direction == 3: return Point(pos.x - 1, pos.y) else: return Point(pos.x + 1, pos.y)
def find_coordinates_and_laser(map: List[str]): coordinates = [] laser = () for y, row in enumerate(map): for x, cell in enumerate(row): if cell == '#': coordinates.append(Point(x,y)) elif cell == 'X': laser = Point(x,y) return coordinates, laser
def inside(self, point): x = Point(self._view_window[0], 0) y = Point(0, self._view_window[1]) translated = conv.iso_to_flat(self.translate(point)) print(self._pos.x) if self._pos.x < translated.x < (self._pos + x).x and self._pos.y < translated.y < (self._pos + y).y: return True else: return False
def vaporize_rotation(laser_pos, asteroids): upper_bounds = Point(max([a.x for a in asteroids]), max([a.y for a in asteroids])) q1 = ( Point(laser_pos.x, 0), Point(upper_bounds.x, laser_pos.y) ) q2 = ( Point(laser_pos.x, laser_pos.y), Point(upper_bounds.x, upper_bounds.y) ) q3 = ( Point(0, laser_pos.y), Point(laser_pos.x, upper_bounds.y) ) q4 = ( Point(0,0), Point(laser_pos.x, laser_pos.y) ) visible = find_visible(laser_pos, asteroids) vaporized = [] for q in [q1,q2,q3,q4]: vap_order = get_vap_order(laser_pos, visible, q) vaporized += vaporize_quadrant(visible, vap_order) return vaporized
def draw(self, screen, camera): lx = Point(self._size.x, 0) ly = Point(0, self._size.y) p0 = converters.flat_to_iso(self._pos).xy p1 = converters.flat_to_iso(self._pos + lx).xy p2 = converters.flat_to_iso(self._pos + lx + ly).xy p3 = converters.flat_to_iso(self._pos + ly).xy points = [p0, p1, p2, p3] if any([camera.inside(Point(p)) for p in points]): points = [camera.translate(Point(p)).xy for p in points] pygame.draw.polygon(screen, self._color, points)
def get_connected_tiles(pos: Point, map): connected = [] for c in [ Point(pos.x, pos.y + 1), Point(pos.x, pos.y - 1), Point(pos.x + 1, pos.y), Point(pos.x - 1, pos.y) ]: if c in map: connected.append(c) return connected
def from_file(file_name): file = open(file_name, 'r') file_content = file.readlines() file.close() matrix = [] for row in file_content: coordinates = [int(c) for c in row.split(",")] point_a = Point(coordinates[0], coordinates[1]) point_b = Point(coordinates[2], coordinates[3]) point_c = Point(coordinates[4], coordinates[5]) matrix.append(Triangle(point_a, point_b, point_c)) return matrix
def turn(robot, dir, map): # can turn right turn = Point(-dir.y, dir.x) next = Point(robot.x + turn.x, robot.y + turn.y) if next in map and map[next] != '.': return ('R', turn) # can turn right turn = Point(dir.y, -dir.x) next = Point(robot.x + turn.x, robot.y + turn.y) if next in map and map[next] != '.': return ('L', turn) return None
def find_coordinates(map: List[str]): coordinates = [] for y, row in enumerate(map): for x, cell in enumerate(row): if cell == '#': coordinates.append(Point(x,y)) return coordinates
def count_containing_origin(self): counter = 0 origin = Point(0, 0) for triangle in self.triangles: if triangle.include(origin): counter += 1 return counter
def create_map(instructions, height, width, xs=0, ys=0): map = {} for y in range(ys, ys + height): for x in range(xs, xs + width): computer = Intcode(instructions, [x, y]) computer.run_program() map[Point(x, y)] = computer.get_output() return map
def walk_to_edge(robot, dir, map): steps = 0 pos = robot while True: pos = Point(pos.x + dir.x, pos.y + dir.y) if pos in map and map[pos] != '.': steps += 1 else: break return steps
def part2(instructions): computer = Intcode(instructions, []) computer.run_program() map = create_map(computer.outputs) robot = [key for (key, value) in map.items() if value == '^'][0] dir = Point(0, -1) movements = [] while True: #walk to edge dist = walk_to_edge(robot, dir, map) if dist > 0: movements.append(dist) robot = Point(robot.x + dir.x * dist, robot.y + dir.y * dist) #try turn direction = turn(robot, dir, map) if direction != None: movements.append(direction[0]) dir = direction[1] if dist == 0 and direction == None: break print(movements) computer = Intcode(instructions, []) computer.memory[0] = 2 computer.run_program() #Main routine: [A,B,B,C,C,A,B,B,C,A] computer.inputs += convert_to_ascii('A,B,B,C,C,A,B,B,C,A') computer.run_program() print_message(computer.outputs) #Function A: 'R', 4, 'R', 12, 'R', 10, 'L', 12 computer.inputs += convert_to_ascii('R,4,R,12,R,10,L,12') computer.run_program() print_message(computer.outputs) #Function B: 'L', 12, 'R', 4, 'R', 12 computer.inputs += convert_to_ascii('L,12,R,4,R,12') computer.run_program() print_message(computer.outputs) #Function C: 'L', 12, 'L', 8, 'R', 10 computer.inputs += convert_to_ascii('L,12,L,8,R,10') computer.run_program() print_message(computer.outputs) computer.inputs += convert_to_ascii('n') computer.run_program() print(computer.outputs[-1])
def create_map(inputs: List[int]) -> Dict[Point, str]: map = {} y = 0 x = 0 for i in inputs: if i == 10: y += 1 x = 0 else: map[Point(x, y)] = chr(i) x += 1 return map
def __init__(self): config = configparser.ConfigParser() config.read(constants.CONFIG_FILE) w = int(config[constants.C_WINDOW][constants.C_WIDTH]) h = int(config[constants.C_WINDOW][constants.C_HEIGHT]) middle = w // 2 x_l = middle - w // 4 x_p = middle + w // 4 ys = [y for y in range(h // 16, h, h//4)] self._buttons = [ Button(Point(x_l, ys[0]), Point(x_p - x_l, h//6), lambda: print("button 1"), "Start New Game"), Button(Point(x_l, ys[1]), Point(x_p - x_l, h//6), lambda: print("button 2"), "Load Game"), Button(Point(x_l, ys[2]), Point(x_p - x_l, h//6), lambda: print("button 3"), "Options"), Button(Point(x_l, ys[3]), Point(x_p - x_l, h//6), lambda: exit(), "Exit"), ]
def create_points_from_numpyimage(arr_image: np.ndarray): pass lst = list() image_shape = arr_image.shape image_ht = image_shape[0] image_width = image_shape[1] for x in range(0, image_width): for y in range(0, image_ht): #print("x=%d y=%d" % (x,y)) #Change coordinate system color = arr_image[y][x] #we want black pixels only if (color > 0.5): continue y_cartesian = image_ht - y - 1 p = pt.Point(x, y_cartesian) lst.append(p) return lst
def explore_map(instructions): computer = Intcode(instructions, []) computer.run_program() pos = Point(0,0) map = {} map[pos] = 1 while not computer.stopped: next = calculate_next_move(pos, map) if next == None: break direction = get_direction(pos, next) computer.set_input(direction) computer.run_program() output = computer.get_output() if output == 0: map[next] = 0 elif output == 1: map[next] = 1 pos = next elif output == 2: map[next] = 2 pos = next print(find_shortest_distance((0,0), pos, map)) print(fill_with_oxygen(map))
def part2(instructions): lower_limit = 0 upper_limit = 0 beam_start_x = 0 y = 500 while True: beam_width, beam_start_x, beam_end_x = get_beam_width( instructions, y, beam_start_x - 5) can_fit = can_fit_square(instructions, Point(beam_end_x - 99, y + 99), Point(beam_end_x, y)) print('{0}-{1}:{2} w:{3}'.format(Point(beam_end_x - 99, y), Point(beam_end_x, y + 99), can_fit, beam_width)) if not can_fit: lower_limit = y y += 100 else: upper_limit = y break y = lower_limit + (upper_limit - lower_limit) // 2 while upper_limit - lower_limit > 1: beam_width, beam_start_x, beam_end_x = get_beam_width( instructions, y, beam_start_x - 50) can_fit = can_fit_square(instructions, Point(beam_end_x - 99, y + 99), Point(beam_end_x, y)) print('Testing at {0}: {1} (lower {2} upper {3})'.format( y, can_fit, lower_limit, upper_limit)) if can_fit: upper_limit = y else: lower_limit = y y = lower_limit + (upper_limit - lower_limit) // 2 beam_width, beam_start_x, beam_end_x = get_beam_width( instructions, upper_limit, beam_start_x - 5) x_start = beam_end_x - 99 target = Point(x_start, upper_limit) print(target.x * 10000 + target.y)
def update(computer, state): computer.run_program() for index in range(0, len(computer.outputs), 3): state[Point(computer.outputs[index], computer.outputs[index + 1])] = computer.outputs[index + 2] return state
def Parabola(x: float) -> float: y = multiplier * pow((x - origin_x), 2) + origin_y return y pass ######### #Objetive - # smoothen out the parabola # begin with lowest x, compute y, x+deltaX, compute y, compute distance, if more than threshold than x+0.5deltax # if less than threshold then x+0.25deltax min_distance = 4 # we expect 2 points to be not any more further than this x_current = 0 y_current = Parabola(x_current) point_last = Point(x_current, y_current) delta_x = 1 lst_points.append(point_last) while (x_current <= img_width): y_current = Parabola(x_current) distance = math.sqrt((point_last.X - x_current)**2 + (point_last.Y - y_current)**2) if (distance > min_distance): #select this point pt = Point(x_current, y_current) lst_points.append(pt) x_current = x_current + 1 point_last = pt else: #too close , move on to the next point x_current = x_current + 1
def flat_to_iso(point): return Point(point.x - point.y, (point.x + point.y) / 2)
def iso_to_flat(point): return Point((2 * point.y + point.x) / 2, (2 * point.y - point.x) / 2)
def create_state(input): tiles = defaultdict(lambda: 0) for index in range(0, len(input), 3): tiles[Point(input[index], input[index + 1])] = input[index + 2] return tiles
vaporized = [] for a in vap_order: vaporized.append(a[1]) visible.remove(a[1]) return vaporized def find_visible(pos, asteroids): visible = [] for target in asteroids: if pos == target: continue if can_detect(pos, target, asteroids): visible.append(target) return visible def part1(asteroids): return find_best_location(asteroids) def part2(pos, asteroids): asteroids.remove(pos) vaporized = vaporize_all(pos, asteroids) return vaporized[199].x * 100 + vaporized[199].y if __name__ == "__main__": with open('day10.txt') as f: contents = f.read() map = parse_map(contents) asteroids = find_coordinates(map) print(part1(asteroids)) print(part2(Point(30,34),asteroids))
def __init__(self, point_a, point_b, point_c): center = Point((point_a.x + point_b.x + point_c.x) / 3, \ (point_a.y + point_b.y + point_c.y) / 3) self.points = [point_a, point_b, point_c] self.points.sort(key=lambda p: atan2(p.x - center.x, p.y - center.y))
image_noisy=skimage.util.random_noise(img,mode="s&p",seed=None, clip=True,salt_vs_pepper=salt_pepper_ratio) # #Generate points using the equation of the Circle # radius=min(img_height, img_width)/2.0 origin_x=img_width*0.5 origin_y=img_height*0.5 lst_points=list() deg_per_radian=360/(math.pi*2.0) delta_theta_degrees=5 delta_theta_radians=delta_theta_degrees * 1/deg_per_radian theta=0.0; while (theta <= (2*math.pi)): x=(math.cos(theta) * radius) + origin_x y=(math.sin(theta) * radius) + origin_y pt=Point(x,y) lst_points.append(pt) theta+=delta_theta_radians print("Generated %d point without noise" % (len(lst_points))) # #Generate noisy around the Parabola # stddev=5 #1 worked with stddev=1 num_noisy_points=20 #1 worked best with stddev=1 #0 #5 #20 lst_points_with_noise=list() for pt_original in lst_points:
def test_creation_2_args(self): p = Point(1, 2) self.assertEqual(p.x, 1) self.assertEqual(p.y, 2) self.assertEqual(p.xy, (1, 2))