-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem169.gspl
98 lines (88 loc) · 1.36 KB
/
problem169.gspl
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
"Half of largest power of 2 that divides into input N"&
{
1 :i
{ $ ;i 4*% 0= } {
& ;i 2 * :i
} #
& & ;i
} :powtwo
"Integer division-by-2 algorithm (built-in division is floating)"&
{
{ } {
$ ;powtwo! $ :tmp 2*- ;tmp ^ ;divtwo! +
} {
"0 / 2 == 0, we're done"&
} ¿
} :divtwo
"Get first element from array"&
{
1_ :v
{
{ ;v 0 < } {
& ; :v
} {
&
} ¿
} ^ f
;v
} :first
"Get second element from array"&
{
1_ :v
{
{ ;v 1_ = } {
& 2_ :v
} {
& { ;v 2_ = } {
& ; :v
} {
&
} ¿
} ¿
} ^ f
;v
} :second
"Uncached fusc function"&
{
{ $1> } {
& { $2% } {
"Odd case"&
& 1 - ;divtwo! $ 1 + ;cachedfusc! ^ ;cachedfusc! +
} {
"Even case"&
& ;divtwo! ;cachedfusc!
} ¿
} {
&
} ¿
} :fusc
"Cache is stored as a list of [key value] pairs"&
[] :cache
"Find value in cache, return -1 if not found"&
{
:n
1_ :cachedvalue
{
; :curr
{ ;curr ;first! ;n = } {
& ;curr ;second! :cachedvalue
} {
&
} ¿
} ;cache f
;cachedvalue
} :findcached
"Append to cache; takes two arguments"&
{
:v :k
;cache [] ;k + ;v + 2 / + :cache
} :appendcache
"Cached fusc function"&
{
{ $ ;findcached! $ 1_ > } {
&^&
} {
&& $ ;fusc! $ :tmp ;appendcache! ;tmp
} ¿
} :cachedfusc
10 25 ×& 1+ ;cachedfusc! . 10 ,