示例#1
0
def vectorsToLines(vectors):
    lines = []
    start = point2d(0, 0)
    for i in range(len(vectors)):
        end = point2d(start.x + vectors[i].x, start.y + vectors[i].y)
        lines.append(line2d(point2d(start.x, start.y), point2d(end.x, end.y)))
        start = end
    return lines
示例#2
0
def parseAsVectors(csvStr: str):
    vectors = []
    for move in csvStr.split(","):
        if move[0] == "R":
            vectors.append(point2d(int(move[1:]), 0))
        elif move[0] == "L":
            vectors.append(point2d(-1 * int(move[1:]), 0))
        elif move[0] == "U":
            vectors.append(point2d(0, int(move[1:])))
        elif move[0] == "D":
            vectors.append(point2d(0, -1 * int(move[1:])))

    return vectors
示例#3
0
def part1(computer: intcode_computer):
    pos = point2d(0, 0)
    forward = point2d(0, 1)
    grid = HashGrid(0)

    while not computer.is_complete:
        curr_color = grid.get_value(pos.x, pos.y)
        computer.execute([curr_color])
        grid.set_value(pos.x, pos.y, computer.output.pop(0))
        turn = computer.output.pop(0)
        forward = rotate(forward, turn)
        pos.x += forward.x
        pos.y += forward.y

    print(len(grid.data))
    return
示例#4
0
def parse_input():
    asteroids = []
    data = utils.input.getInput().readlines()
    for y in range(len(data)):
        for x in range(len(data[y])):
            if data[y][x] == "#":
                asteroids.append(point2d(x, y))
    return asteroids
示例#5
0
def part1():
    ic = intcode_computer(utils.input.getInputAsCSVInts())
    g = HashGrid(-1)
    g.set_value(0, 0, 1)

    map_world(ic, g, point2d(0, 0), 0)
    g.set_value(0, 0, -2)
    print(g.as_string(output_map=out_map))
    return
示例#6
0
def part2():
    ic = intcode_computer(utils.input.getInputAsCSVInts())
    g = HashGrid(-1)
    g.set_value(0, 0, 1)

    map_world(ic, g, point2d(0, 0), 0)
    s = g.find(2)[0]
    time = spread_oxygen(g, [s.pos], 0)
    print(time)
    return
示例#7
0
def part2(computer: intcode_computer):
    pos = point2d(0, 0)
    forward = point2d(0, 1)
    grid = HashGrid(0)
    grid.set_value(pos.x, pos.y, 1)

    while not computer.is_complete:
        curr_color = grid.get_value(pos.x, pos.y)
        computer.execute([curr_color])
        grid.set_value(pos.x, pos.y, computer.output.pop(0))
        turn = computer.output.pop(0)
        forward = rotate(forward, turn)
        pos.x += forward.x
        pos.y += forward.y

    output = grid.as_string(output_map={"1": "X"})
    print(output)

    return
示例#8
0
def spread_oxygen(g: HashGrid, locations: List[point2d], depth: int):
    new_locations = []
    for l in locations:
        for direction in d.values():
            p = point2d(l.x + direction.x, l.y + direction.y)
            if g.get_value(p.x, p.y) == 1:
                new_locations.append(p)
                g.set_value(p.x, p.y, 2)

    if len(new_locations) == 0:
        return depth
    else:
        return spread_oxygen(g, new_locations, depth + 1)
示例#9
0
def map_world(ic: intcode_computer, g: HashGrid, pos: point2d, depth: int):
    for dir_code in d:
        next_pos = point2d(pos.x + d[dir_code].x, pos.y + d[dir_code].y)
        if g.get_value(next_pos.x, next_pos.y) == -1:
            ic.execute([dir_code])
            o = ic.pop_output()[0]
            g.set_value(next_pos.x, next_pos.y, o)
            if o == 1 or o == 2:
                map_world(ic, g, next_pos, depth + 1)
                ic.execute([do[dir_code]])
                ic.pop_output()
                if (o == 2):
                    print(depth + 1)
示例#10
0
def part2():
    asteroids = parse_input()
    base = point2d(22, 19)
    up = point2d(0, -1)
    asteroids.remove(base)

    known_angles = []

    for a in asteroids:
        angle = angle_between(up, point2d(a.x - base.x, a.y - base.y))
        found = False
        for k in known_angles:
            if math.isclose(angle, k[0]):
                k[1].append(a)
                found = True
                break
        if not found:
            known_angles.append((angle, [a]))

    # sort the angles
    known_angles.sort(key=lambda x: x[0])

    #sort based on distance
    for k in known_angles:
        k[1].sort(key=lambda a: base.manhattanDistance(a.x, a.y))

    i = 0
    count = 0
    while count < 200:
        if len(known_angles[i][1]) > 0:
            a = known_angles[i][1].pop()
            count += 1
            print("%s: (%s, %s)" % (count, a.x, a.y))
        i += 1
        if i >= len(known_angles):
            i = 0

    return
示例#11
0
import utils.input
from utils.grid import HashGrid, point2d
from days.day11.intcode_computer import intcode_computer
from typing import List

d = {1: point2d(0, 1), 2: point2d(0, -1), 3: point2d(-1, 0), 4: point2d(1, 0)}
do = {1: 2, 2: 1, 3: 4, 4: 3}

out_map = {"-2": "S", "-1": " ", "0": "#", "1": ".", "2": "C"}


def part1():
    ic = intcode_computer(utils.input.getInputAsCSVInts())
    g = HashGrid(-1)
    g.set_value(0, 0, 1)

    map_world(ic, g, point2d(0, 0), 0)
    g.set_value(0, 0, -2)
    print(g.as_string(output_map=out_map))
    return


def map_world(ic: intcode_computer, g: HashGrid, pos: point2d, depth: int):
    for dir_code in d:
        next_pos = point2d(pos.x + d[dir_code].x, pos.y + d[dir_code].y)
        if g.get_value(next_pos.x, next_pos.y) == -1:
            ic.execute([dir_code])
            o = ic.pop_output()[0]
            g.set_value(next_pos.x, next_pos.y, o)
            if o == 1 or o == 2:
                map_world(ic, g, next_pos, depth + 1)
示例#12
0
def rotate(vector: point2d, turn: int):
    angle = (math.pi / 2) if turn == 0 else -1 * (math.pi / 2)
    sin = int(math.sin(angle))
    cos = int(math.cos(angle))
    return point2d(vector.x * cos - vector.y * sin,
                   vector.y * cos + vector.x * sin)
示例#13
0
def get_normal_vector(a: point2d, b: point2d):
    m = math.sqrt(math.pow(a.x - b.x, 2) + math.pow(a.y - b.y, 2))
    ratio = float(1) / m
    return point2d((a.x - b.x) * ratio, (a.y - b.y) * ratio)