import sys # https://www.coding-dojo-silesia.pl/golf WIDTH = 8 HEIGHT = 8 class Person: def __init__(self, x, y, s, q): self.x = int(x) self.y = int(y) self.s = s self.q = int(q) self.n = (self.x << 2) ^ 7 * (self.y << 3) ^ 5 self.i = 1 def point_free(x, y, people): for person in people: if x == person.x and y == person.y: return False return True def check_neighborhood(person, people): for otherPerson in people: 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': return True return False def next_iteration(people): for person in people: cure(person); if person.s == 'S': person.i += 1 person.n = gen_n(person.n) [new_x, new_y] = gen_new_cords(person) if point_free(new_x, new_y, people) and person.q == 0: person.x = new_x person.y = new_y if person.s == 'H' and check_neighborhood(person, people): person.s = 'S' return people def cure(person): if person.s == 'S' and person.i > 13: person.s = 'R' person.i = 1 def gen_n(n): n ^= n << 13 n ^= n >> 17 n ^= n << 5 return n % (1 << 30) def gen_new_cords(person): x = person.x y = person.y check = person.n % 4 if check == 0: x += 1 if check == 1: x -= 1 if check == 2: y += 1 if check == 3: y -= 1 if x >= WIDTH: x -= WIDTH elif x < 0: x += WIDTH if y >= HEIGHT: y -= HEIGHT elif y < 0: y += HEIGHT return [x, y] def get_people(peopleString): peopleUnparsed = peopleString.split(",") people = [] for item in peopleUnparsed: person = Person(item[0], item[1], item[2], item[3]) people.append(person) return people def print_field(people): for i in range(0, HEIGHT): string = '' for j in range(0, WIDTH): person = [x for x in people if x.x == j and x.y == i] if len(person) == 0 : string += '.' else: string += person[0].s print(string) def main(): iterations = int(sys.argv[1]) peopleUnparsed = sys.argv[2] people = get_people(peopleUnparsed) for i in range(0, iterations): people = next_iteration(people) print_field(people) main()