-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunicationTuning.py
More file actions
430 lines (365 loc) · 12.5 KB
/
Copy pathCommunicationTuning.py
File metadata and controls
430 lines (365 loc) · 12.5 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# After reading up on enumeration in pyhton i found a new more clean solution
from collections import deque
import re
def fineTuneDataMarker(file_buffer, markerLength):
for index, value in enumerate(file_buffer):
if len(set(file_buffer[index-markerLength:index])) == markerLength:
return index
def generateTree(file_buffer, d_name):
d_size = 0
dir_list=[]
file_list=[]
line = file_buffer.popleft().split()
if set(["$", "cd"]).issubset(set(line)):
if line[2] != "..":
dir_list.append(generateTree(file_buffer))
elif set(["$", "ls"]).issubset(set(line)):
num_pop = 0
for new_line in file_buffer:
li = new_line.split()
if len(li) > 2:
break
else:
file_list.append(li[0:2])
num_pop += 1
while num_pop != 0:
file_buffer.popleft()
num_pop-=1
while len(file_buffer) > 0:
if file_buffer[0].split()[2] != '..':
nextDir = file_buffer[0].split()[2]
file_buffer.popleft()
dir_list.append(generateTree(file_buffer, nextDir))
else:
file_buffer.popleft()
break
for s, _ in file_list:
if s.isdigit():
d_size += int(s)
for s in dir_list:
d_size += int(s.get("size"))
return dict(name = d_name, dir = dir_list, file = file_list, size = d_size)
def calcDirSize(fileSystem, range):
value = 0
dirs = fileSystem.get("dir")
for dir in dirs:
if dir.get("size") <= range:
value += int(dir.get("size"))
if len(dir.get("dir")) > 0:
value += calcDirSize(dir, range)
return int(value)
def generate(file_buffer):
buffer = deque(file_buffer)
line = buffer.popleft().split()
return generateTree(buffer, line[2])
def listDirs(fileSystem):
value = []
dirs = fileSystem.get("dir")
for dir in dirs:
value.append(int(dir.get("size")))
if len(dir.get("dir")) > 0:
value.extend(listDirs(dir))
return value
def filesystemCleanup(fileSystem, max, free):
value = []
used = fileSystem.get("size")
range = max - free
value.extend(listDirs(fileSystem))
return min([item for item in value if used - item <= range])
def CycleSignalStrength(file_buffer):
cycle_stop = 20
cycle = 0
signal = 1
signals = []
def tick():
nonlocal signal, signals, cycle, cycle_stop
cycle += 1
if cycle == cycle_stop:
signals.append(signal*cycle)
cycle_stop += 40
for instruction in file_buffer.splitlines():
for command in instruction.split():
if command == 'noop':
tick()
elif command == 'addx':
tick()
tick()
else:
signal += int(command)
if cycle_stop > 220:
break
return sum(signals)
def DrawSignal(file_buffer):
cycle = 0
signal = 1
def tick():
nonlocal cycle, signal
if cycle % 40 in (signal-1, signal, signal+1):
print('#', end="")
else:
print('.', end="")
cycle += 1
if cycle % 40 == 0:
print()
for instruction in file_buffer.splitlines():
for command in instruction.split():
if command == 'noop':
tick()
elif command == 'addx':
tick()
tick()
else:
signal += int(command)
def FindBestSignalSpot(file_buffer):
def adj(i, j):
return (i, j-1), (i+1, j), (i, j+1), (i-1, j)
grid = {
(i,j): x
for i, row in enumerate(file_buffer.splitlines())
for j, x in enumerate(row)
}
start = next(k for k, v in grid.items() if v == 'S')
end = next(k for k, v in grid.items() if v == 'E')
grid[start] = 'a'
grid[end] = 'z'
visited = {}
queue = deque([(0,start)])
while len(queue) > 0:
t, p = queue.popleft()
if p in visited:
continue
visited[p]=t
val = ord(grid[p])
for n in adj(*p):
n_val = ord(grid.get(n, '{'))
if n_val - val > 1:
continue
queue.append((t+1, n))
print(visited[end])
def FindBestHickingTrail(file_buffer):
def adj(i, j):
return (i, j-1), (i+1, j), (i, j+1), (i-1, j)
grid = {
(i,j): x
for i, row in enumerate(file_buffer.splitlines())
for j, x in enumerate(row)
}
start = next(k for k, v in grid.items() if v == 'S')
end = next(k for k, v in grid.items() if v == 'E')
grid[start] = 'a'
grid[end] = 'z'
startpositions = []
for k, v in grid.items():
if v == 'a':
startpositions.append(k)
lengths = []
for starts in startpositions:
visited = {}
queue = deque([(0,starts)])
while len(queue) > 0:
t, p = queue.popleft()
if p in visited:
continue
visited[p]=t
val = ord(grid[p])
for n in adj(*p):
n_val = ord(grid.get(n, '{'))
if n_val - val > 1:
continue
queue.append((t+1, n))
if end in visited:
lengths.append(visited[end])
print(min(lengths))
def cmp(x, y):
if isinstance(x, int) and isinstance(y, int):
return x - y
if isinstance(x, list) and isinstance(y, list):
for i, j in zip(x, y):
result = cmp(i,j)
if result != 0:
return result
return len(x) - len(y)
if isinstance(x, list):
return cmp(x, [y])
if isinstance(y, list):
return cmp([x], y)
assert False
def DistresSignal(file_buffer):
signals = [[eval(x), eval(y)] for x, y in list(map(lambda x: x.split() , file_buffer.split('\n\n')))]
values = 0
for i, signal in enumerate(signals):
if cmp(*signal) < 0:
values += i + 1
print(values)
def SortOutSignal(file_buffer):
signals = [[eval(x)] for x in file_buffer.split() if len(x) > 0]
signals.append(eval('[[6]]'))
signals.append(eval('[[2]]'))
n = len(signals)
for i in range(n):
done = True
for j in range(n - i - 1):
if cmp(signals[j], signals[j + 1]) > 0:
signals[j], signals[j + 1] = signals[j + 1], signals[j]
done = False
if done:
break
print((signals.index([[6]])+1) * (signals.index([[2]])+1))
def SandCave(file_buffer):
rockLabyrint = [[x.strip() for x in row.split('->')] for row in file_buffer.split('\n')]
minWidth = 500
maxWidth = 500
settled = 0
layout = deque([deque(['+'])])
def move(x, y):
if x == len(layout)-1 or y == len(layout[0])-1 or y < 0 or x < 0:
return 0, False
if layout[x + 1][y] == '.':
return move(x + 1, y)
elif layout[x + 1][y - 1] == '.':
return move(x + 1, y - 1)
elif layout[x + 1][y + 1] == '.':
return move(x + 1, y + 1)
else:
layout[x][y] = 'o'
return 1, True
def moveX(x, y):
if y == len(layout[0])-1:
for i in range(len(layout) - 1):
layout[i].append('.')
layout[len(layout)-1].append('#')
elif y < 0:
for i in range(len(layout) - 1):
layout[i].appendleft('.')
layout[len(layout)-1].appendleft('#')
y = 0
if layout[x + 1][y] == '.':
return moveX(x + 1, y)
elif layout[x + 1][y - 1] == '.':
return moveX(x + 1, y - 1)
elif layout[x + 1][y + 1] == '.':
return moveX(x + 1, y + 1)
elif layout[x][y] == '+':
layout[x][y] = '0'
return 1, False
else:
layout[x][y] = 'o'
return 1, True
def growCave(width, depth):
miW = minWidth
maW = maxWidth
if width < minWidth:
for _ in range(minWidth-width):
for i in range(len(layout)):
layout[i].appendleft('.')
miW = width
if width > maxWidth:
for _ in range(width-maxWidth):
for i in range(len(layout)):
layout[i].append('.')
maW = width
if depth - len(layout) >= 0:
for _ in range(len(layout)-1, depth):
layout.append(deque(['.' for _ in layout[0]]))
return miW, maW, len(layout)
def rockLine(row):
pos_x, pos_y = list(map(int, row[0].split(',')))
pos_x -= minWidth
layout[pos_y][pos_x] = '#'
for i in range(1, len(row)):
x, y = list(map(int, row[i].split(',')))
x -= minWidth
if pos_x == x:
if pos_y < y:
for s in range(pos_y, y+1):
layout[s][pos_x] = '#'
else:
for s in range(y, pos_y+1):
layout[s][pos_x] = '#'
if pos_y == y:
if pos_x < x:
for s in range(pos_x, x+1):
layout[pos_y][s] = '#'
else:
for s in range(x, pos_x+1):
layout[pos_y][s] = '#'
pos_x, pos_y = x, y
for row in rockLabyrint:
for element in row:
column, stone = list(map(int, element.split(',')))
minWidth, maxWidth, d = growCave(column, stone)
#growCave(maxWidth+1, len(layout)+1)
#growCave(minWidth-1, 0)
for row in rockLabyrint:
rockLine(row)
con = True
sand = [0, layout[0].index('+')]
while con == True:
set_stone, con = move(*sand)
settled += set_stone
print(settled)
for i in range(len(layout)):
for p in range(len(layout[0])):
if layout[i][p] == 'o':
layout[i][p] = '.'
con = True
settled = 0
layout.append(deque(['.' for _ in layout[0]]))
layout.append(deque(['#' for _ in layout[0]]))
while con == True:
sand = [0, layout[0].index('+')]
set_stone, con = moveX(*sand)
settled += set_stone
print(settled)
paint = False
if paint:
for i in range(len(layout)):
for p in range(len(layout[0])):
print(layout[i][p], end='')
print()
def findingBeacons(file_buffer):
minWidth, maxWidth, minDepth, maxDepth = 0, 0, 0, 0
S = set()
B = set()
def test(i,j,S):
for (sx, sy, d) in S:
dist = abs(i-sx) + abs(j-sy)
if dist <= d:
return False #Inside distance for S
return True
for line in file_buffer.split('\n'):
sx, sy, bx, by = map(int, re.findall(r'-?\d+', line))
d = abs(sx - bx) + abs(sy- by)
S.add((sx,sy,d))
B.add((bx,by))
minWidth = min(min( x for x, *_ in S ), min( x for x, *_ in B ))-(max(d for *_, d in S)//2)
maxWidth = max(max( x for x, *_ in S ), max( x for x, *_ in B ))+(max(d for *_, d in S)//2)
minDepth = min(min( y for _, y, _ in S ), min( y for *_, y in B ))-1
maxDepth = max(max( y for _, y, _ in S ), max( y for *_, y in B ))+1
spot = 0
for i in range(minWidth, maxWidth):
j = 2000000
for j in range(minDepth, maxDepth):
if (i,j) not in B and not test(i,j,S):
spot+=1
print(spot)
MIN = 0
MAX = 4_000_000
def gen_outskirts():
for sx, sy, d in S:
d = d + 1
p = [sx-d, sy]
for move in [(1,1),(-1,1),(-1,-1),(1,-1)]:
for _ in range(d):
p[0] += move[0]
p[1] += move[1]
if MIN <= p[0] <= MAX and MIN <= p[1] <= MAX:
yield p
for px, py in gen_outskirts():
for sx, sy, sd in S:
pd = abs(sx-px) + abs(sy-py)
if pd <= sd:
break
else:
print(px, py, px * 4000000 + py)
break