diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2014-08-31 20:21:45 +0200 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2014-08-31 20:35:09 +0200 |
| commit | 95341b61b030c9e1290f3b326cb7ec584f543aea (patch) | |
| tree | 852386fa04d32eb859bca11c0eff7b5ef9e50f00 /102.py | |
| parent | 571164d977f91925c4c76a292f74f5f93d09ae23 (diff) | |
Diffstat (limited to '102.py')
| -rw-r--r-- | 102.py | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -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 + |
