Skip to content

Instantly share code, notes, and snippets.

@jamarju
Last active April 9, 2018 22:17
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 jamarju/51971d245591f76401444248820c9682 to your computer and use it in GitHub Desktop.
Save jamarju/51971d245591f76401444248820c9682 to your computer and use it in GitHub Desktop.
#codejam Qualification Round 2018, Cubic UFO (problem #4) JUDGE
#!/usr/bin/env python2
import sys
from math import acos, sqrt, pi
import numpy
from scipy.spatial import ConvexHull
class Vec:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def mod(self):
return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def theta(self, other):
return acos(self.x * other.x + self.y * other.y + self.z * other.z)
def equal(a, b):
return abs(a - b) < 0.000001
def judge(A, sides):
for v in sides:
if not equal(v.mod(), 0.5): return "Modulus not 0.5!"
if not equal(sides[0].theta(sides[1]), pi/2): return "0/1 not perpendicular!"
if not equal(sides[1].theta(sides[2]), pi/2): return "1/2 not perpendicular!"
if not equal(sides[2].theta(sides[0]), pi/2): return "2/0 not perpendicular!"
s2v = [
( -1, -1, -1 ), ( 1, -1, -1 ), ( -1, 1, -1 ), ( 1, 1, -1 ),
( -1, -1, 1 ), ( 1, -1, 1 ), ( -1, 1, 1 ), ( 1, 1, 1 )
]
verts = []
for i in xrange(8):
verts.append(Vec(
sides[0].x * s2v[i][0] + sides[1].x * s2v[i][1] + sides[2].x * s2v[i][2],
sides[0].y * s2v[i][0] + sides[1].y * s2v[i][1] + sides[2].y * s2v[i][2],
sides[0].z * s2v[i][0] + sides[1].z * s2v[i][1] + sides[2].z * s2v[i][2],
))
points = numpy.array( [ [v.x, v.z] for v in verts ] )
hull = ConvexHull(points)
a = hull.volume
if not equal(A, a): return "area {} != {}".format(A, a)
return "OK"
def main(argv):
if len(argv) != 3:
sys.stderr.write(argv[0] + " test.txt out.txt")
exit(1)
with open(argv[1]) as f:
T = int(f.readline())
AA = [ float(A.strip()) for A in f.readlines() ]
with open(argv[2]) as f:
for t in xrange(T):
f.readline()
sides = [ Vec(* ( float(c) for c in f.readline().split()) ) for _ in xrange(3) ]
print("Case #{}: {}".format(t + 1, judge(AA[t], sides)))
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment