1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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);
|