Advertisement
nein_yards

Untitled

Sep 30th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. class Person:
  2. x = 0
  3. y = 0
  4. flu_remaining = 0
  5. prev_infected = False
  6.  
  7. def __init__(self, x, y, flu_remaining=0, prev_infected=False):
  8. self.x = x
  9. self.y = y
  10. self.flu_remaining = flu_remaining
  11. self.prev_infected = prev_infected
  12.  
  13. def distance(self, person):
  14. return ((self.x - person.x)**2 + (self.y - person.y)**2) ** 0.5
  15.  
  16.  
  17. def flu_epidemic(mat, duration, flu_distance, flu_time, incubation_time):
  18. h = len(mat)
  19. w = len(mat[0])
  20. persons = []
  21. num_sick = 0
  22.  
  23. for i in range(h):
  24. for j in range(w):
  25. if mat[i][j] == 'P':
  26. persons.append(Person(j, i))
  27. elif mat[i][j] == 'I':
  28. persons.append(Person(j, i, flu_time, True))
  29. num_sick += 1
  30. n_persons = len(persons)
  31.  
  32. adj_mat = [[False for c in range(n_persons)]
  33. for r in range(n_persons)]
  34. for i in range(n_persons-1):
  35. for j in range(i+1, n_persons):
  36. if persons[i].distance(persons[j]) <= flu_distance:
  37. adj_mat[i][j] = True
  38. adj_mat[j][i] = True
  39.  
  40. for t in range(0, duration+1, incubation_time):
  41. for i in range(n_persons):
  42. if persons[i].flu_remaining == 0:
  43. continue
  44. persons[i].flu_remaining -= incubation_time
  45. if persons[i].flu_remaining == 0:
  46. num_sick -= 1
  47. continue
  48. for j in range(n_persons):
  49. if i == j:
  50. continue
  51. if adj_mat[i][j]:
  52. if not persons[j].prev_infected:
  53. persons[j].flu_remaining = flu_time
  54. persons[j].prev_infected = True
  55. num_sick += 1
  56.  
  57. return num_sick
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement