Skip to content

Instantly share code, notes, and snippets.

@sujaldev
Last active September 3, 2024 07:04
Show Gist options
  • Save sujaldev/2dcea3a63b12a4fa4b09464154fbc143 to your computer and use it in GitHub Desktop.
Save sujaldev/2dcea3a63b12a4fa4b09464154fbc143 to your computer and use it in GitHub Desktop.
Solution to the Longshot Systems hiring challenge.
from base64 import b64encode, b64decode
from playwright.sync_api import sync_playwright
def exec_instruction(instruction):
instruction = b64decode(instruction)[1:-1]
if instruction == b"END":
page.evaluate(f"ws.send('{b64encode(bytes(f'{sum(registers)}', 'utf8')).decode()}')")
return
elif instruction[0] not in (65, 77, 83): # ADD, MOV, STORE
return
match instruction.split():
case [b"ADD", value, r1, r2]:
r1, r2 = int(r1[1:]), int(r2[1:])
registers[r2] = registers[r1] + int(value)
case [b"MOV", r1, r2]:
r1, r2 = int(r1[1:]), int(r2[1:])
registers[r2] = registers[r1]
case [b"STORE", value, r1]:
r1 = int(r1[1:])
registers[r1] = int(value)
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
# Challenge 1
page.goto("https://challenge.longshotsystems.co.uk/go")
container = page.locator("xpath=/html/body/div/div[1]/div")
answer = "".join([e.strip() for e in container.all_text_contents()])
page.evaluate("document.querySelector('.answer-panel').style.visibility = 'visible';")
page.get_by_label("Answer").fill(answer)
page.get_by_label("name").fill("Your Name")
page.get_by_role("button").click()
# Challenge 2
registers = [0] * 16
page.on("websocket", lambda ws: ws.on("framereceived", lambda msg: exec_instruction(msg)))
page.wait_for_timeout(5000)
input("Press enter to exit")
browser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment