-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_20.py
312 lines (273 loc) · 9.43 KB
/
aoc_20.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
#!/usr/bin/env python3
import numpy as np
from tqdm import tqdm
"""
when to move which number (at its current pos)
._____________________ 1.––. 8. 15.
| .__________________ 2.<–´–. 9. .
| | ._______________ 3.<–––´–. 10. .
| | | .____________ 4.<–––––´ . .
| | | | ._________ 5. .
| | | | | .______ 6. .
| | | | | | .___ 7. 14.
| | | | | | |
1 2 -3 3 -2 0 4
1 2 -3 3 -2 0 4
`>>+´
2 1 -3 3 -2 0 4
`>>.>>+´
1 -3 2 3 -2 0 4
#<<´ `+<<.<<.
1 2 3 -2 -3 0 4
1 -3 2 3 -2 0 4
1 2 3 -2 0 4 -3 # 1 <
1 2 3 -2 0 -3 4 # 2 <
1 2 3 -2 -3 0 4 # 3 <
1 2 3 -2 -3 0 4
`>>.>>.>>+´
1 2 -2 -3 0 3 4
#<<.<<´ `+
1 2 -3 0 3 4 -2
*
1 2 -3 0 3 4 -2
.>>.>>.>>.>>+´ `>>#
1 2 -3 0 3 4 -2
4 1 2 -3 0 3 -2 # 1 >
1 4 2 -3 0 3 -2 # 2 >
1 2 4 -3 0 3 -2 # 3 >
1 2 -3 4 0 3 -2 # 4 >
2 % 7 => 5
-1 % 7 => 6
check out if there is only one single '0' in the input and at which line:
~$ grep "^0" -n input_20.txt
"""
sample_input = [
"1\n",
"2\n",
"-3\n",
"3\n",
"-2\n",
"0\n",
"4\n"
]
def get_new_idx(arr,old_idx):
N = len(arr)
n = arr[old_idx]
new_idx = None
if n > 0:
new_idx = (n+old_idx) % (N-1)
if old_idx==N-1 and new_idx==0: new_idx = N-1
else:
new_idx = (n-(N-1-old_idx)) % (N-1)
if old_idx!=0 and new_idx==0: new_idx = N-1
return new_idx
def shift_all_left_by_1_cyclic(arr,old_idx,new_idx):
"""
- shift all elems in [old_idx,new_idx] left by 1, cyclically
- old_idx and new_idx are valid indices in arr
"""
assert old_idx < new_idx, "precondition violated!"
tmp = arr[old_idx]
for i in range(old_idx,new_idx,+1):
arr[i] = arr[i+1]
arr[new_idx] = tmp
"""# return a map of the changed indices: old_idx –> new_idx
map_old2new = {i: i-1 for i in range(old_idx+1,new_idx+1)}
map_old2new[old_idx] = new_idx
return map_old2new"""
def shift_all_right_by_1_cyclic(arr,old_idx,new_idx):
"""
- shift all elems in [old_idx,new_idx] right by 1, cyclically
- old_idx and new_idx are valid indices in arr
"""
assert old_idx > new_idx, "precondition violated!"
tmp = arr[old_idx]
for i in range(old_idx,new_idx,-1):
arr[i] = arr[i-1]
arr[new_idx] = tmp
"""# return a map of the changed indices: old_idx –> new_idx
map_old2new = {i: i+1 for i in range(new_idx,old_idx)}
map_old2new[old_idx] = new_idx
return map_old2new"""
def print_before_and_after(old_idx,new_idx,arr_before,arr_after):
print(f" old_idx,new_idx: {old_idx},{new_idx}")
print(f" initially: {arr_before}")
fw = np.max( list(map(lambda x: len(str(x)), [np.min(arr_after),np.max(arr_after)])) )
s,e = (old_idx,new_idx) if old_idx < new_idx else (new_idx,old_idx)
r = range(s,e+1)
arr_str = ""
for i,v in enumerate(arr_after):
if i in r:
arr_str += f"\033[1;33m{v:{fw}}\033[0;0m "
else:
arr_str += f"{v:{fw}} "
#print(f" after shift: {arr_after}")
print(f" after shift: [{arr_str}\b]")
def testing_nums(orig_arr,nums_kind="negative"):
#if nums_kind != "negative" and nums_kind != "positive":
if not (nums_kind == "negative" or nums_kind == "positive"):
raise Exception("only support 'negative' and 'positive'")
N = len(orig_arr)
for old_idx in range(N):
for n in range(1,N+(N-1)*1):
if nums_kind == "negative": n = -n
print(f"testing n={n} @idx {old_idx}:")
arr = orig_arr.copy()
arr[old_idx] = n
arr_before_shift = arr.copy()
new_idx = get_new_idx(arr,old_idx)
if new_idx==old_idx:
print(f" new==old, nothing to do for\n {arr_before_shift}")
continue
elif new_idx > old_idx:
shift_all_left_by_1_cyclic(arr, old_idx, new_idx)
print_before_and_after(old_idx, new_idx, arr_before_shift, arr)
else: #new_idx < old_idx
shift_all_right_by_1_cyclic(arr, old_idx, new_idx)
print_before_and_after(old_idx, new_idx, arr_before_shift, arr)
print("–"*5)
def test_shifting(N_entries=5):
print("testing cyclic shifting:")
xi = [f"x{i}" for i in range(N_entries)]
orig = np.array(xi,dtype=object) # dtype=object, so I can mix int with strings
print("–"*50)
testing_nums(orig,nums_kind="positive")
print("–"*50)
testing_nums(orig,nums_kind="negative")
print("–"*50)
def get_old_idx(nums_indices,list_idx):
for j,v in enumerate(nums_indices):
if v == list_idx:
return j
raise Exception("shouldn't get here!")
"""
TODO: no separate nums_indices and arr, but zip them in array of pairs (idx,num)
pairs = [(idx,num) for (idx,num) in zip(range(N),data)] #np.array(data.copy())
print(f"pairs: {pairs}")
print(f"indices: {[p[0] for p in pairs]}")
print(f"numbers: {[p[1] for p in pairs]}")
TODO: only shift indices, ans use map_idx2num
"""
def mix_arr(data,arr,nums_indices):
N = len(data)
for i in tqdm(range(N)):
list_idx = i % N
#if list_idx==0: print("–––")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n = data[list_idx]
arr_before_shift = arr.copy()
#old_idx = nums_indices[list_idx]
old_idx = get_old_idx(nums_indices,list_idx)
new_idx = get_new_idx(arr,old_idx)
#print(f"idx % N: {list_idx} –– num: old_idx –> new_idx: {n:2}: {old_idx}–>{new_idx}")
if new_idx==old_idx:
#print(f" new==old, nothing to do for\n {arr_before_shift}")
continue
elif new_idx > old_idx:
# update arr
shift_all_left_by_1_cyclic(arr, old_idx, new_idx)
# update nums_indices
shift_all_left_by_1_cyclic(nums_indices, old_idx, new_idx)
"""print(f" nums_indices after [shift L]: {nums_indices}")
print_before_and_after(old_idx, new_idx, arr_before_shift, arr)"""
else: #new_idx < old_idx
# update arr
shift_all_right_by_1_cyclic(arr, old_idx, new_idx)
# update nums_indices
shift_all_right_by_1_cyclic(nums_indices, old_idx, new_idx)
"""print(f" nums_indices after [shift R]: {nums_indices}")
print_before_and_after(old_idx, new_idx, arr_before_shift, arr)"""
#print(f" from nums_indices: {np.array([data[i] for i in nums_indices])}")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if i==7-1: break
"""print(f"after round {i_round+1}, arr:")
[print(e) for e in arr]"""
def solve(data,whichPart="Part1"):
data = [int(l.strip()) for l in data]
N = len(data)
arr = np.array(data.copy())
#print(f"data: {data}")
print(f"have \033[1;31m{len(arr)-len(np.unique(arr))}\033[0;0m non-unique vals")
nums_indices = [i for i in range(N)]
#print(f"nums_indices initially: {nums_indices}")
#––– Part 1
if whichPart=="Part1":
mix_arr(data,arr,nums_indices)
#––– Part 2
if whichPart=="Part2":
arr *= 811589153
for i_round in tqdm(range(10)):
mix_arr(data,arr,nums_indices)
print(f"arr: {arr}")
print(f"from nums_indices: {np.array([data[i] for i in nums_indices])}")
zero_idx = np.where(arr==0)[0][0]
print(f"0 @idx {zero_idx} –– N elems left of 0: {arr.size-1-zero_idx}")
# get 1000th, 2000th, and 3000th number after 0 (wrap around)
req_indices = [(zero_idx+offset)%N for offset in np.array([1000,2000,3000])]
print(f"idxs of 1000th,2000th,3000th num after zero_idx: {req_indices}")
print(f"nums of 1000th,2000th,3000th num after zero_idx: {arr[req_indices]}")
print(f">> SOLUTION Part 1/2 (sum of these 3 nums): {sum(arr[req_indices])}")
if __name__ == "__main__":
#––– testing cyclic shifting
#test_shifting()
#exit()
#––– sample input
print("for sample input:")
solve(sample_input,whichPart="Part1")
#exit()
#––– Part 1 and Part 2
print("for input file:")
with open('input_20.txt', 'r') as f:
file_data = f.readlines()
solve(file_data,whichPart="Part1")
"""
##note:
## np.roll(a, 1) == right_shift(a,1)
## np.roll(a,-1) == left_shift(a,1)
##shift positive:
## 2 1 -3 3 -2 0 4
## `>>.>>´
## 2 1 -3 3 -2 0 4
## `>>´
## 1 2 -3 3 -2 0 4
## `>>´
## 1 -3 2 3 -2 0 4
##shift negative:
## 2 1 -3 3 -2 0 4
## `<<.<<´
## 2 1 -3 3 -2 0 4
## `<<´
## 2 1 -3 -2 3 0 4
## `<<´
## 2 1 -2 -3 3 0 4
##
def shift_positive(a,old_idx,new_idx):
# number n > 0
# => shift n to right by n
# and all @idx
# index_of(n)+n, index_of(n)+(n-1), ..., index_of(n)+1
# to the left
tmp = a[old_idx]
for i in range(old_idx+1,new_idx+1,+1):
a[i-1] = a[i]
a[new_idx] = tmp
def shift_negative(a,old_idx,new_idx):
# number n < 0
# => shift n to left by n
# and all @idx
# index_of(n)-|n|, index_of(n)-(|n|-1), ..., index_of(n)-1
# to the right
tmp = a[old_idx]
for i in range(old_idx-1,new_idx-1,-1):
a[i+1] = a[i]
a[new_idx] = tmp
a = np.array((2, 1, -3, 3, -2, 0, 4))
print(f"a before: {a}")
shift_positive(a,0,2)
print(f"a after: {a}")
print("–––")
a = np.array((2, 1, -3, 3, -2, 0, 4))
print(f"a before: {a}")
shift_negative(a,4,2)
print(f"a after: {a}")
"""