-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathp90.qky
168 lines (148 loc) · 4.57 KB
/
p90.qky
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
( Algorithm: enumerate all possible cubes as bit vectors, filter down to only
cubes that even have a hope of succeeding (note: I initially missed that each
cube face has a *different* digit, so there's a lazy hack to throw out cubes
with duplicates as well), then test all combinations and count the number of
solutions. )
( Utility functions )
[ -1 peek ] is peer ( [ ... c ] --> c )
[ split nip ] is after ( a n --> a[n...] )
( Ranged "for" loop )
[ stack ] is for.min
[ stack ] is for.action
[ for.min share i^ + ] is index ( --> n )
[ ]'[ for.action put
over for.min put
swap -
times [ for.action share do ]
for.min release
for.action release ] is for ( min limit --> )
( Transform { item index --> item' } each item from a nest into a new nest )
[ stack ] is map.action
[ ]'[ map.action put
dup size times [ dup i^ peek i^ map.action share do swap i^ poke ]
map.action release ] is map ( [ a ... z ] --> [ a' ... z' ] )
( For each { item index --> } )
[ stack ] is each.action
[ ]'[ each.action put
dup size times [ dup i^ peek i^ each.action share do ]
drop
each.action release ] is each ( a --> )
( Reduce { a b -> c } )
[ stack ] is reduce.action
[ ]'[ reduce.action put
over size times [
over i^ peek
reduce.action share do
]
nip
reduce.action release ] is reduce ( [ a ... z ] init --> f(z, ... f(a, init)) )
( Oops, missed the part about no duplicate digits -- lazy fix here )
[ 1 rot << & ] is contains ( item digits --> b )
[ 0 swap 10 times [
i^ over contains if [ swap 1 + swap ]
]
drop
6 =
] is distinct ( cube --> b )
( Test if cube could possibly lead to a solution )
[ over contains swap ] is _contains ( cube digit --> b cube )
[ unrot or swap ] is _or ( a1 a2 cube --> a1||a2 cube )
[ 0 _contains 1 _contains _or
0 _contains 4 _contains _or
0 _contains 9 _contains 6 _contains _or _or
1 _contains 6 _contains 9 _contains _or _or
2 _contains 5 _contains _or
3 _contains 6 _contains 9 _contains _or _or
4 _contains 9 _contains 6 _contains _or _or
6 _contains 9 _contains 4 _contains _or _or
8 _contains 1 _contains _or
drop 8 times and ] is possible ( cube --> b )
( Test if two cubes can create all squares < 100 )
[ stack ] is test.left
[ test.left share ] is t1
[ stack ] is test.right
[ test.right share ] is t2
[ test.right put
test.left put
0 t1 contains 1 t2 contains and
1 t1 contains 0 t2 contains and
or
0 t1 contains 4 t2 contains and
4 t1 contains 0 t2 contains and
or
0 t1 contains 9 t2 contains 6 t2 contains or and
9 t1 contains 6 t1 contains or 0 t2 contains and
or
1 t1 contains 6 t2 contains 9 t2 contains or and
6 t1 contains 9 t1 contains or 1 t2 contains and
or
2 t1 contains 5 t2 contains and
5 t1 contains 2 t2 contains and
or
3 t1 contains 6 t2 contains 9 t2 contains or and
6 t1 contains 9 t1 contains or 3 t2 contains and
or
4 t1 contains 9 t2 contains 6 t2 contains or and
9 t1 contains 6 t1 contains or 4 t2 contains and
or
6 t1 contains 9 t1 contains or 4 t2 contains and
4 t1 contains 6 t2 contains 9 t2 contains or and
or
8 t1 contains 1 t2 contains and
1 t1 contains 8 t2 contains and
or
8 times and
test.left release
test.right release
] is test ( a1 a2 --> b )
( Map digits to bit vectors (only care about presence and not count) )
[ map [ drop 1 swap << ]
0 reduce | ] is bitify ( [ a ... z ] --> a...z )
( Stash combinations in a global... )
[ stack ] is combos
[] combos put
[ nested combos take swap join combos put ] is combinations.add ( a --> )
[ combos share ] is combinations ( --> a )
( Stash count in a global )
[ stack ] is counts
0 counts put
[ counts take ] is count.get
[ count.get 1 + counts put ] is increment
( Recursively enumerate all digit combinations and map to a bit vector )
( faces --> )
forward is enumerate
[
dup size 6 < iff [
dup size iff [ dup peer ] else 0 10 for [
dup index join enumerate
]
drop
] else [
( Map to bit vector, ensure faces are distinct, and only consider cubes
that could lead to a solution )
bitify
dup distinct if [
dup possible if [
dup combinations.add
]
]
drop
]
] resolves enumerate
( Initialize nest of combinations )
say "Enumerating potentially-valid cubes..." cr
[] enumerate
say "Enumerated" sp combinations size echo sp say "potentially valid cubes." cr
( Enumerate pairs )
[ stack ] is left.stack
[ left.stack share ] is left
say "Processing..." sp
combinations each [
dup echo sp
swap left.stack put
combinations swap after each [
drop left test if increment
]
left.stack release
]
cr cr say "Solution:" sp count.get echo cr