Skip to content

Instantly share code, notes, and snippets.

@LunaticNeko
Created March 1, 2022 14:22
Show Gist options
  • Save LunaticNeko/aa228cc6d644fb7b65c0f08a048c2df2 to your computer and use it in GitHub Desktop.
Save LunaticNeko/aa228cc6d644fb7b65c0f08a048c2df2 to your computer and use it in GitHub Desktop.
BitBurner - parentheses validation contract task
# When your computer is fast enough, you can rely on luck and hackjob.
# This is NOT valid for actual programming tests IRL. You will need a
# more serious effort for that, including picking parentheses to remove
# more deterministically, or have some better heuristics than "lol d2".
from random import randint
'''
Validates the string.
'''
def validate(s):
depth = 0
for c in s:
if c == '(':
depth += 1
elif c == ')':
depth -= 1
if depth < 0:
return False
return True if depth == 0 else False
## MAIN ##
count = 0
S = "((a))()))a)())"
sols = []
mlen = 0
for count in range(20000):
s = ""
for c in S:
if ((c == '(' or c == ')') and randint(0,1) == 1) or c == 'a':
s += c
if validate(s):
sols.append((len(s), s))
if len(s) > mlen:
mlen = len(s)
for l,s in sorted(list(set(sols))):
if l == mlen:
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment