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
import json from visualize import Visualizer, COLORS with open("infi-2019-input.json") as f: data = json.load(f) flats = {k: v for k, v in data['flats']} x, y = data['flats'][0] print(flats) max_x = max(flats.keys()) + 1 max_y = max(flats.values()) + 1 viz = Visualizer((0, 0, max_x, max_y)) for x, y in flats.items(): viz.draw_line(((x, 0), (x, y)), COLORS[0]) viz.show()
SCALE = 100 with open("infi-2019-input.json") as f: data = json.load(f) flats = {k: v for k, v in data['flats']} x = min(flats.keys()) y = flats[x] 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