-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve2.a68
89 lines (77 loc) · 2.42 KB
/
solve2.a68
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
PR include "read.a68" PR
CO NOTE: the traversal style was changed a bit; unrelated to the changed needed to handle part 2
(which was simply to perform a deep instead of a shallow copy CO
PROC traverse = (REF NUMBER num, PROC(INT,REF NUMBER,REF NUMBER)VOID visit)VOID:
BEGIN
PROC dive into = (INT depth, REF NUMBER num, parent)VOID:
BEGIN
visit(depth, num, parent);
CASE num IN
(REF PAIR sub): BEGIN
REF NUMBER tmp = right OF sub;
dive into(depth+1, left OF sub, num);
dive into(depth+1, tmp, num)
END
ESAC
END;
dive into(0,num,NIL)
END;
PRIO COPY = 1;
OP COPY = (NUMBER x)NUMBER:
CASE x IN
(REF PAIR pair): make pair(COPY left OF pair, COPY right OF pair)
OUT x
ESAC;
PROC snailfish reduce = (NUMBER n)NUMBER:
BEGIN
NUMBER data := COPY n;
REF NUMBER last regular;
INT add to next regular;
INT exploded;
PROC explode = (INT depth, REF NUMBER cell, parent)VOID:
CASE cell IN
(INT n): IF depth >= 5 AND exploded < 2 THEN
CASE exploded +:= 1 IN
IF REF NUMBER prev cell = last regular; prev cell ISNT NIL THEN
prev cell := n + (prev cell | (INT n): n)
FI,
(add to next regular := n; parent := 0)
ESAC
ELSE
last regular := cell;
cell := n + add to next regular;
IF exploded /= 0 THEN just start again FI
FI
ESAC;
PROC split = (INT depth, REF NUMBER cell, parent)VOID:
(cell | (INT n): IF n >= 10 THEN cell := make pair (n%2, (n+1)%2); just start again FI);
just start again:
CO dump(data); print(new line); CO
exploded := 0;
last regular := NIL;
add to next regular := 0;
traverse(data, explode);
traverse(data, split);
data
END;
CO note: because "INT" is directly convertible into a number, we can't overload "OP +" easily CO
PROC add = (NUMBER x,y)NUMBER:
snailfish reduce(LOC PAIR:=(x,y));
OP ABS = (NUMBER x)INT:
CASE x IN
(INT n): n,
(REF PAIR pair): 3*ABS left OF pair + 2*ABS right OF pair
ESAC;
INT max magnitude := -1000;
NUMBER x1, x2;
FOR i TO UPB homework DO FOR j TO UPB homework DO
IF i /= j THEN
INT this = ABS add(homework[i], homework[j]);
(this > max magnitude | max magnitude := this;
x1 := homework[i];
x2 := homework[j])
FI
OD OD;
dump(x1); print(new line);
dump(x2); print(new line);
print((max magnitude, new line))