Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

This idea is not mine, though I don't know where it originated. I once met it in a programming contest very long ago (1998, if I remember correctly). The task is to write a program in your favorite language that outputs 2012 and only 2012. The catch is that the program must still output 2012 after any one of its characters is modified. The modification can be either insertion, deletion or replacement. Of course, the modification will be such that the program is still syntactically valid.

Since I don't know all the programming languages, I have to ask the audience to help me and test the answers submitted.

Added: Many have commented that my definition of acceptable modifications is too vague. Here's my second attempt: The allowed modifications will leave your program syntactically valid and will not cause it to crash. There, I think that should cover all the compile-time, link-time and run-time errors. Though I'm sure that there will be some odd edge case in some language anyway, so when that comes up we'll look at it individually.

share|improve this question
How about runtime errors like reading a most likely invalid address? – eBusiness Jan 2 '12 at 21:15
@PeterTaylor - There are 3 answers already, the C one with heavy revising. You can look for inspiration there. – Vilx- Jan 2 '12 at 21:58
2  
Anyone figured out if this is impossible in APL or GolfScript or similarly terse? – Jeff Burdges Jan 3 '12 at 8:03
5  
This has got me thinking about DNA and redundancy and the potential for cosmic rays to flip bits in my programs. Interesting stuff. – Jon Purdy Jan 3 '12 at 8:55
4  
... You need some sleep. – Vilx- Jan 3 '12 at 8:57
show 18 more comments

37 Answers

1 2

Tcl, 55 chars.

if {[set a {puts 2012}]=={puts 2012}} $a {
puts 2012
#}

Check if the code is unmodified, then execute it, or print 2012.
I consider accessing a undefined variable as syntax error.

Tcl, 138 Characters

set a {set a {$a};if {\$a=={$a}} {puts 2012}};if {[info ex a]&&$a=={set a {$a};if {\$a=={$a}} {puts 2012}}} [subst -noc $a] {
puts 2012
#}

Ok, this is a quine variant: either the code is unmodified, then execute it, or simply print 2012

The last line is a little bit special: It is a comment, but the } closes the brace.

share|improve this answer

Python 40 characters

a='2012';print(2012
)if a!='2012'else a

I don't think it's possible to get it any smaller with this approach in python.

edit: added 4 quotes to counter effects of -a on the end edit: removed space

share|improve this answer
1  
I don't know much about Python, but can you insert a - before the last a? – Vilx- Jan 3 '12 at 14:45
so it does... this calls for a rethink – Matt Jan 3 '12 at 14:49
a='2012';print a=='2012'and a or 2012 shorter – AMK Dec 16 '12 at 5:59
@AMK as gnibbler mentioned in codegolf.stackexchange.com/a/4614/6687, you can just replace the t in print with = making a='2012';prin= a=='2012'and a or 2012 – jsvk Feb 6 at 2:33

Python (45 49 52 chars)

nn='2012'
if nn=='2012':
    print nn
else:
    print 2012
share|improve this answer
@hammar fixed. Made n a string – elssar Jan 4 '12 at 6:27
Oh that still leaves it susceptible to changing print n to print any constant This needs a complete rethink – elssar Jan 4 '12 at 6:29
Wait a second. @hammar I don't think changing n to -n is legal. The problem states that one character can be modified, but it doesn't say anything about adding a character. – elssar Jan 4 '12 at 6:35
Yes it does: The modification can be either insertion, deletion or replacement. – hammar Jan 4 '12 at 6:39
3  
I hate you guys >_< – elssar Jan 5 '12 at 7:24
show 12 more comments

Lua, 33 36

xx=2012
print(xx==2012
and xx or 2012)
share|improve this answer
I think changing and x to and 1 will break it. :( – Dillon Cower Jan 6 '12 at 6:13
@DC gah I guess I do have to use 2 xx's. – greatwolf Jan 6 '12 at 6:16
1  
Change and xx to and -xx.. eeeeeeek! – Dillon Cower Jan 6 '12 at 6:33

I write it in Java

try{
    //your code
) catch(Exception e) {
//eat exception
} finally{
    System.out.println("2012");
}
share|improve this answer
5  
If I change 2012 to 2013, it gives the wrong output. And, yes, any character of the program may be modified. – Vilx- Jan 10 '12 at 12:28
4  
haha, is this how blindly people believe in "finally will always execute no matter what", that they even think the code there is immune to changes?! – noah1989 Apr 24 '12 at 15:12

Python - 50 41 40 41 chars

x='2012'
if x=='2012': print x
else: print '2012'

exec('cHJpbnQgMjAxMg=='.decode('base64'))

exec('x\x9c+(\xca\xcc+Q0204\x02\x00\x13\xfa\x03\x13'.decode('zip')

)

Yes, I know you can modify 'print x' into "print 1" or something similar, but not sure how to beat that..

share|improve this answer
And this can still be beaten by a single hash mark at the start of the line... – Vilx- Mar 25 '12 at 19:52
@Vilx- If I break it into two lines, it will throw an error with a comment, but it'll increase my byte count by 1. – Sargun Dhillon Mar 25 '12 at 21:33
I guess now would be the time for you to discover that which others already did in the comments - this is NOT code golf. XD – Vilx- Mar 26 '12 at 7:53

Ruby (57 36)

EDIT And another one in 36 chars.

p((x,y='2012','2012';x=='2012'?x:y)) 

I guess using strings is pretty failsafe because that minus thing doesn't work any more.


EDIT next try (36 chars) [never mind, adding a - after p results in -2012]

p [2012,2012].find(2012).first||2012


This one will only work this year, but also for similar contests in the future :) (15.chars)

p Time.now.year

Note that this substitution is also working:

p Time.new.year

57 chars. If you count the newlines, too it is 76 chars, though.

p( 
#
#
#
#
#
#
#
#
#
#
__LINE__.to_s.prepend(
#
#
#
#
#
#
#
__LINE__.to_s))

share|improve this answer
First two solutions - add a # at the start of the line. Last solution - enter a newline before p. (Note - I don't know Ruby myself, so perhaps I'm wrong). – Vilx- Apr 25 '12 at 13:02
damn :) you're right – padde Apr 25 '12 at 13:30
Prints "2012" for me. The quotes aren't conform - are they? – user unknown May 10 '12 at 3:37
Yeah you are right. You could use $><< or puts instead. Whatever, i'm not gonna try this any more, i gave up :) – padde May 10 '12 at 7:02
1 2

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.