Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Created January 3, 2018 03:29
Show Gist options
  • Select an option

  • Save devNoiseConsulting/ff0d0f6857ebfc4645fe995945a92325 to your computer and use it in GitHub Desktop.

Select an option

Save devNoiseConsulting/ff0d0f6857ebfc4645fe995945a92325 to your computer and use it in GitHub Desktop.
Sporifica Virus - Advent of Code - 20171222
let virusGrid = require('./virusGrid');
let checkGridSize = function(grid, x, y) {
let width = grid[0].length;
if (x < 0) {
grid.unshift(new Array(width).fill(0).map(v => '.'));
x = 0;
}
if (x == grid.length) {
grid.push(new Array(width).fill(0).map(v => '.'));
}
if (y < 0) {
grid.forEach(v => {
v.unshift('.');
});
y = 0;
}
if (y == grid[0].length) {
grid.forEach(v => {
v.push('.');
});
}
return [grid, x, y];
};
let virusWorker_1 = function(grid, iterations) {
let x = (y = Math.floor(grid.length / 2));
let directions = [
[-1, 0], // north
[0, 1], // east
[1, 0], // south
[0, -1] // west
];
let getNewDirection = function(direction, change) {
return (directions.length + direction + change) % directions.length;
};
let currentDirection = 0;
let left = -1;
let right = 1;
let infections = 0;
for (let i = 0; i < iterations; i++) {
//console.log('x, y, direction', x, y, currentDirection);
let state = grid[x][y];
if (state == '.') {
grid[x][y] = '#';
infections++;
currentDirection = getNewDirection(currentDirection, left);
x += directions[currentDirection][0];
y += directions[currentDirection][1];
} else {
grid[x][y] = '.';
currentDirection = getNewDirection(currentDirection, right);
x += directions[currentDirection][0];
y += directions[currentDirection][1];
}
[grid, x, y] = checkGridSize(grid, x, y);
//console.log(grid.map(v => v.join('')).join('\n'));
}
return infections;
};
let virusWorker_2 = function(grid, iterations) {
let x = (y = Math.floor(grid.length / 2));
let directions = [
[-1, 0], // north
[0, 1], // east
[1, 0], // south
[0, -1] // west
];
let getNewDirection = function(direction, change) {
return (directions.length + direction + change) % directions.length;
};
let currentDirection = 0;
let left = -1;
let right = 1;
let reverse = 2;
let infections = 0;
for (let i = 0; i < iterations; i++) {
//console.log('x, y, direction', x, y, currentDirection);
let state = grid[x][y];
switch (state) {
case '.':
grid[x][y] = 'W';
currentDirection = getNewDirection(currentDirection, left);
x += directions[currentDirection][0];
y += directions[currentDirection][1];
break;
case 'W':
grid[x][y] = '#';
infections++;
x += directions[currentDirection][0];
y += directions[currentDirection][1];
break;
case '#':
grid[x][y] = 'F';
currentDirection = getNewDirection(currentDirection, right);
x += directions[currentDirection][0];
y += directions[currentDirection][1];
break;
case 'F':
grid[x][y] = '.';
currentDirection = getNewDirection(currentDirection, reverse);
x += directions[currentDirection][0];
y += directions[currentDirection][1];
break;
}
[grid, x, y] = checkGridSize(grid, x, y);
//console.log(grid.map(v => v.join('')).join('\n'));
}
return infections;
};
let virusWorker = virusWorker_2;
let iterations = 10000000;
let result = virusWorker(virusGrid, iterations);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment