Exemplo n.º 1
0
def parse(spots):
    #print(spots)
    most_in_range = 0
    closest = None
    for spot in spots:
        in_range = 0
        for drone in drones:
            #print(spot, drone)
            in_range += 1 if utils.manh(spot, drone[:3]) <= drone[3] else 0
        if in_range > most_in_range:
            most_in_range = in_range
            closest = spot
    return most_in_range, closest
Exemplo n.º 2
0
    consts.append([tuple(map(int, line.split(',')))])

new = True
while new:
    new = False
    n = []
    removed = set()
    for i in range(len(consts) - 1):
        if i in removed:
            continue
        for j in range(i + 1, len(consts)):
            if j in removed:
                continue
            for a in consts[i]:
                for b in consts[j]:
                    if utils.manh(a, b) <= 3:
                        consts[i].extend(consts[j])
                        new = True
                        removed.add(j)
                        break
                else:
                    continue
                break
    for r in sorted(removed, reverse=True):
        consts.pop(r)

ans = len(consts)
print(ans)
if testing:
    if part_one == ans:
        print('PART ONE CORRECT')
Exemplo n.º 3
0
        min_y = y
    if not max_y or y > max_y:
        max_y = y
    coords.append([x, y, n, 0])

on_edge = set()
safe_region = 0
for x in range(min_x, max_x + 1):
    for y in range(min_y, max_y + 1):
        min_d = None
        val = None
        min_count = 0
        dist_from = 0
        for n in range(len(coords)):
            coord = coords[n]
            man_d = utils.manh(coord[:2], (x, y))
            if min_d is None or man_d < min_d:
                min_d = man_d
                val = n
                min_count = 1
            elif man_d == min_d:
                min_count += 1
            dist_from += man_d
        if min_count == 1:
            if x in (min_x, max_x) or y in (min_y, max_y):
                on_edge.add(val)
            coords[val][3] += 1
        safe_region += 1 if dist_from < max_man_sum else 0

r_coords = [
    x for x in sorted(coords, key=lambda x: x[3]) if x[2] not in on_edge
Exemplo n.º 4
0
                                break
                        if is_match:
                            break
                    if is_match:
                        break
                if is_match:
                    break
            if is_match:
                break

        if is_match:
            print(f'\r{n} is oriented at {scanner_loc}')
            scanner_locs.append(scanner_loc)
            for beacon in rot_beacons:
                true_beacon = sumt(scanner_loc, beacon)
                all_beacons.add(true_beacon)
            matched.add(n)
    for n in matched:
        scanners.pop(n)
    print('\rOriented', len(all_beacons), 'beacons;', len(scanners),
          'remaining scanners')

p1 = len(all_beacons)
print(f'Part 1: {p1}')

# Part 2

for a, b in itertools.combinations(scanner_locs, 2):
    p2 = max(p2, utils.manh(a, b))
print(f'Part 2: {p2}')
Exemplo n.º 5
0
    if mins[2] is None or z < mins[2]:
        mins[2] = z
    if maxs[0] is None or x > maxs[0]:
        maxs[0] = x
    if maxs[1] is None or y > maxs[1]:
        maxs[1] = y
    if maxs[2] is None or z > maxs[2]:
        maxs[2] = z
    if maxd is None or r > maxd[3]:
        maxd = drone

max_radius = maxd[3]

ans = 0
for drone in drones:
    if utils.manh(drone[:3], maxd[:3]) <= max_radius:
        ans += 1

print(ans)
if testing:
    if part_one == ans:
        print('PART ONE CORRECT')
    else:
        print('PART ONE FAILED')

print()
print('PART TWO')
ans = None


def parse(spots):
Exemplo n.º 6
0
test = len(sys.argv) > 1
input_file = 'input' + sys.argv[0].split('.')[1].lstrip('/') + ('.test' if test
                                                                else '')

p1 = 0
p2 = 0

dirs = []
with open(input_file) as f:
    dirs = f.readline().rstrip().split(', ')

x, y = 0, 0
direction = 0
visited = set()
for d in dirs:
    turn, dist = d[0], int(d[1:])
    direction += 1 if turn == 'R' else -1
    direction %= 4
    for i in range(dist):
        if direction % 2 == 0:  # N/S
            y += 1 if direction == 0 else -1
        else:
            x += 1 if direction == 1 else -1
        if (x, y) in visited and not p2:
            p2 = utils.manh((x, y))
        visited.add((x, y))

p1 = utils.manh((x, y))
print(f'Part 1: {p1}')
print(f'Part 2: {p2}')
Exemplo n.º 7
0
        x = []
        for arr in (p, v, a):
            x.append(list(map(int, arr.split(','))))
        particles.append(x)

o_particles = deepcopy(particles)

for _ in range(1000):
    closest = None
    closest_dist = float('inf')
    for i, n in enumerate(particles):
        p, v, a = n
        for j in range(len(p)):
            v[j] += a[j]
            p[j] += v[j]
        man_dist = utils.manh(p)
        if man_dist < closest_dist:
            closest = i
            closest_dist = man_dist

p1 = closest
print(f'Part 1: {p1}')
particles = deepcopy(o_particles)

consec = [0, len(particles)]
while consec[0] < 100:
    for i, n in enumerate(particles):
        p, v, a = n
        for j in range(len(p)):
            v[j] += a[j]
            p[j] += v[j]
Exemplo n.º 8
0
steps = {}


def parse(dirs):
    global steps
    x, y = 0, 0
    s = 0
    wire = set()
    for move in dirs:
        direction = move[0]
        length = int(move[1:])
        for i in range(length):
            s += 1
            x += {'R': 1, 'L': -1}.get(direction, 0)
            y += {'U': 1, 'D': -1}.get(direction, 0)
            wire.add((x, y))
            steps[(x, y)] = s + steps.get((x, y), 0)
    return wire


w1 = parse(inp1)
w2 = parse(inp2)

for point in w1 & w2:
    md = utils.manh(point)
    p1 = md if p1 == 0 or md < p1 else p1
    p2 = steps[point] if p2 == 0 or steps[point] < p2 else p2

print(f'Part 1: {p1}')
print(f'Part 2: {p2}')
Exemplo n.º 9
0
width = s - 1

step = 0
direction = 0
while n > goal:
    n -= 1

    if step and step % width == 0:
        direction += 1
    step += 1

    dx, dy = back_moves[direction]
    x += dx
    y += dy

p1 = utils.manh((0, 0), (x, y))
print(f'Part 1: {p1}')

moves = {
    0: (0, -1),
    1: (-1, 0),
    2: (0, 1),
    3: (1, 0),
}
grid = {(0, 0): 1}
x, y = 1, 0
ring = 1
step = 0
direction = 0
while True:
    width = ring * 2