Created
March 8, 2014 12:26
-
-
Save plexus/9429778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Call like : node saving_time.js 9:00 | |
/* | |
Idea taken from http://codegolf.com/saving-time | |
Take an input representing a time, like 22:15 or 04:23, and draw a clock indicating | |
hours and minutes, like below. | |
o | |
o o | |
o o | |
h o | |
o o | |
m o | |
o | |
*/ | |
function main() { | |
var time = process.argv[2]; | |
var minHrs = parseTime(time); | |
console.log( | |
buildClock( | |
mapHours(minHrs[0]), | |
mapMinutes(minHrs[1]) | |
) | |
); | |
} | |
function parseTime(time) { | |
var parts = time.split(":"); | |
return [parseInt(parts[0]), parseInt(parts[1])]; | |
}; | |
// mapMinutes 0-60 -> 0-11 | |
function mapMinutes(minutes) { | |
return Math.floor(minutes/5); | |
} | |
// mapHours 0-24 -> 0-11 | |
function mapHours(hours) { | |
return hours % 12; | |
} | |
function buildClock(hourPos, minPos) { | |
var template = | |
" A\n" + | |
" L B\n\n"+ | |
" K C\n\n" + | |
"J D\n\n" + | |
" I E\n\n" + | |
" H F\n" + | |
" G"; | |
for (var i = 0; i <= 11; i++) { | |
if (i == hourPos) { | |
template = template.replace(String.fromCharCode(65 + i), 'h'); | |
} else if (i == minPos) { | |
template = template.replace(String.fromCharCode(65 + i), 'm'); | |
} else { | |
template = template.replace(String.fromCharCode(65 + i), 'o'); | |
} | |
} | |
return template; | |
} | |
var tests = [ | |
function parseTime_should_parse_time () { | |
var result = parseTime("22:10"); | |
assertArrayEqual(result, [22, 10]); | |
}, | |
function mapMinutes_should_map_correctly() { | |
var mappings = [ | |
[10, 2], | |
[32, 6], | |
[0, 0], | |
[59, 11] | |
]; | |
mappings.forEach(function(mapping) { | |
assertEqual(mapping[1], mapMinutes(mapping[0])); | |
}); | |
}, | |
function mapHours_should_map_correctly() { | |
var mappings = [ | |
[1, 1], | |
[2, 2], | |
[12, 0], | |
[13, 1], | |
[15, 3], | |
[23, 11], | |
[0, 0] | |
]; | |
mappings.forEach(function(mapping) { | |
assertEqual(mapping[1], mapHours(mapping[0])); | |
}); | |
}, | |
function test_buildClock() { | |
assertEqual( | |
buildClock(9, 7), | |
" o\n"+ | |
" o o\n" + | |
"\n" + | |
" o o\n" + | |
"\n" + | |
"h o\n" + | |
"\n" + | |
" o o\n" + | |
"\n" + | |
" m o\n" + | |
" o" | |
); | |
assertEqual( | |
buildClock(2, 7), | |
" o\n"+ | |
" o o\n" + | |
"\n" + | |
" o h\n" + | |
"\n" + | |
"o o\n" + | |
"\n" + | |
" o o\n" + | |
"\n" + | |
" m o\n" + | |
" o" | |
); | |
} | |
]; | |
// An improvised, poor man's testing framework | |
function assert(bool, message) { | |
if(!bool) { | |
console.log("FAIL: " + message); | |
process.exit(1); | |
} | |
} | |
function assertEqual(real, expected, message) { | |
assert(real === expected, "Expected " + expected + ", got " + real + ' ' + (typeof(message) == 'undefined' ? '' : message)); | |
} | |
function assertArrayEqual(real, expected) { | |
assertEqual(real.length, expected.length, "Arrays are not the same length"); | |
for(var i = 0; i < real.length ; i++) { | |
if (Array.isArray(real[i]) && Array.isArray(expected[i])) { | |
assertArrayEqual(real[i], expected[i]); | |
} else { | |
assertEqual(real[i], expected[i], "difference at position " + i); | |
} | |
} | |
} | |
// Run the tests, this will exit if any test fails | |
tests.forEach(function(test) { test(); }); | |
// Run the program | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment