12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- local InstructionSets = require(script.InstructionSets)
- local WireWorld = require(script.WireWorld)
- local Directions = {
- ["U"] = Vector2.new( 0, 1),
- ["D"] = Vector2.new( 0,-1),
- ["L"] = Vector2.new(-1, 0),
- ["R"] = Vector2.new( 1, 0),
- }
- local function dist(p1,p2)
- local xDist = math.abs(p2.X - p1.X)
- local yDist = math.abs(p2.Y - p1.Y)
-
- return xDist + yDist
- end
- local function plot(instructionSet)
- local newWorld = WireWorld.new()
- local currentPosition = Vector2.new(0, 0)
- local collisions = {}
- local numSteps = 0
-
- for _, instruction in ipairs(instructionSet) do
- local direction, steps = instruction.direction, instruction.steps
- local displacement = Directions[direction] * steps
- local displacedPosition = currentPosition
- -- move x axis
- local xDist = displacement.X
- local xStepDirection = xDist / math.abs(xDist)
- for dx = xStepDirection, xDist, xStepDirection do
- local displacedPosition = currentPosition + Vector2.new(dx,0)
- numSteps = numSteps + 1
- local alreadyHere = newWorld:getTraversed(displacedPosition) ~= nil
- if not alreadyHere then
- newWorld:setTraversed(displacedPosition, numSteps)
- end
- end
-
- -- move y axis
- local yDist = displacement.Y
- local yStepDirection = yDist / math.abs(yDist)
- for dy = yStepDirection, yDist, yStepDirection do
- local displacedPosition = currentPosition + Vector2.new(0,dy)
- numSteps = numSteps + 1
- local alreadyHere = newWorld:getTraversed(displacedPosition) ~= nil
- if not alreadyHere then
- newWorld:setTraversed(displacedPosition, numSteps)
- end
- end
-
- currentPosition = currentPosition + displacement
- end
-
- return newWorld
- end
- local world1 = plot(InstructionSets[1])
- local world2 = plot(InstructionSets[2])
- local collisions = WireWorld.getCollisions(world1, world2)
- local center = Vector2.new(0, 0)
- local closestDist = math.huge
- for _, contactInfo in ipairs(collisions) do
- local pointDist = dist(center, contactInfo.pos)
- if pointDist < closestDist then
- closestDist = pointDist
- end
- end
- local lowestCombinedSteps = math.huge
- for _, contactInfo in ipairs(collisions) do
- local combinedSteps = contactInfo.combinedSteps
- if combinedSteps < lowestCombinedSteps then
- lowestCombinedSteps = combinedSteps
- end
- end
- print("Closest intersection:")
- print("\t"..closestDist)
- print("Lowest combined steps:")
- print("\t"..lowestCombinedSteps)
|