Skip to content

Instantly share code, notes, and snippets.

@FBosler
Last active October 16, 2022 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FBosler/be0e667ed8437160572979d38fb99fb4 to your computer and use it in GitHub Desktop.
Save FBosler/be0e667ed8437160572979d38fb99fb4 to your computer and use it in GitHub Desktop.
foobar_dodge_the_lasers
from decimal import Decimal, localcontext
def solution(s):
n = Decimal(s)
with localcontext() as ctx:
ctx.prec = 102
r = Decimal(2).sqrt()
s = Decimal(2) + Decimal(2).sqrt()
def solve(n):
if n == 0:
return 0
Brn = int(r * n)
Brns = int(Decimal(Brn) / s)
return (Brn * (Brn + 1)) / 2 - solve(Brns) - Brns * (Brns + 1)
return str(int(solve(n)))
@ajmeese7
Copy link

This can be improved slightly by replacing Decimal(2).sqrt() in the s variable assignment with r, since they are exactly the same. I tried it against the test cases to make sure and the results were identical.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment