Skip to content

Instantly share code, notes, and snippets.

@PoignardAzur
Created August 23, 2023 13:59
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 PoignardAzur/ddc65535aa6a47e5e6cfefb1987bc50b to your computer and use it in GitHub Desktop.
Save PoignardAzur/ddc65535aa6a47e5e6cfefb1987bc50b to your computer and use it in GitHub Desktop.
Small program written for the 2023 Master Dev France hackaton
/*
# Instructions
You've come a long way since you opened your first pizzeria! Your recipes were so successful that they were snapped up all over town, and you soon had to open other establishments to serve the ever-growing demand.
To optimize deliveries and reduce the number of kilometers covered by your delivery drivers, you want to set up an intelligent order allocation system. You need to assign each order to the nearest pizzeria.
What will be the total distance covered by your delivery drivers to deliver all the pizzas?
We assume that the city streets form a grid with integer coordinates, so the distance between two points will be calculated using the Manhattan distance. All orders are delivered individually, and each deliveryman returns to his original pizzeria after each delivery.
Remember that the Manhattan distance between (x, y) and (x', y') is |x - x'| + |y - y'|.
# Data
## Input
Line 1: two integers N and M, respectively the number of pizzerias and the number of orders to be processed.
N next lines: two integers x and y corresponding to the coordinates of a pizzeria on the grid.
M following lines: two integers x and y corresponding to the coordinates of an order on the grid.
There will be a maximum of 100 pizzerias and 100 orders. The coordinates are between 0 and 1,000,000.
## Output
The total distance traveled by your delivery drivers to deliver the orders.
*/
// Example input; actual input was given to the program by the hackaton
const input = `
3 4
12 15
28 14
55 49
16 18
50 22
29 11
5 44
`.trim().split('\n');
ContestResponse()
function ContestResponse() {
const [pizzeriasCount, _ordersCount] = input[0].split(' ').map(Number);
const pizzerias = input.slice(1, pizzeriasCount + 1).map(pizzeria => pizzeria.split(' ').map(Number) as [number, number]);
const orders = input.slice(pizzeriasCount + 1).map(order => order.split(' ').map(Number) as [number, number]);
// Error messages were ignored by the validator but still displayed, useful for debugging.
console.error(pizzerias);
console.error(orders);
let totalDistance = 0;
orders.forEach(order => {
let minDistance = Infinity;
pizzerias.forEach(pizzeria => {
const distance = Math.abs(order[0] - pizzeria[0]) + Math.abs(order[1] - pizzeria[1]);
if (distance < minDistance) {
minDistance = distance;
}
});
totalDistance += minDistance;
});
// The answer had to be written to the console
console.log(totalDistance * 2);
}
@PoignardAzur
Copy link
Author

Quick notes:

I never actually participated to the Master Dev France hackaton (I tried, but there were no more spots this year). Fortunately the challenges are publicly archived.

Going through previous challenges was a pretty good learning exercise:

  • To familiarize myself with JS again (I hadn't used it in a while).
  • To learn TypeScript.
  • To learn Deno.js; in particular, to learn how to use the debugger with VsCode.
  • To learn how to use Github Copilot. That alone was worth the time invested: it's an amazing productivity tool.

Instructions were taken from one of the 2023 challenges, and translated to English.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment