From 95341b61b030c9e1290f3b326cb7ec584f543aea Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sun, 31 Aug 2014 20:21:45 +0200 Subject: moved files to higher directory after split to new repository --- 102.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 102.py (limited to '102.py') diff --git a/102.py b/102.py new file mode 100644 index 0000000..a2108f6 --- /dev/null +++ b/102.py @@ -0,0 +1,43 @@ + +# check if r*(AC) + s*(AB) == P + +def point_in_triangle((x_a, y_a), (x_b, y_b), (x_c, y_c), (x_p, y_p)): + t = x_c - x_a + u = x_b - x_a + v = y_c - y_a + w = y_b - y_a + + # Cramer's rule + detA = t*w - u*v + detR = x_p*w - u*y_p + detS = t*y_p - x_p*v + + if detA == 0: + print "Not solvable" + + r = detR / float(detA) + s = detS / float(detA) + + return 0 <= r <= 1 and 0 <= s <= 1 and r + s < 1 + + +count = 0 + +f = open('102.txt', 'r') +for line in f: + line = line.rstrip('\r\n') + coords = [ int(x) for x in line.split(',') ] + + # move one point to origin, adjust other points + (a1, a2) = (coords[0], coords[1]) + A = (0, 0) + B = (coords[2]-a1, coords[3]-a2) + C = (coords[4]-a1, coords[5]-a2) + P = (-a1, -a2) + + if point_in_triangle(A, B, C, P): + count += 1 +f.close() + +print count + -- cgit v1.2.3