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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#advent of code 2018
#day 11
#part 2
#too lazy to clean up the code, might do it later (i.e. never)
#setup
GridSize = 300;
SquareSize = 3;
fcserialnumber = int(open("input.txt",'r').readline());
testing = False;
#testing = True;
if testing:
fcserialnumber = 42; #18=>90,269,16 ; 42 => 21,61;
class fuelcell:
def __init__(self, x, y, serialnumber):
self.positionX = x;
self.positionY = y;
self.rackID = self.positionX + 10;
self.powerlevel = self.rackID * self.positionY;
self.powerlevel += serialnumber;
self.powerlevel *= self.rackID;
self.powerlevel = self.powerlevel//100;
self.powerlevel = self.powerlevel%10;
self.powerlevel -= 5;
def coords(self):
print(f'Fuel Cell coordinates (x,y): {self.positionX},{self.positionY} ');
def __str__(self):
return f'{self.powerlevel}';
'''
testinput = [[3,5,8],[122,79,57],[217,196,39],[101,153,71]];
for t in testinput:
dummycell = fuelcell(t[0],t[1],t[2]);
print(t, " -> ",dummycell.powerlevel);
dummycell.coords();
print("#############################");
print();
'''
fcgrid = [];
gridline = [];
print("#############################");
print("INITIATE BATTERY ARRAY");
for Y in range(GridSize):
gridline.clear();
for X in range(GridSize):
gridline.append(fuelcell(X+1,Y+1,fcserialnumber));
fcgrid.append(gridline.copy());
print("#############################");
print("CALCULATE SQUARE SUBSUMS");
sumtable = [];
gridline.clear();
gridline.append(fcgrid[0][0].powerlevel);
for X in range(1,GridSize):
SubSumFc = fcgrid[0][X].powerlevel + gridline[X-1];
gridline.append(SubSumFc);
sumtable.append(gridline.copy());
currentsubsum = 0;
for Y in range(1,GridSize):
gridline.clear();
SubSumFc = sumtable[Y-1][0] + fcgrid[Y][0].powerlevel;
gridline.append(SubSumFc);
currentsubsum = fcgrid[Y][0].powerlevel;
for X in range(1,GridSize):
currentsubsum += fcgrid[Y][X].powerlevel;
gridline.append(sumtable[Y-1][X] + currentsubsum);
sumtable.append(gridline.copy());
print("#############################");
print("SEARCH FOR HIGHEST SUBSUM SQUARE");
maxpower = -999;
maxX = 0;
maxY = 0;
powernow = 0;
maxsquare = 0;
for Y in range(1,GridSize-3):
ssy = GridSize - Y;
for X in range(1,GridSize-3):
ssx = GridSize - X;
SquareSize = (ssx>=ssy)*ssy + ssx*(ssx<ssy);
for s in range(SquareSize):
powernow = 0;
powernow = sumtable[Y+s][X+s] - sumtable[Y-1][X+s] - sumtable[Y+s][X-1] + sumtable[Y-1][X-1];
if (powernow > maxpower):
maxX = X;
maxY = Y;
maxpower = powernow;
maxsquare = s;
#print("new max power: ", maxpower, " // ", powernow);
print("#############################");
print("Results:");
print(maxX, "\t", maxY, "\t", maxsquare, "\t", maxpower);
fcgrid[maxY][maxX].coords();
print("winrar part 2:\t", fcgrid[maxY][maxX].positionX,",",fcgrid[maxY][maxX].positionY,",",maxsquare+1,end="");
|