Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Fill the below function
- * You'll be scored on your function's character count of the line(s) between the brackets
- * Bragging rights for lowest run time of the line(s) between the brackets
- * You cannot use any existing library that solves the heart of the challenge
- * You cannot read from your own source code
- * All code must appear between the braces of your main function
- * Emitting errors/warnings not permitted
- * You cannot alter the function signature in any productive way
- * There may be hidden tests to be provided at scoring time, so check your assumptions
- * You may submit more than one function
- */
- /**
- * Meta-Primes
- *
- * While 25 is not prime, the sum of the digits of 25 is 7, which is prime. We'll call this "meta-prime".
- * Find the sum of meta-prime numbers less than N.
- *
- * The list of meta-primes under 30 is
- * 2,3,5,7,11,12,14,16,20,21,23,25,29
- * The sum of meta-primes below 30 is
- * 2+3+5+7+11+12+14+16+20+21+23+25+29 = 188
- *
- * Given N, return the sum of meta-primes less than N
- *
- * 1 is not considered a prime number
- *
- * @param int N
- * @return int[]
- */
- let Solutions = {
- codegolf103_fake: function f(N) {
- return expect;
- },
- _codegolf103_tyler1a: function f(N) {
- // Write a prime checker
- P=n=>{if(n==1)return 0;for(i=2;i<n;i++)if(n%i<1)return 0;return n}
- // Sum the meta-primes
- for(r=0;--N;)r+=P([...''+N].reduce((a,b)=>+a+ +b,0))?N:0
- return r
- },
- // print(`${N} -> ${s}`)
- xcodegolf103_tyler1b: function f(N) {
- P=n=>{for(i=2;i<n;i++)if(n%i<1)return 0;return n}
- for(r=0;--N;)r+=P([...''+N].reduce((a,b)=>+a+ +b,0))>1?N:0
- return r
- },
- xcodegolf103_tyler1c: function f(N) {
- P=n=>{for(i=2;i<n;i++)if(n%i<1)return 0;return n}
- for(r=0;--N;){for(s=0,n=N;n;n=Math.floor(n/10))s+=n%10;r+=P(s)>1?N:0}
- return r
- },
- xcodegolf103_tyler1c2: function f(N) {
- P=n=>[2,3,5,7,11,13,17,19,23,29,31,37,41,43].includes(n)?2:1
- for(r=0;--N;){for(s=0,n=N;n;n=Math.floor(n/10))s+=n%10;r+=P(s)>1?N:0}
- return r
- },
- xcodegolf103_tyler1d: function f(N) {
- for(r=0;--N;r+=s>1?N:0)
- for(s=[...''+N].reduce((a,b)=>+a+ +b),i=1;++i<s;)if(s%i<1)s=1
- return r
- },
- codegolf103_tyler1e: function f(N) {
- for(r=0;--N;r+=s>1?N:0)for(s=[...''+N].reduce((a,b)=>+a+ +b),i=1;++i<s;)s=s%i?s:1
- return r
- },
- codegolf103_tyler1g: function f(N) {
- for(r=0;--N;r+=s>1?N:0)for(s=[...''+N].reduce((a,b)=>a- -b),i=1;++i<s;)s=s%i?s:1
- return r
- },
- codegolf103_tyler1g_map_coop: function f(N) {
- for(r=0;--N;r+=s>1?N:0)for(s=0,[...''+N].map(b=>s-=-b),i=1;++i<s;)s=s%i?s:1
- return r
- },
- codegolf103_tyler1g_coop: function f(N) {
- for(r=0;--N;r+=s>1?N:0)for(s=eval([...N+''].join`+`),i=1;++i<s;)s=s%i?s:1
- return r
- },
- codegolf103_tyler1f: function f(N) {
- for(s=[...''+--N].reduce((a,b)=>+a+ +b),i=1;++i<s;)s=s%i?s:1
- return(s>1?N:0)+(N?f(N):0)
- },
- codegolf103_tyler1f_coop: function f(N) {
- for(s=eval([...--N+''].join`+`),i=1;++i<s;)s=s%i?s:1
- return(s>1?N:0)+(N?f(N):0)
- },
- codegolf103_karl: function f(N) {
- for(t=0;N--;){r=[...N+''].reduce((a,b)=>+a+ +b)
- p=1
- for(x=2;x<r;)if(!(r%x++))p=0
- t+=p&r>1?N:0}return t;
- },
- codegolf103_andrew: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(i=2,t=0;i<N;i++) {
- c=(i+'').split``.reduce((a,b)=>+a + +b)
- t+=v=P(c)?i:0
- }
- return t;
- },
- codegolf103_andrew2: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(i=2,t=0;i<N;i++)
- t+=v=P(c=(i+'').split``.reduce((a,b)=>+a + +b))?i:0
- return t;
- },
- codegolf103_andrew3: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(i=2,t=0;i<N;i++)t+=P(c=(i+'').split``.reduce((a,b)=>+a+ +b))?i:0
- return t;
- },
- codegolf103_andrew4: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(t=0;N--;)
- t+=P(c=(N+'').split``.reduce((a,b)=>+a+ +b))?N:0
- return t
- },
- codegolf103_andrew5: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(t=0;N--;)
- t+=P(c=[...N+''].reduce((a,b)=>+a+ +b))?N:0
- return t
- },
- codegolf103_andrew6: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(t=0;N--;)t+=P(c=[...N+''].reduce((a,b)=>+a+ +b))?N:0
- return t
- },
- codegolf103_andrew7: function f(N) {
- P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
- for(t=0;N--;)t+=P(eval([...N+''].join`+`))?N:0
- return t
- },
- codegolf103_andrew8: function f(N) {
- for(t=0;N--;t+=(P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0)(eval([...N+''].join`+`))?N:0);return t
- },
- codegolf103_tw3: function f(N) {
- r=[]
- p=function(n){if(n==(0|1))return 0
- for(var i=2;i<n;i++){if(n%i==0)return 0}return 1}
- for(i=2;i<N;i++){v=eval(i.toString().replace(/(\d)(?=\d)/g,'$1+'))
- p(v)?r.push(i):0}
- return r.reduce((a,b)=>a+b,0)
- },
- codegolf103_tw4: function f(N) {
- r=[]
- l=[2,3,5,7,11,13,17,19,23,29,31,37,41,43]
- for(i=2;i<N;i++){v=eval(i.toString().replace(/(\d)(?=\d)/g,'$1+'))
- l.includes(v)?r.push(i):0}
- return r.reduce((a,b)=>a+b,0)
- },
- codegolf103_mark6: function f(N) {
- for(r=0;--N;r+=N*(p*s>1)){s=0;[...''+N].map(x=>s-=-x);for(p=x=s;--x>1;)p*=s%x}return r
- },
- codegolf103_mark6_coop: function f(N) {
- for(r=0;--N;r+=N*(p*s>1)){s=eval([...N+''].join`+`);for(p=x=s;--x>1;)p*=s%x}return r
- },
- codegolf103_mark7: function f(N) {
- for(r=0;--N;r+=p*(s>1)?N:0){s=0;[...''+N].map(x=>s-=-x);for(x=p=2;x<s;)p*=s%x++}return r
- },
- codegolf103_cody: function f(N) {
- for(a=0,i=2;i<N;i++){c=0;s=i+'';for(j=0;j<s.length;j++)c+=+s[j];p=(c==1)?1:0;for(k=2;k<c;k++)if(c%k==0)p=1;if(!p)a+=i}return a
- },
- codegolf103_adam_golfed: function f(N) {
- r=0
- for(i=10;i<N;i++){m=0
- v=i
- while(v){m+=v%10
- v=Math.floor(v/10)}
- p=!(Array(m+1).join(1).match(/^1?$|^(11+?)\1+$/))
- if(p==true)r=r+i}return r+17
- },
- };
- let tests = [
- {
- 'input': [10],
- 'expect': [17],
- },
- {
- 'input': [30],
- 'expect': [188],
- },
- {
- 'input': [100],
- 'expect': [1672],
- },
- {
- 'input': [1000],
- 'expect': [162948],
- },
- {
- 'input': [10000],
- 'expect': [14244131],
- },
- {
- 'input': [100000],
- 'expect': [1276409458],
- },
- ];
- let timerCount = 1, results = [], bests = [], passed, input, expect, chars, time, actual, fails, error,
- prefix = 'codegolf103',
- colors = getColors(),
- handicaps = {};
- handicaps[prefix] = {
- tyler: 1 - 0.247,
- andrew: 1 - 0.017,
- tw: 1 + 0.308,
- karl: 1 + 0.270,
- mark: 1 - 0.293,
- adam: 1 + 0.201,
- john: 1 + 0.255,
- cody: 1 + 0.067,
- };
- for (let func in Solutions) {
- if (prefix !== func.substr(0, prefix.length)) {
- continue;
- }
- print(func + " - ");
- // Test function against each provide data set
- fails = [];
- passed = true;
- time = +new Date();
- for (let test in tests) {
- // Delete lingering globals between tests
- for (vv in aa = [...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$']) {
- eval('delete ' + aa[vv])
- eval('delete $' + aa[vv])
- }
- input = JSON.parse(JSON.stringify(tests[test].input));
- expect = JSON.parse(JSON.stringify(tests[test].expect));
- error = false;
- try {
- actual = Solutions[func](...input);
- } catch (e) {
- actual = null;
- error = e.message;
- }
- if (actual != expect) {
- print("\nTest " + test + " Failed\nExpected: ");
- print(expect);
- print("Actual : ");
- print(actual);
- print("Input : ");
- print(input);
- print("Error : ");
- print((error ? colors.FgRed + error : colors.FgGreen + 'False') + colors.Reset);
- passed = false;
- fails.push(test)
- }
- }
- time = +new Date() - time;
- time = time / 1000;
- print(func + " - " + (passed ? colors.FgGreen + 'Passed' : colors.FgRed + "Failed") + colors.Reset + "\n");
- chars = Solutions[func].toString().split('\n');
- chars = chars.slice(1, chars.length - 1).map(s => s.trim()).join().length;
- // Time function
- if (1 < timerCount) {
- time = +new Date();
- let i;
- for (i = 0; i < timerCount; i++) {
- for (test in tests) {
- input = JSON.parse(JSON.stringify(tests[test].input));
- expect = tests[test].expect;
- try {
- actual = Solutions[func](...input);
- } catch (e) {
- }
- }
- if ((+new Date() - time) > 10000) {
- break;
- }
- }
- time = +new Date() - time;
- time = time / 1000 * (timerCount / i);
- }
- let id = func.match(new RegExp(prefix + '_([a-z]+)'))
- let score = Math.round(chars / (handicaps[prefix][id[1]] || 1));
- let result = {};
- result['func'] = func;
- result['passed'] = (passed ? 'Yes' : 'no');
- result['chars'] = chars;
- result['score'] = passed ? score : '';
- result['time * ' + timerCount] = time;
- result['fails'] = fails;
- results.push(result);
- // In support of multiple submissions, track everyone's best score
- if (passed
- && !func.match(/(fake|coop|post|fail)$/)
- && (!bests[id[1]] || bests[id[1]] > chars)
- ) {
- bests[id[1]] = chars;
- }
- }
- results.sort((a, b) => a.passed != b.passed
- ? (a.passed > b.passed ? 1 : -1)
- : a.chars != b.chars
- ? (a.chars - b.chars)
- : a.time - b.time);
- bests = Object.values(bests);
- if (bests.length > 0) {
- let result = {};
- result['func'] = ' -- Average Best -- ';
- result['passed'] = 'Yes';
- result['chars'] = (bests.reduce((a, b) => a + b) / bests.length).toFixed(2);
- result['score'] = '';
- result['time * ' + timerCount] = ' - ';
- result['fails'] = '';
- results.push(result);
- }
- print(arrayToTextTable(results));
- function print(s) {
- console.log(s);
- }
- /**
- * Render an ASCII table from supplied 2d array
- *
- * @param input Data to render
- * @param keyAsCol1 Whether to include first dimension array key as column one
- *
- * @return string A basic ASCII table of the data
- */
- function arrayToTextTable(input, keyAsCol1) {
- let data = [];
- let widths = {};
- // Determine column widths
- for (let key in input) {
- let values = input[key];
- if (keyAsCol1) {
- // Like array_unshift, but force empty string for key
- values._ = key;
- }
- // The actual column width determination
- for (let col in values) {
- let datum = values[col];
- widths[col] = Math.max(widths[col] || 0, String(datum).length);
- }
- data.push(values);
- }
- // The above may have added a column, get column widths for headers last
- let headers = JSON.parse(JSON.stringify(data[data.length - 1]));
- for (let col in headers) {
- widths[col] = Math.max(widths[col] || 0, col.length);
- }
- // Draw horizontal bars for top and bottom
- let bar = '';
- for (let i in widths) {
- bar += '+'.padEnd(widths[i] + 3, '-');
- }
- bar += "+\n";
- // Draw column headers
- for (let key in headers) {
- headers[key] = key.padEnd(widths[key], ' ');
- }
- result = bar + '| ' + Object.values(headers).join(' | ') + " |\n" + bar;
- // Draw data
- for (let i in data) {
- let row = data[i];
- for (let col in row) {
- let datum = row[col];
- result += '| ' + ('' + datum).padEnd(widths[col] + 1, ' ');
- }
- result += "|\n";
- }
- result += bar;
- return result;
- }
- function getColors() {
- let colors = {}
- colors.Reset = "\x1b[0m"
- colors.Bright = "\x1b[1m"
- colors.Dim = "\x1b[2m"
- colors.Underscore = "\x1b[4m"
- colors.Blink = "\x1b[5m"
- colors.Reverse = "\x1b[7m"
- colors.Hidden = "\x1b[8m"
- colors.FgBlack = "\x1b[30m"
- colors.FgRed = "\x1b[31m"
- colors.FgGreen = "\x1b[32m"
- colors.FgYellow = "\x1b[33m"
- colors.FgBlue = "\x1b[34m"
- colors.FgMagenta = "\x1b[35m"
- colors.FgCyan = "\x1b[36m"
- colors.FgWhite = "\x1b[37m"
- colors.BgBlack = "\x1b[40m"
- colors.BgRed = "\x1b[41m"
- colors.BgGreen = "\x1b[42m"
- colors.BgYellow = "\x1b[43m"
- colors.BgBlue = "\x1b[44m"
- colors.BgMagenta = "\x1b[45m"
- colors.BgCyan = "\x1b[46m"
- colors.BgWhite = "\x1b[47m"
- return colors
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement