示例#1
0
def find_smaller_a(particles, p):
    slower = []
    a = manhattan(p.a)
    for test_p in particles:
        if manhattan(test_p.a) < a:
            slower.append(test_p)
    return slower
示例#2
0
def find_slower(particles, p):
    slower = []
    v = manhattan(p.v)
    for test_p in particles:
        if manhattan(test_p.v) < v:
            slower.append(test_p)
    return slower
示例#3
0
def problem1(input):
    # the one that has the smallest accelleration will stay closest.
    # so find those with the (same) smalles acc:
    smallest_a = find_smallest_a(input)
    smallest_a_particles = list(
        filter(lambda p: manhattan(p.a) == smallest_a, input))

    # now from this set, those with the smallest velocity will be slower in the long run:
    smallest_v = find_smallest_v(smallest_a_particles)
    smallest_v_particles = list(
        filter(lambda p: manhattan(p.v) == smallest_v, smallest_a_particles))

    # ... and from the slowest, which one is the nearest? that must be the one:
    smallest_d = find_smallest_d(smallest_v_particles)
    smallest_d_particles = list(
        filter(lambda p: manhattan(p.coords) == smallest_d,
               smallest_v_particles))

    solution = smallest_d_particles.pop().index

    # for i in range(0,10000):
    #     for p in input:
    #         p.apply_tick()
    #     min_p = min_dist_particle(input)
    #     print("Min p: {}".format(min_p))

    print("Solution 1: {}".format(solution))
示例#4
0
def problem2(input):
    run = True
    while run:
        collides = find_colliding_p(input)
        for c in collides:
            input.remove(c)

        # after movement, check for possible future colissions:
        # find the particle(s) nearest to 0 (if some have the same smalles distance, consider all)
        # for each found particle, check:
        #   are there SLOWER ones? if yes, we might have a colission in the future, so proceed
        #   are there ones with smaller accelleration? if yes, we might have a colission in the future, so proceed
        #   --> in any case, repeat
        #  if not, we're done.
        smallest_d = find_smallest_d(input)
        smallest_d_particles = list(
            filter(lambda p: manhattan(p.coords) == smallest_d, input))
        to_check = set(input) - set(smallest_d_particles)
        possible_coll_nr = 0
        for p in smallest_d_particles:
            slower = find_slower(to_check, p)
            possible_coll_nr += len(slower)

            smaller_a = find_smaller_a(to_check, p)
            possible_coll_nr += len(smaller_a)
        if possible_coll_nr == 0:
            break

        # move forward in time:
        for p in input:
            p.apply_tick()

    solution = len(input)
    print("Solution 2: {}".format(solution))
示例#5
0
def find_smallest_d(particles):
    min_d = 0
    for i in range(0, len(particles)):
        p = particles[i]
        m = manhattan(p.coords)
        if min_d == 0 or m < min_d:
            min_d = m
    return min_d
示例#6
0
def find_smallest_v(particles):
    min_v = 0
    for i in range(0, len(particles)):
        p = particles[i]
        m = manhattan(p.v)
        if min_v == 0 or m < min_v:
            min_v = m
    return min_v
示例#7
0
def find_smallest_a(particles):
    min_a = 0
    for i in range(0, len(particles)):
        p = particles[i]
        m = manhattan(p.a)
        if min_a == 0 or m < min_a:
            min_a = m
    return min_a
示例#8
0
def min_dist_particle(particles):
    dist = 0
    min_index = -1
    for i in range(0, len(particles)):
        m = manhattan(particles[i].coords)
        if dist == 0 or m < dist:
            dist = m
            min_index = i
    return min_index