summaryrefslogtreecommitdiff
path: root/102.py
blob: a2108f603c528f58ad5c24109f46e8dc18195666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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