#advent of code 2018 #day 10 #part 1 & part 2 class light: def __init__(self, x,y,vx,vy): self.Pos = [x,y]; self.Vel = [vx,vy]; def CalcNextPos(self, t): return [self.Pos[0]+t*self.Vel[0],self.Pos[1]+t*self.Vel[1]]; def __str__(self): return f'<{self.Pos[0]},{self.Pos[1]}>'; def CalcArea(x1,x2,y1,y2): return abs(x1-x2)*abs(y1-y2); f = open("input.txt",'r'); Testing = False; #Testing = True; if Testing: f.close(); f = open("testinput.txt",'r'); lights = []; for tl in f: tl = tl.replace("position=<", ""); tl = tl.replace("velocity=<", ""); tl = tl.replace(">", ""); tl = tl.replace(",", ""); tl=tl.split(); lights.append( light(int(tl[0]),int(tl[1]),int(tl[2]),int(tl[3])) ); f.close(); Xmin = min(lights, key = lambda x: x.Pos[0]).Pos[0]; Xmax = max(lights, key = lambda x: x.Pos[0]).Pos[0]; Ymin = min(lights, key = lambda y: y.Pos[1]).Pos[1]; Ymax = max(lights, key = lambda y: y.Pos[1]).Pos[1]; #print(Xmin,Xmax,Ymin,Ymax); PrevArea = CalcArea(Xmin,Xmax,Ymin,Ymax); ElapsedTime = 0; while True: #print(ElapsedTime); ElapsedTime += 1; Xmin = min(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[0]).CalcNextPos(ElapsedTime)[0]; Xmax = max(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[0]).CalcNextPos(ElapsedTime)[0]; Ymin = min(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[1]).CalcNextPos(ElapsedTime)[1]; Ymax = max(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[1]).CalcNextPos(ElapsedTime)[1]; CurrArea = CalcArea(Xmin,Xmax,Ymin,Ymax); if (CurrArea > PrevArea): MessageTime = ElapsedTime-1; break; PrevArea = CurrArea; pointlist = []; for l in lights: xy = l.CalcNextPos(MessageTime); pointlist.append((xy[0],xy[1])); Xmin = min(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[0]).CalcNextPos(ElapsedTime)[0]; Xmax = max(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[0]).CalcNextPos(ElapsedTime)[0]; Ymin = min(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[1]).CalcNextPos(ElapsedTime)[1]; Ymax = max(lights, key = lambda L: L.CalcNextPos(ElapsedTime)[1]).CalcNextPos(ElapsedTime)[1]; print("###########"); print("part1:"); for Y in range(Ymin,Ymax+1): for X in range(Xmin,Xmax+1): sign = " "; if ((X,Y) in pointlist): sign = "#"; print(sign, end=""); #print(end=""); print(); part2 = MessageTime; print("###########"); print("part2 ,", part2);