From c1fdc53e46d456d6bfa4f5a416a1e58e8a6468ef Mon Sep 17 00:00:00 2001 From: b-idea Date: Thu, 28 Sep 2023 13:25:01 +0200 Subject: added days: 10,11,12,14,16,18 --- 2018/aoc2018-d10.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 2018/aoc2018-d10.py (limited to '2018/aoc2018-d10.py') diff --git a/2018/aoc2018-d10.py b/2018/aoc2018-d10.py new file mode 100755 index 0000000..7bd2ae5 --- /dev/null +++ b/2018/aoc2018-d10.py @@ -0,0 +1,88 @@ +#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); -- cgit v1.2.3