Skip to content

Commit

Permalink
feature: All in a Single Night (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotacke committed Nov 6, 2018
1 parent 6b91aaa commit 833f4f4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
10 changes: 10 additions & 0 deletions day-09-all-in-a-single-night/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ The shortest of these is `London -> Dublin -> Belfast = 605`, and so the answer

What is the distance of the shortest route?

## Part Two

The next year, just to show off, Santa decides to take the route with the **longest distance** instead.

He can still start and end at any two (different) locations he wants, and he still must visit each location exactly once.

For example, given the distances above, the longest route would be `982` via (for example) `Dublin -> London -> Belfast`.

What is the distance of the longest route?

## References
- https://adventofcode.com/2015/day/9
73 changes: 73 additions & 0 deletions day-09-all-in-a-single-night/shortest-distance2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const getPermutations = (cities) => {
const permutations = [];

const permute = (array, permutation = []) => {
if (!array.length) {
permutations.push(permutation);

return;
}

for (let i = 0; i < array.length; i++) {
const rest = [...array];
const city = rest.splice(i, 1)[0];

permute(rest, [...permutation, city]);
}
};

permute(cities, []);

return permutations;
};

const shortestDistance = (input) => {
const cities = new Set();

const connections = input
.split('\n')
.map((distance) => {
const parts = distance
.trim()
.match(/(\w+) to (\w+) = (\d+)/);

return {
a: parts[1],
b: parts[2],
distance: parseInt(parts[3]),
};
});

connections.forEach((connection) => {
cities.add(connection.a);
cities.add(connection.b);
});

const permutations = getPermutations([...cities])
.map((permutation) => {
const distance = permutation.reduce((previousValue, currentValue, currentIndex, array) => {
const nextValue = array[currentIndex + 1];

if (nextValue) {
const connection = connections
.find((x) =>
(x.a === currentValue && x.b === nextValue) ||
(x.a === nextValue && x.b === currentValue)
);

return previousValue + connection.distance;
}

return previousValue;
}, 0);

return {
distance,
route: permutation,
};
});

return Math.max(...permutations.map((x) => x.distance));
};

module.exports = shortestDistance;
14 changes: 14 additions & 0 deletions day-09-all-in-a-single-night/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');

const distance = require('./shortest-distance');
const distance2 = require('./shortest-distance2');

describe('Day 9: All in a Single Night', () => {
it('should calculate shortest distance', () => {
Expand All @@ -13,4 +14,17 @@ describe('Day 9: All in a Single Night', () => {

assert.strictEqual(shortest, 605);
});

describe('Part Two', () => {
it('should calculate longest distance', () => {
const distances =
`London to Dublin = 464
London to Belfast = 518
Dublin to Belfast = 141`;

const shortest = distance2(distances);

assert.strictEqual(shortest, 982);
});
});
});

0 comments on commit 833f4f4

Please sign in to comment.