epidemic.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import sys
  2. # https://www.coding-dojo-silesia.pl/golf
  3. WIDTH = 8
  4. HEIGHT = 8
  5. class Person:
  6. def __init__(self, x, y, s, q):
  7. self.x = int(x)
  8. self.y = int(y)
  9. self.s = s
  10. self.q = int(q)
  11. self.n = (self.x << 2) ^ 7 * (self.y << 3) ^ 5
  12. self.i = 1
  13. def point_free(x, y, people):
  14. for person in people:
  15. if x == person.x and y == person.y:
  16. return False
  17. return True
  18. def check_neighborhood(person, people):
  19. for otherPerson in people:
  20. if (otherPerson.x >= person.x-1 and otherPerson.x <= person.x+1) and (otherPerson.y >= person.y-1 and otherPerson.y <= person.y+1) and otherPerson.s == 'S':
  21. return True
  22. return False
  23. def next_iteration(people):
  24. for person in people:
  25. cure(person);
  26. if person.s == 'S':
  27. person.i += 1
  28. person.n = gen_n(person.n)
  29. [new_x, new_y] = gen_new_cords(person)
  30. if point_free(new_x, new_y, people) and person.q == 0:
  31. person.x = new_x
  32. person.y = new_y
  33. if person.s == 'H' and check_neighborhood(person, people):
  34. person.s = 'S'
  35. return people
  36. def cure(person):
  37. if person.s == 'S' and person.i > 13:
  38. person.s = 'R'
  39. person.i = 1
  40. def gen_n(n):
  41. n ^= n << 13
  42. n ^= n >> 17
  43. n ^= n << 5
  44. return n % (1 << 30)
  45. def gen_new_cords(person):
  46. x = person.x
  47. y = person.y
  48. check = person.n % 4
  49. if check == 0:
  50. x += 1
  51. if check == 1:
  52. x -= 1
  53. if check == 2:
  54. y += 1
  55. if check == 3:
  56. y -= 1
  57. if x >= WIDTH:
  58. x -= WIDTH
  59. elif x < 0:
  60. x += WIDTH
  61. if y >= HEIGHT:
  62. y -= HEIGHT
  63. elif y < 0:
  64. y += HEIGHT
  65. return [x, y]
  66. def get_people(peopleString):
  67. peopleUnparsed = peopleString.split(",")
  68. people = []
  69. for item in peopleUnparsed:
  70. person = Person(item[0], item[1], item[2], item[3])
  71. people.append(person)
  72. return people
  73. def print_field(people):
  74. for i in range(0, HEIGHT):
  75. string = ''
  76. for j in range(0, WIDTH):
  77. person = [x for x in people if x.x == j and x.y == i]
  78. if len(person) == 0 :
  79. string += '.'
  80. else:
  81. string += person[0].s
  82. print(string)
  83. def main():
  84. iterations = int(sys.argv[1])
  85. peopleUnparsed = sys.argv[2]
  86. people = get_people(peopleUnparsed)
  87. for i in range(0, iterations):
  88. people = next_iteration(people)
  89. print_field(people)
  90. main()