-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossmintChallengeRyanKelly.py
145 lines (99 loc) · 3.88 KB
/
crossmintChallengeRyanKelly.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
from typing import Tuple as T, Dict, List as L
Z = int
def phase1():
for row, column in phase1XCoords():
createPolyanets(row, column)
def phase1XCoords():
from itertools import chain
downSlope = zip(range(2, 9), range(2, 9))
upSlope = zip(range(8, 1, -1), range(2, 9))
return list(set(chain(downSlope, upSlope)))
def createPolyanets(row: Z, column: Z) -> None:
createEntity("polyanets", row, column)
def createSoloons(row: Z, column: Z, color: str) -> None:
createEntity("soloons", row, column, trait={"color": validateSoloonsColor(color)})
def validateSoloonsColor(color: str) -> str:
c = color.lower()
if c != "blue" and c != "red" and c != "purple" and c != "white":
raise Exception(f"Invalid soloons color '{c}'?")
return c
def createComeths(row: Z, column: Z, direction: str) -> None:
createEntity("comeths", row, column, trait={"direction": validateComethsDirection(direction)})
def validateComethsDirection(direction: str) -> str:
d = direction.lower()
if d != "up" and d != "down" and d != "right" and d != "left":
raise Exception(f"Invalid comeths direction '{d}'?")
return d
def createEntity(entityType: str, row: Z, column: Z, trait: Dict = {}) -> None:
import requests
url = createUrl(entityType)
postData = {"candidateId": candidateId(), "row": row, "column": column}
postData.update(trait)
sleepFor429()
r = requests.post(url, data=postData)
print(f"{url=} {postData=} {r=}")
def sleepFor429():
from time import sleep
sleep(5)
def createUrl(entityType: str) -> str:
entityType = validateEntityType(entityType)
return baseUrl() + entityType
def validateEntityType(entityType: str) -> str:
e = entityType.lower()
if e != "polyanets" and e != "soloons" and e != "comeths":
raise Exception(f"Invalid entity type '{e}'?")
return e
def baseUrl() -> str:
return "https://challenge.crossmint.io/api/"
def candidateId() -> str:
return "1c4f30de-ed58-4112-903e-18f11ac6bd64"
def parseGoalMap() -> L[L[str]]:
import json
r = goalMap()
goal = json.loads(r.text)["goal"]
return goal
def goalMap() -> str:
import requests
url = goalMapUrl()
r = requests.get(url)
print(f"goalMap {url=} {r=}\n")
print(r.text)
return r
def goalMapUrl() -> str:
return baseUrl() + "map/" + candidateId() + "/goal"
def phase2():
goal = parseGoalMap()
for row, rowEntities in enumerate(goal):
for column, entity in enumerate(rowEntities):
e = entity.lower()
print(f"{row=} {column=} {e=}")
if e == "space":
continue
elif e == "polyanet":
createPolyanets(row, column)
elif "cometh" in e:
direction = parseGoalComethDirection(e)
createComeths(row, column, direction)
elif "soloon" in e:
color = parseGoalSoloonColor(e)
createSoloons(row, column, color)
else:
raise Exception(f"Unhandled goal entity '{e}'? {row=} {column=}")
def parseGoalComethDirection(e) -> str:
direction, cometh = e.split("_")
if cometh != "cometh":
raise Exception(f"Something looks wrong with cometh goal str '{e}'? Doesn't have 'cometh' as second word?")
if direction not in ["up", "down", "left", "right"]:
raise Exception(f"Cometh goal str '{e}' doesn't have expected direction?")
return direction
def parseGoalSoloonColor(e) -> str:
color, soloon = e.split("_")
if soloon != "soloon":
raise Exception(f"Something looks wrong with soloon goal str '{e}'? Doesn't have 'soloon' as second word?")
if color not in ["blue", "red", "purple", "white"]:
raise Exception(f"Soloon goal str '{e}' doesn't have expected color?")
return color
if __name__ == "__main__":
# phase1()
# goalMap()
phase2()