Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:24
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 ghaiklor/f4f423e5adcb716969b1 to your computer and use it in GitHub Desktop.
Save ghaiklor/f4f423e5adcb716969b1 to your computer and use it in GitHub Desktop.
Advent of Code (Day 9 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const DIRECTION_REGEX = /(\w+) to (\w+) = (\d+)/;
// Takes input and builds map of all possible distances between two points
const buildDistanceMap = input => {
const map = new Map();
input.forEach(direction => {
const parsed = direction.match(DIRECTION_REGEX);
map.set(`${parsed[1]} -> ${parsed[2]}`, +parsed[3]);
map.set(`${parsed[2]} -> ${parsed[1]}`, +parsed[3]);
});
return map;
};
// Takes input and builds unique set of all places that need to be visited
const buildPlacesSet = input => {
const places = new Set();
input.forEach(direction => {
const parsed = direction.match(DIRECTION_REGEX);
places.add(parsed[1]).add(parsed[2]);
});
return places;
};
// Takes array of items and builds array with all possible permutations
const permute = input => {
const array = Array.from(input);
const permute = (res, item, key, arr) => {
return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(perm => [item].concat(perm)) || item);
};
return array.reduce(permute, []);
};
const distances = buildDistanceMap(INPUT);
const places = buildPlacesSet(INPUT);
const allPossibleRoutes = permute(places);
// All possible distances
const allPossibleDistances = allPossibleRoutes.reduce((acc, route) => {
let total = 0;
for (let i = 0; i < route.length; i++) {
if (route[i + 1] === undefined) break;
total += distances.get(`${route[i]} -> ${route[i + 1]}`);
}
return acc.concat([total]);
}, []);
// Just changed min() method to max()
const result = Math.max.apply(Math, allPossibleDistances);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment