def do_part_2(station, data): asteroids = init_asteroids(data) viz = Visualizer((-1, -1, 36, 36), 20, False) for xy in asteroids: viz.draw_point(xy, COLORS[2], 4) grouped_by_angle = defaultdict(list) for location in station.locations_by_angle(asteroids): grouped_by_angle[location.angle].append(location) asteroids_shot = 0 asteroid_200 = None while asteroids_shot < 200: for angle, locations in grouped_by_angle.items(): asteroids_shot += 1 last_asteroid_shot = locations[0].asteroid viz.draw_line((station.xy, last_asteroid_shot.xy), COLORS[1], 2) viz.draw_point(last_asteroid_shot.xy, Color(R=255, G=0, B=0), 4) if asteroids_shot == 200: asteroid_200 = last_asteroid_shot break grouped_by_angle[angle] = locations[1:] viz.draw_point(station.xy, COLORS[0], 10) viz.show() return asteroid_200
def do_part_2(station, data): viz = Visualizer((0, 0, 36, 36), 20, flip_vertical=False) print(f"\nThe asteroid can see this:") asteroids = init_asteroids(data) asteroids_by_degrees = list() for a in asteroids_by_angle(station, asteroids): asteroids_by_degrees.append((rad_to_deg(a[0]), *a)) # for a in sorted(asteroids_by_degrees): # print(a) sets_by_angle = defaultdict(list) for a in sorted(asteroids_by_degrees): sets_by_angle[a[0]].append(a[3]) for k, v in sets_by_angle.items(): if len(v) > 0: print(k, len(v), v) for a in v: if a.xy == (0, 2): print('^^^^^^^^') asteroids_shot = 0 last_asteroid_shot = None while asteroids_shot < 200: for k, v in sets_by_angle.items(): last_asteroid_shot = v[0] sets_by_angle[k] = v[1:] asteroids_shot += 1 viz.draw_line((station.xy, last_asteroid_shot.xy), COLORS[1], 2) viz.draw_point(last_asteroid_shot.xy, COLORS[2], 4) print( f"Boom, shot asteroid nr. {asteroids_shot}: {last_asteroid_shot}" ) if asteroids_shot == 200: code = last_asteroid_shot.x * 100 + last_asteroid_shot.y print(f"200th asteroid shot is: {last_asteroid_shot}: {code}") break viz.draw_point(station.xy, COLORS[0], 10) viz.show() return 0
max_x = max(flats.keys()) + 1 max_y = max(flats.values()) + 1 viz = Visualizer((0, 0, max_x, max_y), scale=SCALE) for m, n in flats.items(): viz.draw_line(((m, 0), (m, n)), COLORS[0], width=SCALE) stap = 0 for rechts, omhoog in data['sprongen']: assert rechts + omhoog <= 4 stap += 1 new_x = x + 1 + rechts new_y = y + omhoog viz.draw_polyline(((x, y), (x + 1, y), (new_x, new_y)), COLORS[1], width=SCALE//10) viz.draw_point((x, y), COLORS[2], size=SCALE//10) x = new_x y = new_y if (x not in flats) or (y < flats[x]): print(f"Santa is op zijn bek gegaan na stap {stap}") break drop_y = flats[x] viz.draw_line(((x, y), (x, drop_y)), COLORS[1]) y = drop_y if x == max(flats.keys()): print("Santa heeft de laatste flat gehaald! (Antwoord = 0).") break