-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug-new.py
546 lines (494 loc) · 18 KB
/
debug-new.py
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
total = 0
symbol_format_info = []
number_format_info = []
pointers = []
generated_list = []
sharp_position = []
original_sharp_position = []
group_position = []
group_dict = {}
positions = []
new_position = []
sharp_sum = []
def recursion(index, case_index):
global symbol_format_info, generated_list, sharp_position, group_position, group_dict, original_sharp_position, \
positions, new_position, sharp_sum
answer = 0
if index == len(sharp_position) - 1:
temp_index = -1
temp_bool = False
new_position[index][1] = len(positions[index]) - 1
# print(index, new_position[index][0], new_position[index][1])
if index != 0:
for element_index in range(new_position[index][0], new_position[index][1] + 1):
if positions[index][element_index][0] - 1 >= 0 and sharp_sum[positions[index][element_index][0] - 1] - \
sharp_sum[
positions[index - 1][new_position[index - 1][1]][1]] > 0:
temp_bool = True
break
else:
temp_index = element_index
elif index == 0:
for element_index in range(new_position[index][0], new_position[index][1] + 1):
if positions[index][element_index][0] - 1 >= 0 and sharp_sum[
positions[index][element_index][0] - 1] > 0:
temp_bool = True
break
else:
temp_index = element_index
if temp_index == -1 and temp_bool is True:
return 0
elif temp_index == -1 and temp_bool is False:
pass
else:
new_position[index][1] = temp_index
answer = new_position[index][1] - new_position[index][0] + 1
# print(index, new_position[index][0], new_position[index][1], temp_index)
# print(index, "answer", answer)
return answer
while True:
new_position[index][1] = len(positions[index]) - 1
temp_bool = True
while temp_bool is True:
for element_index in range(len(positions[index + 1])):
if positions[index + 1][element_index][0] - positions[index][new_position[index][0]][1] >= 2:
new_position[index + 1][0] = element_index
break
if sharp_sum[positions[index + 1][new_position[index + 1][0]][0] - 1] - sharp_sum[
positions[index][new_position[index][0]][1]] == 0:
break
new_position[index][0] += 1
if new_position[index][0] > new_position[index][1]:
temp_bool = False
break
if temp_bool is False:
break
temp_index = -1
for element_index in range(new_position[index][0], new_position[index][1] + 1):
if positions[index + 1][new_position[index + 1][0]][0] - positions[index][element_index][1] >= 2:
temp_index = element_index
else:
break
# print(index, new_position[index][0], new_position[index][1], temp_index)
if temp_index == -1:
break
new_position[index][1] = temp_index
temp_index = -1
temp_bool = False
if index != 0:
for element_index in range(new_position[index][0], new_position[index][1] + 1):
if positions[index][element_index][0] - 1 >= 0 and sharp_sum[positions[index][element_index][0] - 1] - \
sharp_sum[positions[index - 1][new_position[index - 1][1]][1]] > 0:
temp_bool = True
break
else:
temp_index = element_index
elif index == 0:
for element_index in range(new_position[index][0], new_position[index][1] + 1):
if positions[index][element_index][0] - 1 >= 0 and sharp_sum[
positions[index][element_index][0] - 1] > 0:
temp_bool = True
break
else:
temp_index = element_index
if temp_index == -1 and temp_bool is True:
break
elif temp_index == -1 and temp_bool is False:
pass
else:
new_position[index][1] = temp_index
# print(index, new_position[index][0], new_position[index][1], temp_index)
ans = new_position[index][1] - new_position[index][0] + 1
if ans <= 0:
break
answer += ans * recursion(index + 1, case_index)
# print(index, "answer", answer)
if new_position[index][1] == len(positions[index]) - 1:
break
new_position[index][0] = new_position[index][1] + 1
new_position[index + 1][0] = 0
new_position[index + 1][1] = len(positions[index + 1]) - 1
return answer
def alignment(start, end, case_index):
global sharp_position, symbol_format_info, generated_list
# print("---------")
# print("question", end)
# print(symbol_format_info[case_index])
# print(generated_list)
temp_index = end
sharp_index_start = -1
sharp_index_end = -1
group_index = -1
length = -1
while True:
sharp_index_end = 0
for element_index in range(temp_index - 1, -1, -1):
if element_index >= len(generated_list):
continue
if generated_list[element_index] == '#':
sharp_index_end = element_index
break
sharp_index_start = -1
for element_index in range(len(sharp_position)):
if sharp_index_end <= sharp_position[element_index][1] and sharp_index_end >= sharp_position[element_index][
0]:
sharp_index_start = sharp_position[element_index][0]
sharp_index_end = sharp_position[element_index][1]
group_index = element_index
break
if sharp_index_end - sharp_index_start + 1 < end - start + 1:
temp_index = sharp_index_start
continue
temp_bool = True
temp_num = 0
for element_index in range(start - 1, -1, -1):
if symbol_format_info[case_index][element_index] == '#':
temp_num += 1
else:
break
if sharp_index_end - sharp_index_start + 1 <= end - start + temp_num + 1:
length = end - sharp_index_end
else:
length = start - sharp_index_start
length = end - sharp_index_end
for element_index in range(sharp_index_start, sharp_index_start + length):
if symbol_format_info[case_index][element_index] == '#':
temp_bool = False
break
if temp_bool is False:
temp_start = -1
temp_end = -1
# print(sharp_index_start, sharp_index_end, temp_num, length)
for element_index in range(sharp_index_start + length - 1, sharp_index_start - 1, -1):
if symbol_format_info[case_index][element_index] == '#':
if temp_bool is False:
temp_bool = True
temp_end = element_index
temp_start = element_index
elif temp_bool is True:
break
# print(temp_start, temp_end)
alignment(temp_start, temp_end, case_index)
sharp_index_start = sharp_position[group_index][0]
sharp_index_end = sharp_position[group_index][1]
if sharp_index_end - sharp_index_start + 1 <= end - start + temp_num + 1:
length = end - sharp_index_end
else:
length = start - sharp_index_start
length = end - sharp_index_end
break
if length >= 0:
for element_index in range(length):
generated_list.insert(sharp_index_start, '.')
temp_index = -1
temp_group = -1
for element_index in range(group_index, len(sharp_position)):
sharp_position[element_index][0] += length
sharp_position[element_index][1] += length
temp_index = sharp_position[element_index][1] + 1
temp_group = element_index
if element_index != len(sharp_position) - 1 and sharp_position[element_index + 1][0] - \
sharp_position[element_index][1] >= 2:
break
for element_index in range(length):
if temp_index >= len(generated_list) or temp_index < 0:
break
del generated_list[temp_index]
for element_index in range(group_index + 1, temp_group + 1):
length = sharp_position[element_index][0] - sharp_position[element_index - 1][1]
for temp_index in range(length - 2):
if temp_index >= len(generated_list) or temp_index < 0:
break
# print(generated_list)
del generated_list[sharp_position[element_index - 1][1] + 1]
sharp_position[element_index][0] -= length - 2
sharp_position[element_index][1] -= length - 2
if element_index != len(sharp_position) - 1:
for temp_index in range(length - 2):
generated_list.insert(sharp_position[element_index][1] + 1, '.')
else:
pass
# print()
for index in range(len(sharp_position)):
length = 0
while True:
temp_bool = True
for element_index in range(sharp_position[index][0], sharp_position[index][1] + 1):
if symbol_format_info[case_index][element_index] == '.':
temp_bool = False
break
if temp_bool is False:
length += 1
generated_list.insert(sharp_position[index][0], '.')
sharp_position[index][0] += 1
sharp_position[index][1] += 1
else:
break
temp_index = sharp_position[index][1] + 1
for element_index in range(index + 1, len(sharp_position)):
if sharp_position[element_index][0] - sharp_position[element_index - 1][1] >= 2:
break
sharp_position[element_index][0] += length
sharp_position[element_index][1] += length
temp_index = sharp_position[element_index][1] + 1
for element_index in range(length):
# print(temp_index, len(generated_list))
if temp_index < 0 or temp_index >= len(generated_list):
break
del generated_list[temp_index]
if length > 0:
for element_index in range(index, len(sharp_position) - 1):
length = 0
while sharp_position[element_index + 1][0] - sharp_position[element_index][1] - length > 2:
del generated_list[sharp_position[element_index][1] + 1]
length += 1
for temp in range(element_index + 1, len(sharp_position)):
sharp_position[temp][0] -= length
sharp_position[temp][1] -= length
# print(generated_list)
# print("end")
def loop_order(case_index):
global pointers, generated_list, number_format_info, symbol_format_info, sharp_position, group_position, group_dict, \
original_sharp_position, positions, new_position, sharp_sum
generated_list = []
for index in range(len(number_format_info[case_index])):
length = number_format_info[case_index][index]
for element in range(length):
generated_list.append('#')
if index < len(number_format_info[case_index]) - 1:
generated_list.append('.')
index = 0
while index < len(symbol_format_info[case_index]):
if symbol_format_info[case_index][index] == '?':
break
if symbol_format_info[case_index][index] != generated_list[index]:
del symbol_format_info[case_index][index]
else:
index += 1
length = len(symbol_format_info[case_index]) - len(generated_list)
index = len(symbol_format_info[case_index]) - 1
while index >= 0:
if symbol_format_info[case_index][index] == '?':
break
if symbol_format_info[case_index][index] != generated_list[index - length]:
del symbol_format_info[case_index][index]
length -= 1
index -= 1
# print(generated_list)
# Use a list store each group's range
sharp_position = []
temp_bool = False
for element_index in range(len(generated_list)):
if generated_list[element_index] == '#':
if temp_bool is False:
temp_bool = True
sharp_position.append([element_index])
else:
if temp_bool is True:
temp_bool = False
sharp_position[-1].append(element_index - 1)
if temp_bool is True:
temp_bool = False
sharp_position[-1].append(len(generated_list) - 1)
# print(symbol_format_info[case_index])
# print(generated_list)
temp_bool = False
while temp_bool is False:
temp_bool = True
index = 0
while index < len(symbol_format_info[case_index]):
if symbol_format_info[case_index][index] == '#' and (
index >= len(generated_list) or (index < len(generated_list) and generated_list[index] == '.')):
end = 0
for element_index in range(index, len(symbol_format_info[case_index])):
if symbol_format_info[case_index][element_index] == '#':
end = element_index
else:
break
start = 0
for element_index in range(index, -1, -1):
if symbol_format_info[case_index][element_index] == '#':
start = element_index
else:
break
alignment(start, end, case_index)
temp_bool = False
index += 1
for index in range(len(sharp_position)):
length = 0
while True:
temp_bool = True
for element_index in range(sharp_position[index][0], sharp_position[index][1] + 1):
if symbol_format_info[case_index][element_index] == '.':
temp_bool = False
break
if temp_bool is False:
length += 1
generated_list.insert(sharp_position[index][0], '.')
sharp_position[index][0] += 1
sharp_position[index][1] += 1
else:
break
temp_index = sharp_position[index][1] + 1
for element_index in range(index + 1, len(sharp_position)):
if sharp_position[element_index][0] - sharp_position[element_index - 1][1] >= 2:
break
sharp_position[element_index][0] += length
sharp_position[element_index][1] += length
temp_index = sharp_position[element_index][1] + 1
for element_index in range(length):
if temp_index < 0 or temp_index >= len(generated_list):
break
del generated_list[temp_index]
if length > 0 and index != len(sharp_position) - 1:
length = 0
while sharp_position[index + 1][0] - sharp_position[index][1] - length > 2:
del generated_list[sharp_position[index][1] + 1]
length += 1
for element_index in range(index + 1, len(sharp_position)):
sharp_position[element_index][0] -= length
sharp_position[element_index][1] -= length
group_position = []
temp_bool = False
all_question = True
for element_index in range(len(symbol_format_info[case_index])):
if symbol_format_info[case_index][element_index] != '.':
if temp_bool is False:
temp_bool = True
all_question = True
group_position.append([element_index])
if symbol_format_info[case_index][element_index] != '?':
all_question = False
else:
if temp_bool is True:
temp_bool = False
group_position[-1].append(element_index - 1)
group_position[-1].append(all_question)
if temp_bool is True:
temp_bool = False
group_position[-1].append(len(symbol_format_info[case_index]) - 1)
group_position[-1].append(all_question)
group_dict = {}
for index in range(len(sharp_position)):
for element_index in range(len(group_position)):
if sharp_position[index][0] >= group_position[element_index][0] and sharp_position[index][1] <= \
group_position[element_index][1]:
group_dict[index] = element_index
break
original_sharp_position = [[element for element in group] for group in sharp_position]
sharp_sum = [0 for i in range(len(symbol_format_info[case_index]))]
for element_index in range(len(symbol_format_info[case_index])):
if symbol_format_info[case_index][element_index] == '#':
if element_index == 0:
sharp_sum[element_index] = 1
else:
sharp_sum[element_index] = sharp_sum[element_index - 1] + 1
else:
if element_index != 0:
sharp_sum[element_index] = sharp_sum[element_index - 1]
print(symbol_format_info[case_index])
print(generated_list)
print(group_position)
print(sharp_position)
positions = [[] for i in range(len(sharp_position))]
for index in range(len(sharp_position)):
positions[index].append([i for i in sharp_position[index]])
positions[index][-1].append(group_dict[index])
if sharp_position[index][0] == 0:
positions[index][-1].append(sharp_sum[sharp_position[index][1]])
else:
positions[index][-1].append(sharp_sum[sharp_position[index][1]] - sharp_sum[sharp_position[index][0] - 1])
bool_value = True
while bool_value:
bool_value = False
for index in range(len(sharp_position)):
length = sharp_position[index][1] - sharp_position[index][0] + 1
if index == len(sharp_position) - 1:
group_index_end = len(group_position)
else:
group_index_end = positions[index + 1][-1][2] + 1
for group_index in range(positions[index][-1][2], group_index_end):
# if group_index != positions[index][-1][2] and group_position[positions[index][-1][2]][2] is False:
# break
# print(index, group_index)
start = max(positions[index][-1][1] + 1, group_position[group_index][0] + length - 1)
if index == len(positions) - 1:
end = group_position[group_index][1] + 1
else:
end = min(positions[index + 1][-1][0] - 1, group_position[group_index][1] + 1)
# print(index, start, end)
if start >= end:
continue
# print(index, start, end)
if group_index > positions[index][-1][2]:
start += 1
if group_position[group_index][0] == 0:
positions[index].append(
[group_position[group_index][0], group_position[group_index][0] + length - 1, group_index,
sharp_sum[group_position[group_index][0] + length - 1]])
else:
positions[index].append(
[group_position[group_index][0], group_position[group_index][0] + length - 1,
group_index, sharp_sum[group_position[group_index][0] + length - 1] - sharp_sum[
group_position[group_index][0]]])
bool_value = True
for temp_index in range(start, end):
if index != len(sharp_position) - 1 and positions[index + 1][-1][0] - temp_index < 2:
break
if symbol_format_info[case_index][temp_index] != '.':
if temp_index - length + 1 == 0:
positions[index].append(
[temp_index - length + 1, temp_index, group_index, sharp_sum[temp_index]])
else:
positions[index].append(
[temp_index - length + 1, temp_index, group_index,
sharp_sum[temp_index] - sharp_sum[temp_index - length]])
bool_value = True
else:
break
# for i in positions:
# print(i)
new_position = [[0, len(positions[index]) - 1] for i in range(len(sharp_position))]
return 0
# return recursion(0, case_index)
with (open("2023/input/input12.txt", "r") as input_file):
# for line in input_file:
# temp = line.strip().split(' ')
# symbol_format_info.append(list(temp[0]))
# number_format_info.append([int(element) for element in temp[1].split(',')])
for line in input_file:
temp = line.strip().split(' ')
symbol_format_info.append([])
number_format_info.append([])
for copy in range(5):
for element in list(temp[0]):
symbol_format_info[-1].append(element)
if copy < 4:
symbol_format_info[-1].append('?')
for element in temp[1].split(','):
number_format_info[-1].append(int(element))
for case in range(len(symbol_format_info)):
decide = False
pointers.append([])
for element_index in range(len(symbol_format_info[case])):
element = symbol_format_info[case][element_index]
if element == '?' or element == '#':
if decide is False:
pointers[-1].append([element_index])
decide = True
else:
if decide is True:
pointers[-1][-1].append(element_index - 1)
decide = False
if decide:
pointers[-1][-1].append(len(symbol_format_info[case]) - 1)
for case in range(len(symbol_format_info)):
a = total
total += loop_order(case)
print(case)
print(total - a)
# print()
# print()
print(total)
# ?###????????#
# . ### ## #