forked from ellisk42/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
towerPrimitives.py
81 lines (70 loc) · 2.28 KB
/
towerPrimitives.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
from program import *
from arithmeticPrimitives import *
from logicalPrimitives import *
from functools import reduce
#def _concatenate(x): return lambda y: x + y
def _left(d):
return lambda k: lambda hand: k(hand - d)
def _right(d):
return lambda k: lambda hand: k(hand + d)
def _loop(n):
def f(start, stop, body, hand):
if start >= stop: return hand,[]
hand, thisIteration = body(start)(hand)
hand, laterIterations = f(start + 1, stop, body, hand)
return hand, thisIteration + laterIterations
return lambda b: lambda h: f(0,n,b,h)
def _embed(body):
def f(k):
def g(hand):
_, bodyActions = body(hand)
hand, laterActions = k(hand)
return hand, bodyActions + laterActions
return g
return f
class TowerContinuation(object):
def __init__(self, x, w, h):
self.x = x
self.w = w
self.h = h
def __call__(self, k):
def f(hand):
thisAction = [(self.x + hand,self.w,self.h)]
hand, rest = k(hand)
return hand, thisAction + rest
return f
# name, dimensions
blocks = { # "1x1": (1.,1.),
# "2x1": (2.,1.),
# "1x2": (1.,2.),
"3x1": (3., 1.),
"1x3": (1., 3.),
# "4x1": (4.,1.),
# "1x4": (1.,4.)
}
epsilon = 0.05
# Ensures axis aligned blocks
def xOffset(w, h):
assert w == int(w)
w = int(w)
if w % 2 == 1:
return 0.5
return 0.
def _range(n):
if n < 100: return range(n)
raise ValueError()
def _fold(l): return lambda x0: lambda f: reduce(
lambda a, x: f(x)(a), l[::-1], x0)
ttower = baseType("tower")
primitives = [
Primitive("left", arrow(tint, ttower, ttower), _left),
Primitive("right", arrow(tint, ttower, ttower), _right),
Primitive("tower_loop", arrow(tint, arrow(tint, ttower), ttower), _loop),
Primitive("tower_embed", arrow(ttower, ttower, ttower), _embed),
] + [Primitive(name, arrow(ttower,ttower), TowerContinuation(xOffset(w, h), w - 2*epsilon, h - epsilon))
for name, (w, h) in blocks.items()] + [
addition, subtraction
] + \
[Primitive(str(j), tint, j) for j in list(range(1,3)) + list(range(-2,0,-1)) ]
def executeTower(p):
return p.evaluate([])(lambda s: (s,[]))(0)[1]