-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsl.py
1114 lines (873 loc) · 40.7 KB
/
dsl.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from .arc_types import *
def identity(x: Any) -> Any:
"""Returns the input unchanged"""
return x
#def add(a: Numerical, b: Numerical) -> Numerical:
@overload
def add[T: Numerical](a: T, b: int) -> T: ... # Order matters...
@overload
def add[T: Numerical](a: T, b: T) -> T: ...
def add(a, b):
"""Adds two numbers or tuples element-wise"""
if isinstance(a, int) and isinstance(b, int):
return a + b
elif isinstance(a, tuple) and isinstance(b, tuple):
return (a[0] + b[0], a[1] + b[1])
elif isinstance(a, int) and isinstance(b, tuple):
return (a + b[0], a + b[1])
return (a[0] + b, a[1] + b)
#def subtract(a: Numerical, b: Numerical) -> Numerical:
def subtract[T: Numerical](a:T, b:T) -> T:
"""Subtracts two numbers or tuples element-wise"""
if isinstance(a, int) and isinstance(b, int):
return a - b
elif isinstance(a, tuple) and isinstance(b, tuple):
return (a[0] - b[0], a[1] - b[1])
elif isinstance(a, int) and isinstance(b, tuple):
return (a - b[0], a - b[1])
return (a[0] - b, a[1] - b)
#def multiply(a: Numerical, b: Numerical) -> Numerical:
@overload
def multiply[T: Numerical](a: T, b: int) -> T: ... # Order matters...
@overload
def multiply[T: Numerical](a: T, b: T) -> T: ...
def multiply(a, b):
"""Multiplies two numbers or tuples element-wise"""
if isinstance(a, int) and isinstance(b, int):
return a * b
elif isinstance(a, tuple) and isinstance(b, tuple):
return (a[0] * b[0], a[1] * b[1])
elif isinstance(a, int) and isinstance(b, tuple):
return (a * b[0], a * b[1])
return (a[0] * b, a[1] * b)
#def divide(a: Numerical, b: Numerical) -> Numerical:
def divide[T: Numerical](a: T, b: T) -> T:
"""Performs floor division of two numbers or tuples element-wise"""
if isinstance(a, int) and isinstance(b, int):
return a // b
elif isinstance(a, tuple) and isinstance(b, tuple):
return (a[0] // b[0], a[1] // b[1])
elif isinstance(a, int) and isinstance(b, tuple):
return (a // b[0], a // b[1])
return (a[0] // b, a[1] // b)
#def double(n: Numerical) -> Numerical:
def double[T: Numerical](n: T) -> T:
"""Multiplies a number or tuple by two"""
return n * 2 if isinstance(n, int) else (n[0] * 2, n[1] * 2)
#def halve(n: Numerical) -> Numerical:
def halve[T: Numerical](n: T) -> T:
"""Performs floor division of a number or tuple by two"""
return n // 2 if isinstance(n, int) else (n[0] // 2, n[1] // 2)
def is_even(n: Integer) -> Boolean:
"""Checks if a number is even"""
return n % 2 == 0
# was 'invert'
#def invert(n: Numerical) -> Numerical:
def negate[T: Numerical](n: T) -> T:
"""Returns the additive inverse (negation) of a number or tuple"""
return -n if isinstance(n, int) else (-n[0], -n[1])
def is_positive(x: Integer) -> Boolean:
""" positive """
return x > 0
def logical_not(a: Boolean) -> Boolean:
"""Returns the logical NOT of the input"""
return not a
# was both
def logical_and(a: Boolean, b: Boolean) -> Boolean:
"""Returns the logical AND of the inputs"""
return a and b
# was either
def logical_or(a: Boolean, b: Boolean) -> Boolean:
"""Returns the logical OR of the inputs"""
return a or b
def is_equal(a: Any, b: Any) -> Boolean:
"""Returns True iff 'a' and 'b' are equal"""
return a == b
def greater_than(a: Integer, b: Integer) -> Boolean:
"""Returns True iff 'a' is greater than 'b'"""
return a > b
def contains(value: Any, container: Container) -> Boolean:
"""Returns True iff 'value' is present in 'container'"""
return value in container
# was combine
#def union(a: Container, b: Container) -> Container:
def union[T: Container](a: T, b: T) -> T:
"""Returns the union of two containers of the same type"""
return type(a)((*a, *b))
def intersection(a: FrozenSet,b: FrozenSet) -> FrozenSet:
"""Returns the intersection of two containers"""
return a & b
#def difference(a: FrozenSet, b: FrozenSet) -> FrozenSet:
def difference[T: FrozenSet](a: T, b: T) -> T:
"""Returns the set difference of two containers (elements in 'a' but not in 'b')"""
return type(a)(e for e in a if e not in b)
# was dedupe
def remove_duplicates(tup: Tuple) -> Tuple:
"""Removes duplicate elements from a tuple, preserving order"""
return tuple(e for i, e in enumerate(tup) if tup.index(e) == i)
# was order
def sort(container: Container, compfunc: Callable) -> Tuple:
"""Sorts a container and returns the result as a tuple. Uses the provided function for sorting"""
return tuple(sorted(container, key=compfunc))
def repeat(item: Any, times: Integer) -> Tuple:
"""Creates a tuple containing 'item' repeated 'times'"""
return tuple(item for i in range(times))
def size(container: Container) -> Integer:
"""Number of elements in the container"""
return len(container)
#def maximum(container: IntegerSet) -> Integer:
def maximum(container: Container) -> Integer:
"""Returns the maximum value in a container (0 if empty)"""
return max(container, default=0)
#def minimum(container: IntegerSet) -> Integer:
def minimum(container: Container) -> Integer:
"""Returns the minimum value in a container (0 if empty)"""
return min(container, default=0)
def valmax(container: Container, compfunc: Callable) -> Integer:
""" maximum by custom function """
return compfunc(max(container, key=compfunc, default=0))
def valmin(container: Container, compfunc: Callable) -> Integer:
""" minimum by custom function """
return compfunc(min(container, key=compfunc, default=0))
def argmax(container: Container, compfunc: Callable) -> Any:
""" largest item by custom order """
return max(container, key=compfunc)
def argmin(container: Container, compfunc: Callable) -> Any:
""" smallest item by custom order """
return min(container, key=compfunc)
def most_common(container: Container) -> Any:
""" most common item """
return max(set(container), key=container.count)
def least_common(container: Container) -> Any:
""" least common item """
return min(set(container), key=container.count)
def increment[T: Numerical](x: T) -> T:
""" incrementing """
return x + 1 if isinstance(x, int) else (x[0] + 1, x[1] + 1)
def decrement[T: Numerical](x: T) -> T:
""" decrementing """
return x - 1 if isinstance(x, int) else (x[0] - 1, x[1] - 1)
def crement[T: Numerical](x: T) -> T:
""" incrementing positive and decrementing negative """
if isinstance(x, int):
return 0 if x == 0 else (x + 1 if x > 0 else x - 1)
return (
0 if x[0] == 0 else (x[0] + 1 if x[0] > 0 else x[0] - 1),
0 if x[1] == 0 else (x[1] + 1 if x[1] > 0 else x[1] - 1)
)
def sign[T: Numerical](x: T) -> T:
""" sign """
if isinstance(x, int):
return 0 if x == 0 else (1 if x > 0 else -1)
return (
0 if x[0] == 0 else (1 if x[0] > 0 else -1),
0 if x[1] == 0 else (1 if x[1] > 0 else -1)
)
def to_vertical_vec(i: Integer) -> IntegerTuple:
""" vector pointing vertically """
return (i, 0)
def to_horizontal_vec(j: Integer) -> IntegerTuple:
""" vector pointing horizontally """
return (0, j)
def initset(value: Any) -> FrozenSet:
""" initialize container """
return frozenset({value})
def insert(value: Any, container: FrozenSet) -> FrozenSet:
""" insert item into container """
return container.union(frozenset({value}))
#def remove(value: Any, container: Container) -> Container:
def remove[T: Container](value: Any, container: T) -> T:
""" remove item from container """
return type(container)(e for e in container if e != value)
def get_first(container: Container) -> Any:
""" first item of container """
return next(iter(container))
def get_other(container: Container, value: Any) -> Any:
""" other value in the container """
return get_first(remove(value, container))
def get_last(container: Container) -> Any:
""" last item of container """
return max(enumerate(container))[1]
# was merge
#def flatten(containers: ContainerContainer) -> Container:
@overload
def flatten(x: FrozenSet[Object]) -> Object: ...
@overload
def flatten(x: Grid) -> Grid: ... # ??
@overload
def flatten(x: Indices) -> Indices: ... # ??
def flatten(containers):
"""Flattens a nested container into a single container"""
return type(containers)(e for c in containers for e in c)
#def sfilter(container: Container, condition: Callable) -> Container:
def keep_if_condition[T: Container](container: T, condition: Callable) -> T:
"""Returns 'container' with only the elements that satisfy 'condition'"""
return type(container)(e for e in container if condition(e))
# was mfilter
def keep_if_condition_and_flatten(container: Container, condition: Callable) -> FrozenSet:
"""Returns the elements of 'container' than satisfy 'condition', in a flattened form"""
return flatten(keep_if_condition(container, condition))
def extract_first_matching(container: Container, condition: Callable) -> Any:
"""Returns the first element of 'container' that satisfies 'condition'"""
return next(e for e in container if condition(e))
def interval(start: Integer, stop: Integer, step: Integer) -> Tuple:
"""Returns a tuple containing integers from `range(start, stop, step)`"""
return tuple(range(start, stop, step))
def to_tuple(container: FrozenSet) -> Tuple:
"""Returns a tuple containing the contents of 'container'"""
return tuple(container)
#def as_item_tuple[T:Union[Grid, Objects, Tuple]](a: T, b: T) -> Tuple:
def as_generic_tuple[T](a: T, b: T) -> Tuple: # new
"""Returns the tuple (a, b)"""
return (a, b)
def as_tuple(a: Integer, b: Integer) -> IntegerTuple:
"""Returns the integer tuple (a,b)"""
return (a, b)
def make_cell(color: Color, location: Tuple) -> IntegerTuple: # new
"""Returns the Cell(color, location), where 'location' is a pair of coordinates"""
return (color, location)
# was product
def cartesian_product(a: Container, b: Container) -> FrozenSet:
"""Returns the Cartesian product of two containers as a set of tuples"""
return frozenset((i, j) for j in b for i in a)
def pairwise(a: Tuple, b: Tuple) -> TupleTuple:
"""Returns the pairwise zipping of the two tuples 'a' and 'b'"""
return tuple(zip(a, b))
def condition_if_else(condition: Boolean, a: Any, b: Any) -> Any:
"""if condition==True: return a; else: return b"""
return a if condition else b
def compose( outer: Callable, inner: Callable ) -> Callable:
"""Composes two functions: `outer(inner(x))`"""
return lambda x: outer(inner(x))
def chain( h: Callable, g: Callable, f: Callable ) -> Callable:
"""Composes three functions: `h(g(f(x)))`"""
return lambda x: h(g(f(x)))
# was matcher
def equals( function: Callable, target: Any ) -> Callable:
"""Creates a function that checks if the result of 'function(x)' equals 'target'"""
return lambda x: function(x) == target
# was rbind
def fix_last_argument( function: Callable, fixed_arg: Any ) -> Callable:
"""Returns a new function with the rightmost argument fixed to 'fixed_arg'"""
n = function.__code__.co_argcount
if n == 2:
return lambda x: function(x, fixed_arg)
elif n == 3:
return lambda x, y: function(x, y, fixed_arg)
else:
return lambda x, y, z: function(x, y, z, fixed_arg)
# was lbind
def fix_first_argument( function: Callable, fixed_arg: Any ) -> Callable:
"""Returns a new function with the leftmost argument fixed to 'fixed_arg'"""
n = function.__code__.co_argcount
if n == 2:
return lambda y: function(fixed_arg, y)
elif n == 3:
return lambda y, z: function(fixed_arg, y, z)
else:
return lambda y, z, a: function(fixed_arg, y, z, a)
def power( function: Callable, n: Integer ) -> Callable:
"""Returns a function that applies 'function' to its argument 'n' times"""
if n == 1:
return function
return compose( function, power(function, n-1) )
# was fork(!)
def combine_two_function_results( outer: Callable, f1: Callable, f2: Callable ) -> Callable:
"""Combines the results of two functions ('f1' and 'f2') using 'outer'"""
return lambda x: outer(f1(x), f2(x))
# was apply
#def apply(function: Callable, container: Container) -> Container:
def transform[T: Container](function: Callable, container: T) -> T:
"""Applies a function to each element of a container"""
return type(container)(function(e) for e in container)
# was rapply
def apply_each_function(functions: Container, value: Any) -> Container:
"""Applies each function in a container to a single value"""
return type(functions)(function(value) for function in functions)
# was mapply
# also : container: ContainerContainer # Changed by mdda
def transform_and_flatten(function: Callable, container: Union[ContainerContainer,ColorSet]) -> FrozenSet:
"""Applies a transform to a nested container and flattens the result"""
return flatten(transform(function, container))
# was papply
def transform_both(function: Callable, a: Tuple, b: Tuple) -> Tuple:
"""Applies a function pairwise to elements of two tuples"""
return tuple(function(i, j) for i, j in zip(a, b))
# was mpapply
def transform_both_and_flatten(function: Callable, a: Tuple, b: Tuple) -> Tuple:
"""Applies a function pairwise to two tuples and flattens the result"""
return flatten(transform_both(function, a, b))
# was prapply
def apply_function_on_cartesian_product(function: Callable, a: Container, b: Container) -> FrozenSet:
"""Applies a function to all pairs in the Cartesian product of two containers"""
return frozenset(function(i, j) for j in b for i in a)
def most_common_color(element: Element) -> Color:
"""Returns the most frequent color in a grid or object"""
colors = [c for r in element for c in r] if isinstance(element, tuple) else [c for c, _ in element]
return max(set(colors), key=colors.count)
def least_common_color(element: Element) -> Color:
"""Returns the least frequent color in a grid or object"""
colors = [c for r in element for c in r] if isinstance(element, tuple) else [c for c, _ in element]
return min(set(colors), key=colors.count)
# was height
def get_height(piece: Piece) -> Integer:
"""Returns the height of a grid or patch"""
if len(piece) == 0:
return 0
if isinstance(piece, tuple):
return len(piece)
return lowermost(piece) - uppermost(piece) + 1
# was width
def get_width(piece: Piece) -> Integer:
"""Returns the width of a grid or patch"""
if len(piece) == 0:
return 0
if isinstance(piece, tuple):
return len(piece[0])
return rightmost(piece) - leftmost(piece) + 1
# was shape
def get_shape(piece: Piece) -> IntegerTuple:
"""Returns (height, width) for a grid or patch"""
return (get_height(piece), get_width(piece))
def is_portrait(piece: Piece) -> Boolean:
"""Returns True iff height>width for a grid or patch"""
return get_height(piece) > get_width(piece)
def color_count(element: Element, color: Color) -> Integer:
"""Returns the number of cells of 'color'"""
if isinstance(element, tuple):
return sum(row.count(color) for row in element)
return sum(c == color for c, _ in element)
def color_filter(objs: Objects, color: Color) -> Objects:
"""Returns the objects filtered by 'color'"""
return frozenset(obj for obj in objs if next(iter(obj))[0] == color)
def as_indices(grid: Grid) -> Indices:
"""Returns the indices of all cells in 'grid'"""
return frozenset((i, j) for i in range(len(grid)) for j in range(len(grid[0])))
def size_filter(container: Container, size: Integer) -> FrozenSet:
"""Returns the objects filtered by 'size'"""
return frozenset(item for item in container if len(item) == size)
def of_color(grid: Grid, color: Color) -> Indices:
"""Returns the indices of all cells in 'grid' which are 'color'"""
return frozenset((i, j) for i, r in enumerate(grid) for j, c in enumerate(r) if c == color)
def upper_left_corner(patch: Patch) -> IntegerTuple:
"""Returns the index of the upper left corner"""
return tuple(map(min, zip(*to_indices(patch))))
def upper_right_corner(patch: Patch) -> IntegerTuple:
"""Returns the index of the upper right corner"""
return tuple(map(lambda ix: {0: min, 1: max}[ix[0]](ix[1]), enumerate(zip(*to_indices(patch)))))
def lower_left_corner(patch: Patch) -> IntegerTuple:
"""Returns the index of the lower left corner"""
return tuple(map(lambda ix: {0: max, 1: min}[ix[0]](ix[1]), enumerate(zip(*to_indices(patch)))))
def lower_right_corner(patch: Patch) -> IntegerTuple:
"""Returns the index of the lower right corner"""
return tuple(map(max, zip(*to_indices(patch))))
def crop(grid: Grid, start: IntegerTuple, dims: IntegerTuple) -> Grid:
""" subgrid specified by start and dimension """
return tuple(r[start[1]:start[1]+dims[1]] for r in grid[start[0]:start[0]+dims[0]])
def to_indices(patch: Patch) -> Indices:
"""Returns the indices of the cells within 'patch'"""
if len(patch) == 0:
return frozenset()
if isinstance(next(iter(patch))[1], tuple):
return frozenset(index for _, index in patch)
return patch
def recolor( color: Color, patch: Patch ) -> Object:
"""Returns 'patch' with each color set to 'color'"""
return frozenset((color, index) for index in to_indices(patch))
def shift_by_vector[T:Union[Patch,Objects]](patch: T, offset: IntegerTuple) -> T:
"""Moves 'patch' by the vector 'offset'"""
if len(patch) == 0:
return patch
di, dj = offset
if isinstance(next(iter(patch))[1], tuple):
return frozenset((color, (i + di, j + dj)) for color, (i, j) in patch)
return frozenset((i + di, j + dj) for i, j in patch)
#def normalize(patch: Patch) -> Patch:
def shift_to_origin[T:Patch](patch: T) -> T:
"""Moves upper left corner of 'patch' to the origin"""
if len(patch) == 0:
return patch
return shift_by_vector(patch, (-uppermost(patch), -leftmost(patch)))
def direct_neighbors(loc: IntegerTuple) -> Indices:
"""Returns indices that are directly adjacent to 'loc'"""
return frozenset({(loc[0] - 1, loc[1]), (loc[0] + 1, loc[1]), (loc[0], loc[1] - 1), (loc[0], loc[1] + 1)})
def diagonal_neighbors(loc: IntegerTuple) -> Indices:
"""Returns indices that are diagonally adjacent to 'loc'"""
return frozenset({(loc[0] - 1, loc[1] - 1), (loc[0] - 1, loc[1] + 1), (loc[0] + 1, loc[1] - 1), (loc[0] + 1, loc[1] + 1)})
def neighbors(loc: IntegerTuple) -> Indices:
"""Returns indices that are either directly of diagonally adjacent to 'loc'"""
return direct_neighbors(loc) | diagonal_neighbors(loc)
def as_objects(grid: Grid, each_object_single_color: Boolean, include_diagonal_neighbors: Boolean, discard_background: Boolean) -> Objects:
"""Converts 'grid' to a set of connected objects"""
bg = most_common_color(grid) if discard_background else None
objs = set()
occupied = set()
h, w = len(grid), len(grid[0])
unvisited = as_indices(grid)
neighbor_fun = neighbors if include_diagonal_neighbors else direct_neighbors
for loc in unvisited:
if loc in occupied:
continue
color = grid[loc[0]][loc[1]]
if color == bg:
continue
obj = {(color, loc)}
candidates = {loc}
while len(candidates) > 0:
neighborhood = set()
for candidate in candidates:
c = grid[candidate[0]][candidate[1]]
if (color == c) if each_object_single_color else (c != bg):
obj.add((c, candidate))
occupied.add(candidate)
neighborhood |= {
(i, j) for i, j in neighbor_fun(candidate) if 0 <= i < h and 0 <= j < w
}
candidates = neighborhood - occupied
objs.add(frozenset(obj))
return frozenset(objs)
def partition(grid: Grid) -> Objects:
"""Converts 'grid' to a set of objects by color"""
return frozenset(
frozenset(
(c, (i, j)) for i, r in enumerate(grid) for j, c in enumerate(r) if c == color
) for color in palette(grid)
)
def partition_only_foreground(grid: Grid) -> Objects:
"""Converts 'grid' to a set of objects by color, ignoring the background"""
return frozenset(
frozenset(
(c, (i, j)) for i, r in enumerate(grid) for j, c in enumerate(r) if c == color
) for color in palette(grid) - {most_common_color(grid)}
)
def uppermost(patch: Patch) -> Integer:
"""Returns the row index of uppermost occupied cell of 'patch'"""
return min(i for i, j in to_indices(patch))
def lowermost(patch: Patch) -> Integer:
"""Returns the row index of lowermost occupied cell of 'patch'"""
return max(i for i, j in to_indices(patch))
def leftmost(patch: Patch) -> Integer:
"""Returns the column index of leftmost occupied cell of 'patch'"""
return min(j for i, j in to_indices(patch))
def rightmost(patch: Patch) -> Integer:
"""Returns the column index of rightmost occupied cell of 'patch'"""
return max(j for i, j in to_indices(patch))
def is_square(piece: Piece) -> Boolean:
"""Returns True iff the piece forms a square"""
return len(piece) == len(piece[0]) if isinstance(piece, tuple) else get_height(piece) * get_width(piece) == len(piece) and get_height(piece) == get_width(piece)
def is_vertical_line(patch: Patch) -> Boolean:
"""Returns True iff the piece forms a vertical line"""
return get_height(patch) == len(patch) and get_width(patch) == 1
def is_horizontal_line(patch: Patch) -> Boolean:
"""Returns True iff the piece forms a horizontal line"""
return get_width(patch) == len(patch) and get_height(patch) == 1
def horizontal_matching(a: Patch, b: Patch) -> Boolean:
"""Returns True iff there exists a row for which both patches have cells"""
return len(set(i for i, j in to_indices(a)) & set(i for i, j in to_indices(b))) > 0
def vertical_matching(a: Patch, b: Patch) -> Boolean:
"""Returns True iff there exists a column for which both patches have cells"""
return len(set(j for i, j in to_indices(a)) & set(j for i, j in to_indices(b))) > 0
def manhattan_distance(a: Patch, b: Patch) -> Integer:
"""Returns the minimum manhattan distance between two patches"""
return min(abs(ai - bi) + abs(aj - bj) for ai, aj in to_indices(a) for bi, bj in to_indices(b))
def adjacent(a: Patch, b: Patch) -> Boolean:
"""Returns True iff two patches are adjacent"""
return manhattan_distance(a, b) == 1
def bordering(patch: Patch, grid: Grid) -> Boolean:
"""Returns True iff 'patch' is adjacent to a border of 'grid'"""
return uppermost(patch) == 0 or leftmost(patch) == 0 or lowermost(patch) == len(grid) - 1 or rightmost(patch) == len(grid[0]) - 1
def centerofmass(patch: Patch) -> IntegerTuple:
"""Returns the center of mass of 'patch'"""
return tuple(map(lambda x: sum(x) // len(patch), zip(*to_indices(patch))))
def palette(element: Element) -> ColorSet:
"""Returns the set of colors occurring in object or grid"""
if isinstance(element, tuple):
return frozenset({c for r in element for c in r}) # Grid
return frozenset({c for c, _ in element}) # Object
#) -> IntegerSet: #mdda : Seems wrong
def count_colors(element: Element) -> Integer:
"""Returns the number of colors occurring in object or grid"""
return len(palette(element))
# was color
def get_color(obj: Object) -> Color:
"""Returns the color of object"""
return next(iter(obj))[0]
def to_object(patch: Patch, grid: Grid) -> Object:
"""Returns the object given by extracting the locations in 'patch' from 'grid'"""
h, w = len(grid), len(grid[0])
return frozenset((grid[i][j], (i, j)) for i, j in to_indices(patch) if 0 <= i < h and 0 <= j < w)
def as_object(grid: Grid) -> Object:
"""Returns the 'grid' converted to an object"""
return frozenset((c, (i, j)) for i, r in enumerate(grid) for j, c in enumerate(r))
def rot90(grid: Grid) -> Grid:
"""Returns 'grid' rotated a quarter turn clockwise"""
return tuple(row for row in zip(*grid[::-1]))
def rot180(grid: Grid) -> Grid:
"""Returns 'grid' rotated a half turn"""
return tuple(tuple(row[::-1]) for row in grid[::-1])
def rot270(grid: Grid) -> Grid:
"""Returns 'grid' rotated a quarter turn anticlockwise"""
return tuple(tuple(row[::-1]) for row in zip(*grid[::-1]))[::-1]
#def horizontal_mirror(piece: Piece) -> Piece:
def horizontal_mirror[T: Piece](piece: T) -> T:
"""Returns 'piece' mirrored along horizontal axis"""
if isinstance(piece, tuple):
return piece[::-1]
d = upper_left_corner(piece)[0] + lower_right_corner(piece)[0]
if isinstance(next(iter(piece))[1], tuple):
return frozenset((c, (d - i, j)) for c, (i, j) in piece)
return frozenset((d - i, j) for i, j in piece)
#def vertical_mirror(piece: Piece) -> Piece:
def vertical_mirror[T: Piece](piece: T) -> T:
"""Returns 'piece' mirrored along vertical axis"""
if isinstance(piece, tuple):
return tuple(row[::-1] for row in piece)
d = upper_left_corner(piece)[1] + lower_right_corner(piece)[1]
if isinstance(next(iter(piece))[1], tuple):
return frozenset((c, (i, d - j)) for c, (i, j) in piece)
return frozenset((i, d - j) for i, j in piece)
#def diagonal_mirror(piece: Piece) -> Piece:
def diagonal_mirror[T: Piece](piece: T) -> T:
"""Returns 'piece' mirrored along diagonal"""
if isinstance(piece, tuple):
return tuple(zip(*piece))
a, b = upper_left_corner(piece)
if isinstance(next(iter(piece))[1], tuple):
return frozenset((v, (j - b + a, i - a + b)) for v, (i, j) in piece)
return frozenset((j - b + a, i - a + b) for i, j in piece)
#def counterdiagonal_mirror(piece: Piece) -> Piece:
def counterdiagonal_mirror[T: Piece](piece: T) -> T:
"""Returns 'piece' mirrored along counterdiagonal"""
if isinstance(piece, tuple):
return tuple(zip(*(r[::-1] for r in piece[::-1])))
return vertical_mirror(diagonal_mirror(vertical_mirror(piece)))
# Is there a better name for this?
# changed patch: Patch :: mdda
def fill(grid: Grid, color: Color, patch: Union[Patch,Objects,Tuple]) -> Grid:
"""Returns 'grid' with 'color' filled in at the indices in 'patch'"""
h, w = len(grid), len(grid[0])
grid_filled = list(list(row) for row in grid)
for i, j in to_indices(patch):
if 0 <= i < h and 0 <= j < w:
grid_filled[i][j] = color
return tuple(tuple(row) for row in grid_filled)
# was underfill
# changed patch: Patch :: mdda
def fill_background(grid: Grid, color: Color, patch: Piece) -> Grid:
"""Returns 'grid' with 'color' filled in at the indices in 'patch' over the background parts"""
h, w = len(grid), len(grid[0])
bg = most_common_color(grid)
g = list(list(r) for r in grid)
for i, j in to_indices(patch):
if 0 <= i < h and 0 <= j < w:
if g[i][j] == bg:
g[i][j] = color
return tuple(tuple(r) for r in g)
# was paint
# changed obj: Object :: mdda
def paint_onto_grid(grid: Grid, obj: Union[Object, Tuple, Indices]) -> Grid:
"""Returns 'grid' the object painted onto it"""
h, w = len(grid), len(grid[0])
grid_painted = list(list(row) for row in grid)
for color, (i, j) in obj:
if 0 <= i < h and 0 <= j < w:
grid_painted[i][j] = color
return tuple(tuple(row) for row in grid_painted)
# was underpaint
# changed obj: Object :: mdda
def paint_onto_grid_background(grid: Grid, obj: Union[Object, Tuple, Indices]) -> Grid:
"""Returns 'grid' the object painted onto only its background"""
h, w = len(grid), len(grid[0])
bg = most_common_color(grid)
g = list(list(r) for r in grid)
for color, (i, j) in obj:
if 0 <= i < h and 0 <= j < w:
if g[i][j] == bg:
g[i][j] = color
return tuple(tuple(r) for r in g)
def horizontal_upscale(grid: Grid, factor: Integer) -> Grid:
"""Returns 'grid' upscaled horizontally by 'factor'"""
g = tuple()
for row in grid:
r = tuple()
for value in row:
r = r + tuple(value for num in range(factor))
g = g + (r,)
return g
def vertical_upscale(grid: Grid, factor: Integer) -> Grid:
"""Returns 'grid' upscaled vertically by 'factor'"""
g = tuple()
for row in grid:
g = g + tuple(row for num in range(factor))
return g
def upscale[T: Union[Element,Patch]](element: T, factor: Integer) -> T:
"""Returns object or grid upscaled overall by 'factor'"""
if isinstance(element, tuple):
g = tuple()
for row in element:
upscaled_row = tuple()
for value in row:
upscaled_row = upscaled_row + tuple(value for num in range(factor))
g = g + tuple(upscaled_row for num in range(factor))
return g
else:
if len(element) == 0:
return frozenset()
di_inv, dj_inv = upper_left_corner(element)
di, dj = (-di_inv, -dj_inv)
normed_obj = shift_by_vector(element, (di, dj))
o = set()
for value, (i, j) in normed_obj:
for io in range(factor):
for jo in range(factor):
o.add((value, (i * factor + io, j * factor + jo)))
return shift_by_vector(frozenset(o), (di_inv, dj_inv))
def downscale(grid: Grid, factor: Integer) -> Grid:
"""Returns 'grid' downscaled overall by 'factor'"""
h, w = len(grid), len(grid[0])
g = tuple()
for i in range(h):
r = tuple()
for j in range(w):
if j % factor == 0:
r = r + (grid[i][j],)
g = g + (r, )
h = len(g)
dsg = tuple()
for i in range(h):
if i % factor == 0:
dsg = dsg + (g[i],)
return dsg
def horizontal_concat(a: Grid, b: Grid) -> Grid:
"""Returns a grid formed by concatenating the grids 'a' and 'b' horizontally"""
return tuple(i + j for i, j in zip(a, b))
def vertical_concat(a: Grid, b: Grid) -> Grid:
"""Returns a grid formed by concatenating the grids 'a' and 'b' vertically"""
return a + b
def smallest_subgrid_containing(patch: Patch, grid: Grid) -> Grid:
""" smallest subgrid containing object """
return crop(grid, upper_left_corner(patch), get_shape(patch))
def horizontal_split(grid: Grid, n: Integer) -> Tuple:
"""Returns 'n' smaller grids formed by splitting 'grid' horizontally"""
h, w = len(grid), len(grid[0]) // n
offset = len(grid[0]) % n != 0
return tuple(crop(grid, (0, w * i + i * offset), (h, w)) for i in range(n))
def vertical_split(grid: Grid, n: Integer) -> Tuple:
"""Returns 'n' smaller grids formed by splitting 'grid' vertically"""
h, w = len(grid) // n, len(grid[0])
offset = len(grid) % n != 0
return tuple(crop(grid, (h * i + i * offset, 0), (h, w)) for i in range(n))
def cellwise(a: Grid, b: Grid, fallback: Color) -> Grid:
"""Returns the grid resulting from copying the places where 'a' and 'b' match, but having the color 'fallback' where they do not"""
h, w = len(a), len(a[0])
resulting_grid = tuple()
for i in range(h):
row = tuple()
for j in range(w):
a_color = a[i][j]
color = a_color if a_color == b[i][j] else fallback
row = row + (color,)
resulting_grid = resulting_grid + (row, )
return resulting_grid
def replace(grid: Grid, replacee: Color, replacer: Color) -> Grid:
"""Returns 'grid' where the color 'replacee' is replaced by 'replacer'"""
return tuple(tuple(replacer if c == replacee else c for c in r) for r in grid)
def switch(grid: Grid, a: Color, b: Color) -> Grid:
"""Returns 'grid' where the colors 'a' and 'b' are switched"""
return tuple(tuple(c if (c != a and c != b) else {a: b, b: a}[c] for c in r) for r in grid)
def center(patch: Patch) -> IntegerTuple:
"""Returns the coordinates of the center of 'patch'"""
return (uppermost(patch) + get_height(patch) // 2, leftmost(patch) + get_width(patch) // 2)
def position(a: Patch, b: Patch) -> IntegerTuple:
"""Returns the relative position of patch 'b' to patch 'a' as (vertical, horizontal). Values are -1, 0, or 1, representing down/left, same, or up/right respectively"""
ia, ja = center(to_indices(a))
ib, jb = center(to_indices(b))
if ia == ib:
return (0, 1 if ja < jb else -1)
elif ja == jb:
return (1 if ia < ib else -1, 0)
elif ia < ib:
return (1, 1 if ja < jb else -1)
elif ia > ib:
return (-1, 1 if ja < jb else -1)
def color_at_location(grid: Grid, location: IntegerTuple) -> Color:
"""Returns the color found in 'grid' at 'location'"""
i, j = location
h, w = len(grid), len(grid[0])
if not (0 <= i < h and 0 <= j < w):
return None
return grid[location[0]][location[1]]
# was canvas
def create_grid(color: Color, dimensions: IntegerTuple) -> Grid:
"""Creates a grid filled with the specified 'color' and 'dimensions' (height, width)"""
return tuple(tuple(color for j in range(dimensions[1])) for i in range(dimensions[0]))
# was corners
def corner_indices(patch: Patch) -> Indices:
"""Returns the indices of the corners of a patch as a set"""
return frozenset({upper_left_corner(patch), upper_right_corner(patch), lower_left_corner(patch), lower_right_corner(patch)})
def line_between(a: IntegerTuple, b: IntegerTuple) -> Indices:
"""Returns the indices of cells forming a line between two points. Supports horizontal, vertical, and exactly diagonal lines. Returns an empty set if the line is not one of these types"""
ai, aj = a
bi, bj = b
si = min(ai, bi)
ei = max(ai, bi) + 1
sj = min(aj, bj)
ej = max(aj, bj) + 1
if ai == bi:
return frozenset((ai, j) for j in range(sj, ej))
elif aj == bj:
return frozenset((i, aj) for i in range(si, ei))
elif bi - ai == bj - aj:
return frozenset((i, j) for i, j in zip(range(si, ei), range(sj, ej)))
elif bi - ai == aj - bj:
return frozenset((i, j) for i, j in zip(range(si, ei), range(ej - 1, sj - 1, -1)))
return frozenset()
# was cover
def erase_patch(grid: Grid, patch: Patch) -> Grid:
"""Erases a patch from the grid by filling it with the most common color (background)"""
return fill(grid, most_common_color(grid), to_indices(patch))
# was trim
def trim_border(grid: Grid) -> Grid:
"""Removes the outer border (one cell wide) from the grid"""
return tuple(r[1:-1] for r in grid[1:-1])
# was move
def move_object(grid: Grid, obj: Object, offset: IntegerTuple) -> Grid:
"""Moves an object on the grid by the given offset (vertical, horizontal)"""
return paint_onto_grid(erase_patch(grid, obj), shift_by_vector(obj, offset))
def top_half(grid: Grid) -> Grid:
"""Returns the top half of the grid"""
return grid[:len(grid) // 2]
def bottom_half(grid: Grid) -> Grid:
"""Returns the bottom half of the grid"""
return grid[len(grid) // 2 + len(grid) % 2:]
def left_half(grid: Grid) -> Grid:
"""Returns the left half of the grid"""
return rot270(top_half(rot90(grid)))
def right_half(grid: Grid) -> Grid:
"""Returns the right half of the grid"""
return rot270(bottom_half(rot90(grid)))
# was vfrontier
def vertical_line(location: IntegerTuple) -> Indices:
""" vertical line through point """
return frozenset((i, location[1]) for i in range(30))
# was hfrontier
def horizontal_line(location: IntegerTuple) -> Indices:
""" horizontal line through point """
return frozenset((location[0], j) for j in range(30))
# was backdrop
def bounding_box_indices(patch: Patch) -> Indices:
"""Returns the indices inside the bounding box of a patch"""
if len(patch) == 0:
return frozenset({})
indices = to_indices(patch)
si, sj = upper_left_corner(indices)
ei, ej = lower_right_corner(patch)
return frozenset((i, j) for i in range(si, ei + 1) for j in range(sj, ej + 1))
# was delta
def bounding_box_delta(patch: Patch) -> Indices:
"""Returns the indices within the bounding box of a patch but *not* part of the patch itself"""
if len(patch) == 0:
return frozenset({})
return bounding_box_indices(patch) - to_indices(patch)
# was gravitate