Skip to content
Snippets Groups Projects
Commit f7002993 authored by Mark Jeeninga's avatar Mark Jeeninga
Browse files

Day 14 Mark

parent 8ffb73da
No related branches found
No related tags found
No related merge requests found
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment