Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
Created November 3, 2020 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapthedingbat/eb73a124f5fcf776cb477792f893babc to your computer and use it in GitHub Desktop.
Save zapthedingbat/eb73a124f5fcf776cb477792f893babc to your computer and use it in GitHub Desktop.
Pirates Engineering Challenge

Pirates

You are shipwrecked on a desert island with nothing but your own company and a solar powered laptop with the Node.js, Golang and Python3 or you prefered development environment installed. Without internet access you're driven, by boredom to scratch in the sand where you find a compass and a scroll left behind by some algorithmically minded pirates. The scroll has a set of instructions of how to find hidden treasure and a working satellite phone. You decide to follow them and escape the island so you can return home to the good weather and political stability of the island you live.

The only problem is that the scroll of instructions is very long and it would take ages to walk around the island and follow each instruction step by step. But wait! Can you use code to simplify the instructions to go straight to the end and get home in time for bake-off?

The Instructions

The scroll consists of an array of instructions of only a single word, either TURN or WALK followed by a space and a number. For TURN instructions the number is in degrees so 90 would be a right turn and 270 for left. WALK is followed by a number of steps forward.

Start facing north at the center of the island (by the obligatory palm tree). Write a function to process this list of instructions and return a new list of instructions that you can use to get to the end of the trail, how few can you do it in? As usual, points for instresting submissions including extreme brevity, verbosity, or performance and of course finding the treasure.

These are the instructions that are written on the scroll (which conveniently supports copy and paste).

[
"WALK 14",
"TURN 90",
"WALK 13",
"TURN 270",
"WALK 12",
"TURN 270",
"WALK 8",
"TURN 270",
"WALK 7",
"TURN 270",
"WALK 11",
"TURN 270",
"WALK 9",
"TURN 90",
"WALK 12",
"TURN 90",
"WALK 15",
"TURN 270",
"WALK 15",
"TURN 270",
"WALK 10",
"TURN 90",
"WALK 9",
"TURN 90",
"WALK 14",
"TURN 90",
"WALK 6",
"TURN 270",
"WALK 7",
"TURN 90",
"WALK 6",
"TURN 90",
"WALK 10",
"TURN 270",
"WALK 15",
"TURN 270",
"WALK 15",
"TURN 270",
"WALK 14",
"TURN 90",
"WALK 6",
"TURN 90",
"WALK 6",
"TURN 270",
"WALK 6",
"TURN 270",
"WALK 10",
"TURN 270",
"WALK 15",
"TURN 270",
"WALK 13",
"TURN 270",
"WALK 13",
"TURN 270",
"WALK 6",
"TURN 90",
"WALK 13",
"TURN 270",
"WALK 10",
"TURN 90",
"WALK 14",
"TURN 270",
"WALK 12",
"TURN 270",
"WALK 10",
"TURN 270",
"WALK 6",
"TURN 270",
"WALK 8",
"TURN 90",
"WALK 11",
"TURN 90",
"WALK 10",
"TURN 270",
"WALK 14",
"TURN 90",
"WALK 15",
"TURN 270",
"WALK 12",
"TURN 90",
"WALK 14",
"TURN 90",
"WALK 12",
"TURN 90",
"WALK 12",
"TURN 90",
"WALK 9",
"TURN 90",
"WALK 14",
"TURN 90",
"WALK 7",
"TURN 270",
"WALK 10",
"TURN 90",
"WALK 13",
"TURN 270",
"WALK 8",
"TURN 270",
"WALK 14",
"TURN 270",
"WALK 9"
]

Answer

  • "TURN 63.43"
  • "WALK 26.83"

Map

function solution(instructions) {
let x = 0;
let y = 0;
let direction = 0;
for (const instruction of instructions) {
let [word, value] = instruction.split(" ");
value = Number.parseFloat(value);
if (word === "WALK") {
let theta = direction * (Math.PI / 180);
x += value * Math.cos(theta);
y += value * Math.sin(theta);
} else {
direction += value;
}
}
return [
"TURN " + Math.atan2(y, x) / (Math.PI / 180),
"WALK " + Math.hypot(x, y)
];
}
module.exports = exports = solution;
b=>{with(Math){x=y=a=0,c=PI/180;for(i of b)v=+i.slice(5),i>"W"?(x+=v*cos(r=a*c),y+=v*sin(r)):a+=v;return['TURN '+atan2(y,x)/c,'WALK '+hypot(x,y)]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment