Advertisement
danchaofan

Euler #102

Dec 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. def check(points):
  2.     tempstr, locations, positive, negative, valid = "", [], False, False, False
  3.     for x in str(points):
  4.         if x == ",":
  5.             locations.append(int(tempstr))
  6.             tempstr = ""
  7.             continue
  8.         tempstr += x
  9.     locations.append(int(tempstr))
  10.     lefttoright = sorted(([locations[0], locations[1]], [locations[2], locations[3]], [locations[4], locations[5]]))
  11.     print(lefttoright)
  12.     for x in range(3):
  13.         if lefttoright[x][0] > 0:
  14.             positive = True
  15.         if lefttoright[x][0] < 0:
  16.             negative = True
  17.         if positive and negative:
  18.             valid = True
  19.     if not valid:
  20.         return False
  21.     positive, negative = False, False
  22.     for x in range(3):
  23.         if lefttoright[x][1] > 0:
  24.             positive = True
  25.         if lefttoright[x][1] < 0:
  26.             negative = True
  27.         if positive and negative:
  28.             valid = True
  29.     if not valid:
  30.         return False
  31.     slope = (lefttoright[0][1] - lefttoright[2][1]) / (lefttoright[0][0] - lefttoright[2][0])
  32.     yintercept1 = lefttoright[0][1] + slope*abs(lefttoright[0][0])
  33.     print(slope, yintercept1)
  34.     if 0 < lefttoright[1][0]:
  35.         slope2 = (lefttoright[0][1] - lefttoright[1][1]) / (lefttoright[0][0] - lefttoright[1][0])
  36.         yintercept2 = lefttoright[0][1] + slope2*abs(lefttoright[0][0])
  37.         print(slope2, yintercept2)
  38.     elif 0 > lefttoright[1][0]:
  39.         slope2 = (lefttoright[1][1] - lefttoright[2][1]) / (lefttoright[1][0] - lefttoright[2][0])
  40.         yintercept2 = lefttoright[1][1] + slope2*abs(lefttoright[1][0])
  41.         print(slope2, yintercept2)
  42.     if yintercept2 < 0 < yintercept1 or yintercept1 < 0 < yintercept2:
  43.         return True
  44.     return False
  45.  
  46. answer = 0
  47. for a in open("triangles.txt"):
  48.     if check(a.rstrip()):
  49.         answer += 1
  50. print(answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement