Skip to content

Instantly share code, notes, and snippets.

@nsriram
Last active December 2, 2017 05:28
Show Gist options
  • Save nsriram/96800557f6e6394cc1be4a6e8fbff362 to your computer and use it in GitHub Desktop.
Save nsriram/96800557f6e6394cc1be4a6e8fbff362 to your computer and use it in GitHub Desktop.
/**
* Steps To Run
* 1. Install NodeJS
* 2. Dowload Gist
* 3. Run from terminal node clockProblem.js
*/
const CLOCK_MAX = 12;
const CLOCK_HALF = 6;
const add = (x, y) => x + y;
const addHours = (currentHour, index) =>
(currentHour + index) > CLOCK_MAX ? (currentHour + index) % CLOCK_MAX : (currentHour + index);
const halfRange = (startHour) => {
if (startHour > CLOCK_MAX || startHour < 1) {
throw new Error('invalid hour');
}
let range = [];
for (x = 0; x < CLOCK_HALF; x++) {
range = [].concat(range, addHours(startHour, x));
}
return {
range,
start: range[0],
end: range[range.length - 1],
};
}
let clockSum = 0;
for (hour = 1; hour <= CLOCK_MAX; hour++) {
clockSum += hour;
}
const expectedSum = clockSum / 2;
for (hour = 1; hour <= CLOCK_HALF; hour++) {
const halfOne = halfRange(hour);
if (halfOne.range.reduce(add, 0) === expectedSum) {
const halfTwo = halfRange(addHours(hour, CLOCK_HALF));
console.log(`Draw line between ${halfOne.start}-${halfTwo.end} and ${halfTwo.start}-${halfOne.end}`);
console.log(`Sum is ${expectedSum}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment