diff --git a/day 14/day_14_mark.js b/day 14/day_14_mark.js new file mode 100644 index 0000000000000000000000000000000000000000..5a2b95d0facc38c25b73954b3609ad4156d8f772 --- /dev/null +++ b/day 14/day_14_mark.js @@ -0,0 +1,58 @@ +range = x => [...Array(x).keys()] + +robots = input.split("\n").map(line => line.match(/p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)/).slice(1).map(Number)) + +n = 103 +m = 101 + +mod = (x,y) => ((x % y) + y) % y + +robotPosition = (robot, t) => [mod(robot[0] + t * robot[2], m), mod(robot[1] + t * robot[3], n)] + +positions = robots.map(robot => robotPosition(robot, 100)) + +print = () => { + map = range(n).map(x => Array(m).fill(0)) + positions.map(position => map[position[1]][position[0]]++) + console.log(map) + console.log(map.map(x => x.join('')).join('\n')) +} + +inQuad1 = position => position[0] < (m-1)/2 && position[1] < (n-1)/2 +inQuad2 = position => position[0] > (m-1)/2 && position[1] < (n-1)/2 +inQuad3 = position => position[0] > (m-1)/2 && position[1] > (n-1)/2 +inQuad4 = position => position[0] < (m-1)/2 && position[1] > (n-1)/2 + +output1 = [inQuad1,inQuad2,inQuad3,inQuad4].reduce((safetyFactor, inQuadX) => safetyFactor * positions.reduce((total, position) => total + inQuadX(position), 0), 1) + +//// + +print = () => { + map = range(n).map(x => Array(m).fill(0)) + positions.map(position => map[position[1]][position[0]]++) + console.log(map.map(x => x.map(y => y ? 'X' : ' ').join('')).join('\n')) +} + +add = (x,y) => [x[0] + y[0], x[1] + y[1]] + +neighbourhood = x => [[-1,1],[1,1],[1,-1],[-1,-1]].map(y => add(x,y)) + +isValid = x => (x[0] >= 0 && x[0] < m && x[1] >= 0 && x[1] < n) + +neighbour_score = (positions) => positions.reduce((total, position) => total + neighbourhood(position).filter(isValid).filter(x => map[position[1]][position[0]] > 0).length, 0) + +maxScore = 0 +maxScoreId = -1 +range(10000).map(i => { + positions = robots.map(robot => robotPosition(robot, i)) + map = range(n).map(x => Array(m).fill(0)) + positions.map(position => map[position[1]][position[0]]++) + score = neighbour_score(positions) + if(score > maxScore){ + maxScoreId = i + maxScore = score + console.log(i, score) + } +}) + +output2 = maxScoreId