JavaScript (updated to work with all test cases)
The little-known truth is that there are actually four 6
s, but one of the betrayed the others and polymorphed into code form to eradicate them from the world digits of the numbers. Here is that traitorous six:
x=prompt(''+
'Enter number');
alert( ( (~x[
'ind'+
'exOf']('666')))?(x
.replace(/666(.*)$/,
function (mat,g){
return '667'+g
['re'+ 'place'
](/./g,0)})):((+x+
1+'').replace(
666,667)));
Here is an explanation. First, beautify the code and remove useless stuff like ''+'string'
and ((code))
:
x = prompt('Enter number');
alert(
~x['indexOf']('666')
?
x.replace(/666(.*)$/, function(mat,g) {
return '667' + g['replace'](/./g,0)
})
:
(+x+1+'').replace(666, 667)
);
Convert weird notations (like ~indexOf
and ['replace']
) into more common ones:
x = prompt('Enter number');
alert(
x.indexOf('666') > -1
?
x.replace(/666(.*)$/, function(mat, g) {
return '667' + g.replace(/./g, 0)
})
:
((parseInt(x) + 1) + '').replace(666, 667)
);
And now simply understand that the algorithm goes like this:
Old version (doesn't work for 666666666
):
s='Enter number';x
=prompt( ''+
s);x=+x+
(-~![]);
x=(''+x).replace('666',
666+([][ +[]]+[])
[+[]]['l ength'[
'repla'+ 'ce'](
/ /g,'')]);alert(x)
To understand this, let's first beautify it:
s = 'Enter number';
x = prompt('' + s);
x = +x + (-~![]);
x = ('' + x).replace('666',666+([][+[]]+[])[+[]]['l ength'['repla'+'ce'](/ /g,'')]);
alert(x);
Now let's remove useless things like '' + string
and 'str' + 'ing'
, remove the unnecessary s
variable, and change weirdness like -~![]
into 1
:
x = prompt('Enter number');
x = +x + 1;
x = ('' + x).replace('666', 666+"undefined"[0]['l ength'['replace'](/ /g,'')]);
alert(x);
'l ength'['replace'](/ /g,'')
is simply "length"
:
x = prompt('Enter number');
x = +x + 1;
x = ('' + x).replace('666', 666+"undefined"[0].length);
alert(x);
And "undefined"[0]
is "u"
, and "u".length
is 1
:
x = prompt('Enter number');
x = +x + 1;
x = ('' + x).replace('666', 666 + 1);
alert(x);
Now we're done! It should be pretty easy to understand now.
1What about something like 456667? Should that return 456670, or are we only worried about a leading 666? – Kyle Kanos – 2014-04-29T14:18:00.610
1@KyleKanos - Yes. The returned value can't contain 666 anywhere. I'll add a few more examples including yours. – billpg – 2014-04-29T15:50:58.370
11I think this would be better as [tag:code-golf] than [tag:popularity-contest] since there are such straightforward solutions. – Nate Eldredge – 2014-04-29T16:08:30.273
1Replacing 666 with 667 won't work if there's more than one 666 in the input. I don't see why not, as long as all instances are replaced left-to-right, e.g.
66666
->66766
. – nyuszika7h – 2014-04-29T16:56:38.52710@nyuszika7h but the result should be
66700
. – Martin Ender – 2014-04-29T17:07:02.1071@m.buettner Oh, right. – nyuszika7h – 2014-04-29T17:08:12.373
What should 6660666 evaluate to? 6670000? – Greenstone Walker – 2014-04-30T02:40:50.220
@GreenstoneWalker Yes, I think thats right. 6670000 is the lowest number greater than 6660666 that has no consecutive 666 digits – Digital Trauma – 2014-04-30T03:55:04.837
Actually you just add 1 once, then search first occurrence of "666", replace it with "667" and substitute all digits after that with 0-s; – Cthulhu – 2014-04-30T06:40:10.340
you can replace "666" with "667" and add zeros, which I did in the Julia example – Milktrader – 2014-04-30T13:04:43.790
3as a real challenge, there should've also been a requirement to not have "666" anywhere in the code – DLeh – 2014-04-30T19:18:46.757
1@DLeh
665 + 1
– Doorknob – 2014-05-01T00:13:36.7171right, try using 665 + 1 in regex. – DLeh – 2014-05-01T10:52:17.250
@DLeh Highest voted answer doesn't use regex. But in any case "regexbefore" + "66" + "6" + "regexafter" still works. Or str(665+1) – Cruncher – 2014-05-01T14:41:25.260
2Depending on your regex implementation, you could use '6{3}' to detect 666. – celtschk – 2014-05-01T19:33:36.750
You want to make 'Your code must not use a loop that adds one until it finds a result that fits the rules' more bulletproof. I could simply add 3 (you never said the numbers had to be consecutive). I could add 2 and subtract 1. I could add 10 and count down, rejecting the first 8 numbers. etc. I could add 1.6 (float) and convert back to integer. This alone (workaround 'adding one') would make a lawyerly code-golf puzzle in itself... – smci – 2014-05-01T20:47:24.663
1As the evil integers should be avoided, shouldn't that also apply to source code + computer memory? That would make the challenge significantly harder. – Simon Forsberg – 2014-05-02T12:53:46.820
Your specification looks weird. I wouldn’t call something an ID unless it were unique. And uniqueness (possibly asking that ID are increasing wrt ales, and that they are always as small as possible) seems to me like a more interesting challenge. Not that I would compete. – Édouard – 2014-05-04T10:38:49.743