Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const problem = {
- aix: {
- simiane: 11,
- marignane: 24,
- vitrolles: 41,
- bouc_bel_air: 26,
- les_milles: 13,
- },
- simiane: {
- aix: 11,
- marignane: 17,
- vitrolles: 27,
- bouc_bel_air: 42,
- les_milles: 28,
- },
- marignane: {
- aix: 24,
- simiane: 17,
- vitrolles: 32,
- bouc_bel_air: 51,
- les_milles: 18,
- },
- vitrolles: {
- aix: 41,
- simiane: 27,
- marignane: 32,
- bouc_bel_air: 35,
- les_milles: 32,
- },
- bouc_bel_air: {
- aix: 26,
- simiane: 42,
- marignane: 31,
- vitrolles: 51,
- les_milles: 17,
- },
- les_milles: {
- aix: 13,
- simiane: 28,
- marignane: 28,
- vitrolles: 18,
- bouc_bel_air: 17,
- },
- }
- const processed = [];
- const takeTheLowest = (city) => {
- const citiesChildren = Object.keys(city);
- let lowest;
- citiesChildren.forEach(cityChild => {
- if (lowest) {
- lowest = city[cityChild] < lowest ? city[cityChild] : lowest
- } else {
- lowest = problem[cityChild]
- }
- })
- return lowest;
- }
- let lowest;
- const createRoad = (start, end) => {
- const roads = [
- problem[start]
- ];
- let total = 0;
- while (roads[roads.length - 1] !== problem[end]) {
- const value = takeTheLowest(roads[roads.length - 1]);
- roads.push(value);
- total += roads[roads.length - 1][value];
- lowest = value;
- }
- const cities = []
- roads.forEach(road => {
- switch (road) {
- case problem.aix:
- cities.push('Aix');
- break;
- case problem.bouc_bel_air:
- cities.push('Bouc Bel Air');
- break;
- case problem.les_milles:
- cities.push('Aix les Milles');
- break;
- case problem.marignane:
- cities.push('Marignane');
- break;
- case problem.simiane:
- cities.push('Simiane');
- break;
- case problem.vitrolles:
- cities.push('Vitrolles');
- break;
- default:
- break;
- }
- })
- return {road, total}
- }
- console.log(createRoad('aix', 'bouc_bel_air'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement