123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- const WIDTH = 8;
- const HEIGHT = 8;
- // https://www.coding-dojo-silesia.pl/golf
- function point_free(x, y, people) {
- const pLength = people.length;
- for (let i = 0; i < pLength; i++) {
- const person = people[i];
- if (x === person.x && y === person.y)
- return false;
- }
- return true;
- }
- function check_neighborhood(person, people) {
- const pLength = people.length;
- for (let i = 0; i < pLength; i++) {
- const otherPerson = people[i];
- if ((otherPerson.x >= person.x-1 && otherPerson.x <= person.x+1) && (otherPerson.y >= person.y-1 && otherPerson.y <= person.y+1) && otherPerson.s === 'S') {
- return true;
- }
- }
- return false;
- }
- function next_iteration(people) {
- people.forEach(function(person, _) { // iterate in a sequence
- cure(person);
- if (person.s === 'S') person.i++;
- person.n = gen_n(person.n);
- [new_x, new_y] = gen_new_cords(person);
- if (point_free(new_x, new_y, people) && person.q === 0) {
- person.x = new_x;
- person.y = new_y;
- }
- if (person.s === 'H' && check_neighborhood(person, people)) {
- // https://en.wikipedia.org/wiki/Moore_neighborhood
- person.s = 'S';
- }
- });
- return people;
- }
- function cure(person) {
- if (person.s === 'S' && person.i > 13) {
- person.s = 'R';
- person.i = 1;
- }
- }
- function gen_n(n) {
- n ^= n << BigInt(13);
- n ^= n >> BigInt(17);
- n ^= n << BigInt(5);
- return n % BigInt(1 << 30);
- }
- function gen_new_cords(person) {
- x = person.x;
- y = person.y;
- switch(person.n % BigInt(4)) {
- case BigInt(0): x++; break;
- case BigInt(1): x--; break;
- case BigInt(2): y++; break;
- case BigInt(3): y--; break;
- }
- // wrap points, like in pacman game
- if (x >= WIDTH) {
- x -= WIDTH;
- } else if (x < 0) {
- x += WIDTH;
- }
- if (y >= HEIGHT) {
- y -= HEIGHT;
- } else if (y < 0) {
- y += HEIGHT;
- }
- return [x, y];
- }
- function get_people(peopleString) {
- peopleUnparsed = peopleString.split(",");
- let people = [];
- peopleUnparsed.forEach(function(item, _) {
- let person = {};
- person.x = Number(item[0]);
- person.y = Number(item[1]);
- person.s = item[2];
- person.q = Number(item[3]);
- person.n = BigInt((person.x << 2) ^ 7 * (person.y << 3) ^ 5);
- person.i = 1;
- people.push(person);
- });
- return people;
- }
- function print_field(P) {
- for (let i = 0; i < HEIGHT; i++) {
- let string = '';
- for (let j = 0; j < WIDTH; j++) {
- const person = P.find(p => p.x === j && p.y === i);
- if (person === undefined)
- string += '.';
- else
- string += person.s;
- }
- console.log(string);
- }
- }
- function main() {
- const iterations = process.argv[2];
- const peopleUnparsed = process.argv[3];
- let people = get_people(peopleUnparsed);
- for (let i = 0; i < iterations; i++) {
- people = next_iteration(people);
- }
- print_field(people);
- }
- main();
|