74
\$\begingroup\$

Scenario

One of your friends is struggling on a homework assignment. He needs a simple program which prints the first 404 natural numbers:

1 2 3 4 5 6 7 8 9 10 11 ... 401 402 403 404

Your challenge is simple: write this program for him.

However, your connection is terrible, so 1 random character is lost every time you send the program. To prevent this from breaking your program, you must make it so that removing any single character will have no effect: the program works, regardless. (The original program must also work.)

Because the connection is too poor to send large files, your code must be as short as possible.

TL:DR - make a radiation hardened program to output the numbers 1 to 404


Rules/Details

  • The output may be a list of integers in any reasonable format (separated by spaces, newlines, commas, etc). However, your output must be consistent and not change when the program is modified.
  • Command line flags that contain logic, execute actual code, generate the number list, and so on, are banned.
  • This is , so the shortest submission (in bytes) wins!
\$\endgroup\$
  • 2
    \$\begingroup\$ Related \$\endgroup\$ – FlipTack Dec 13 '16 at 21:30
  • 16
    \$\begingroup\$ Does the original program (without any characters removed) also need to work? \$\endgroup\$ – Adnan Dec 13 '16 at 21:43
  • 5
    \$\begingroup\$ Can errors be ignored if the program still prints 1 - 404? \$\endgroup\$ – jrich Dec 13 '16 at 23:17
  • 4
    \$\begingroup\$ Maybe there should be a tag like radiation-hardering? \$\endgroup\$ – Vi. Dec 14 '16 at 18:10
  • 5
    \$\begingroup\$ Voting to close, we are not doing your homework for you XD \$\endgroup\$ – チーズパン Dec 16 '16 at 9:05

11 Answers 11

16
\$\begingroup\$

05AB1E, 12 bytes

Code:

XX440044ÔÔŸŸ

Normal code explanation:

X             # Pushes the number 1.
 X            # Pushes the number 1 again.
  440044      # Pushes the number 440044.
        Ô     # Connected uniquify.
         Ô    # Connected uniquify.
          Ÿ   # Inclusive range of the top two elements.
           Ÿ  # Inclusive range on the array, which leaves it intact.

This leads to the following golfed code: X404Ÿ, which is what we want to achieve.

The number 404 is generated by any of these variants:

440044ÔÔ
40044ÔÔ
44044ÔÔ
44004ÔÔ
440044Ô

The inclusive range works as following on two numbers:

`1 5Ÿ`             ->    [1, 2, 3, 4, 5]
[1, 5]Ÿ            ->    [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]Ÿ   ->    [1, 2, 3, 4, 5]

Which always makes the second Ÿ a no-op.

Uses the CP-1252 encoding. Try it online!

\$\endgroup\$
34
\$\begingroup\$

JavaScript, 228 bytes

etInterval=stInterval=seInterval=setnterval=setIterval=setInerval=setIntrval=setInteval=setInteral=setIntervl=setInterva=top=>4

setInterval
`405>(i=this.i?i:1)?alert(i++):0`///`
setInterval
`405>(i=this.i?i:1)?alert(i++):0`///`

Explanation

Evaluating code in a setInterval allows the program to continue even if there is an error. We make sure that the code passed will error out if any single character is removed. Template string syntax can be abused to call setInterval with a constant string without parentheses. Luckily, even if the template string is on the next line it still parses as a function call with such syntax.

The first issue we run into is that setInterval is a function, and thus if a character is removed and the program tries to call setInteval instead, it errors out. Of course, since there are two indentical invocations of the setInterval, we don't have to worry about it actually functioning correctly as long as we avoid the error. So, the first line defines every possible "misspelling" of setTimeout to a valid function.

The first line works by assigning all of these "misspellings" to the function top=>4. Note the ES6 syntax, this simply takes a paramater named "top" and returns 4. Why "top"? Well, the first line must never throw an error even if a character is removed. If an = is removed to make top>4, this boolean expression will be valid since top is predefined in browsers, simply yielding false. If the 4 is removed, the function body simply becomes the first setInterval segment, and the second runs unharmed.

Now, all that is left to worry about is if a ` is removed.

If removed from the beginning, setInterval simply does nothing, evaluating to itself as its own expression. Then, the rest of the second line simply runs a single iteration of the loop, letting the other setInterval fragment finish the job. If removed from the end, the remaining backtick is picked up from the end of the comment.

The newlines are placed so that removal one will not affect program behavior, but they prevent errors in the case of some character removals such as the leading backtick.

\$\endgroup\$
  • 3
    \$\begingroup\$ I pondered what would break if one of the digits in a 405 were removed, but then I realized: nothing, because either all 404 numbers have already been printed, or the second loop will print the missing ones. Spectacular work. \$\endgroup\$ – ETHproductions Dec 14 '16 at 14:01
  • \$\begingroup\$ What happens if part of "top" gets lost? \$\endgroup\$ – Scimonster Dec 15 '16 at 12:11
  • \$\begingroup\$ @Scimonster It still parses as a function, returning 4, just with the unused parameter named "to" or "op" or "tp" \$\endgroup\$ – jrich Dec 15 '16 at 12:12
  • 1
    \$\begingroup\$ @jrich of course. Brain cramp. :P \$\endgroup\$ – Scimonster Dec 15 '16 at 12:14
  • \$\begingroup\$ What happens if you lose part of this? \$\endgroup\$ – dkudriavtsev Dec 16 '16 at 19:51
26
\$\begingroup\$

Pyth - 16 bytes

The basic idea behind this is that when you take a digit off of 404, it only makes the number smaller, so we just have to get the maximum of two 404's to ensure we have the right number. Obviously, there are a bunch more redundancies.

SSsetSS[404  404

Explanation:

SS                       First one does 1-index range, second one sorts, which is no-op 
 s                       If the e is there, this is no-op, if only t is there, it sums a one element list, which is the item
  et                     e picks last element in list, and if e is gone, then t gets you the list without the first element which is good enough when combined with s
   SS                    Sorting twice is no-op
    [                    Start list, this can't be two element because if we get rid of initial 4, the 0 becomes third element which neeeds to be captured
     404                 One of the 404's
     <space><space>404   Need two spaces for redundancy, because 404404 *is* bigger than 404

Try it online here.

\$\endgroup\$
23
\$\begingroup\$

Befunge-98, 37 bytes

20020xx##;;11++::''ee44**``kk@@::..;;

Try it online!

Explanation

Making radiation-hardened code in Befunge-98 isn't too bad, because you can set the "delta" (i.e. the step size of the instruction pointer) manually with x. So if set the delta to (2,0), from then on every other character is skipped and we can simply double up all commands. The tricky thing is getting 2 0 on top of the stack in a reliable way. We'll actually need 0 2 0 for the rest the program to work correctly, but we'll that for free. Here is how we do this:

20020xx

Note that each digit pushes itself, so in the full program, there will be a start 2 0 which we'll simply ignore.

Consequently, dropping either the first or second character from the program is irrelevant because we won't use those digits anyway. Likewise, removing the third character is identical to removing the second, so we don't need to worry about that either.

Let's consider what happens in the other two cases. Dropping the fourth character:

2000xx

Note the delta is set to (0,0). But this doesn't move the instruction pointer at all, so the same x is executed again immediately and this time pops the (2,0) and all is well (there are implicit zeros at the bottom of the stack for our later purposes).

Let's drop the fifth character instead:

2002xx

Now the delta gets set to (0,2). However, there is still no horizontal movement, so the IP wraps immediately back to x and again, the correct delta gets set.

From this point onward we can basically ignore the character duplication as well as this initial part because it will always be skipped:

...#;1+:'e4*`k@:.;

The ; is a sort of comment command which skips everything until the next ; is encountered. However, we jump over the first ; with # so only the part between the ; will be executed from that point onward.

1+    Increment the top of the stack.
:     Duplicate it.
'e    Push 101.
4*    Multiply by 4 to get 404.
`     Greater-than check. Pushes 1 once the top
      reaches 405, otherwise 0.
k@    Terminate the program that many times.
:.    Print a copy of the top of the stack (and a space).
\$\endgroup\$
16
\$\begingroup\$

><>, 103 60 51 bytes

               vv
;?=*4o" e"n:ll<<
;?=*4o" e"n:ll<<

Tested here.

Uses the same tactic as this program. If a character is deleted in the first line, the 2nd line will still be run. If a character in the 2nd line is deleted, the v will move execution to the 3rd line, since the 2nd line is a character shorter. A deletion in the 3rd line has no effect on program control, as it is only execution after a deletion in line 2.

The program will also work in the event that a line break is deleted:

Case 1:

The 2nd line will be run.

               vv;?=*4o" e"n:ll<<
;?=*4o" e"n:ll<<

Case 2:

Both lines become one line with twice the instructions.

               vv
;?=*4o" e"n:ll<<;?=*4o" e"n:ll<<

Explanation:

The core of the program is the following. Note that a 1 is pushed on the stack already by the first line.

ll:n        Push length of the stack twice, duplicate, print as number
"e "o       Push 101, 32, output 32 as space
4*          101 * 4 is 404
=?;         Pop twice, if equal, halt
            (Execution wraps around)
\$\endgroup\$
12
\$\begingroup\$

><>, 42 38 34 bytes

Try it Online!

Thanks to @Martin Ender and @Teal Pelican for shaving off 8 bytes!

<<;?=*4o" e"lnll/
 ;?=*4o" e"lnll<

Similar to mbomb007's answer, but only uses 2 lines.

Instead of counting up from 1 to 404 using a single memory value, I continuously pushed the length of the stack. This made it so that I didn't need to put anything on the stack before the loop started, making things a lot easier.

The original code

The <s turn the IP around, and the / is a no-op, as the IP wraps around and continues along it's normal path. Thus, the relevant code is:

          lnll   Prints the size of the stack + 1 and pushes the stack size twice
     o" e"       Prints a space and pushes 101
   *4            Multiplies to get 404
;?=              If the number we just printed was 404, end the program

And this repeats.

The deletion

The big key here is the mirror /. It doesn't do anything if the second line is modified - only wraps back around to itself and is essentially a no-op. But if a character from the first line is removed, the line will shift down, so that the mirror hits the arrow <, leading into an identical, unmodified loop.

The only other significant deletion would be the \n character. This is also accounted for, as it produces this code:

<<;?=*4o" e"lnll/ ;?)*4o" e"lnll<

Now, we have just appended a copy of the original loop ;?=*4o" e"lnll to itself. As this is a loop, it makes no difference to the execution, and runs as if nothing had changed.

\$\endgroup\$
  • 1
    \$\begingroup\$ Really nice way to reduce it to 2 lines. The only problem I can see is that it's not 38 bytes but 42? Reducing the inner code to; ;?=*4o" e":n:+1l would reduce it down to 38 bytes though. This just places your 84* within the string " e" instead of separate. \$\endgroup\$ – Teal pelican Dec 14 '16 at 9:43
  • 1
    \$\begingroup\$ You can also replace the :+1 and the other : with one l each. \$\endgroup\$ – Martin Ender Dec 14 '16 at 10:06
  • \$\begingroup\$ Yes, thank you both! \$\endgroup\$ – MildlyMilquetoast Dec 14 '16 at 16:25
  • \$\begingroup\$ ;?=*4o" e"n:ll works too. \$\endgroup\$ – mbomb007 Dec 14 '16 at 18:40
11
\$\begingroup\$

A Pear Tree, 34 bytes

The program contains control characters, so here's an xxd dump:

00000000: 7072 696e 7420 312e 2e34 3034 2327 108a  print 1..404#'..
00000010: af70 7269 6e74 2031 2e2e 3430 3423 2710  .print 1..404#'.
00000020: 8aaf                                     ..

A Pear Tree is basically a Perl derivative, with some "interesting" features. I threw it together as a joke (so that I could say that my polyglot printed a partridge in A Pear Tree; in fact, almost all programs do). However, it's Turing-complete, and actually kind-of good at this sort of challenge.

The feature that we mostly care about here is that A Pear Tree will only run a program if some substring of the code has a CRC-32 of 00000000. The code gets rotated to put the substring in question at the start. As it happens, the two (identical) halves of the code each have the CRC-32 in question (due to that suspicious-looking binary comment at the end), so if you delete a character from the source (breaking the CRC), the other half gets rotated to the start and the # sign near the end will comment the damaged half out, in addition to the binary junk.

Another feature makes the program slightly smaller: although A Pear Tree is mostly interpreted as Perl, it has some minor changes to make it work more like Python. One that's relevant here is that unlike Perl's print statement (which just runs the numbers together), A Pear Tree's print statement separates arguments with spaces and prints a final newline. That gives us nicely space-separated output, meaning that we don't have to waste bytes on formatting. (Note that you'll have to give the program no input; if the language receives input, it assumes by default that it's supposed to do something with it.)

Of course, this can't compete with actual golfing languages (nor would I expect it to), but I thought people might find it interesting.

\$\endgroup\$
9
\$\begingroup\$

Befunge 98, 34 bytes

Try it Online!

<<.:@k`*4e':+1[

2.:@k`*4e':+1<^^j

This works very similarly to my ><> answer, but instead of the mirror /, I use the turn left operation [ and then reverse the IP direction, which is functionally equivalent to a mirror in this case.

The original code

<<                 Reverses code direction, sending IP to right
               [   Turns the IP left, into the ^, which reverses it.
                   It then hits this again, turning left to go West. (No-op)
            :+1    Adds 1 to the sum
        *4e'       Pushes 404
     @k`           Ends the program if (sum > 404)
   .:              Prints the sum

The deletion

If something in the second line is deleted, it will shift over, and not affect the top at all.

If anything in the first line is deleted, the [ will send the IP into the <, which starts an identical loop (with the exception of 2j^^ which avoids the ^s used in tandem with the [)

Because there are 2 new lines, it makes no difference to the code if one is deleted (thanks to @masterX244 for this!)

\$\endgroup\$
  • \$\begingroup\$ what happens to empty lines in the code?. Maybe you could double the linefeed to make it immune, too \$\endgroup\$ – masterX244 Dec 14 '16 at 12:04
  • \$\begingroup\$ You're completely correct @masterX244 thanks! Didn't think about that. \$\endgroup\$ – MildlyMilquetoast Dec 14 '16 at 16:17
  • \$\begingroup\$ lol, never used the language at all but for some reason a 6th sense gave me that idea... \$\endgroup\$ – masterX244 Dec 14 '16 at 19:45
5
\$\begingroup\$

Befunge-93, 54 51 bytes

Thanks to Mistah Figgins for saving me 3 bytes.

111111111111111vv
+1_@#-*4"e":.:<<
+1_@#-*4"e":.:<<

Try it online!

This is essentially the same trick as was used in the Fault-Tolerant Hello World challenge. The first line starts by making sure there is a 1 on the top of the stack for the start of the sequence, and then one of the v arrows at the end of the line redirects the code path to the start of the main routine on line 2, executing from right to left.

Removing a character from the first line will just shift the v arrows across by one, but that still allows the code to redirect to the second line successfully. Removing a character from the second line causes the < arrow at the end of the line to shift out of the path of the v above it, so the code path will be redirected to the backup routine on line 3.

Removing the first line break does no harm, because that just moves the third line into place to replace the second line. And removing anything after the end of the second line will have no effect, because that's just the backup code.

\$\endgroup\$
  • \$\begingroup\$ The first line starts by making sure there is a 1 on the top of the stack... The first line is pretty sure of itself in that respect. Also, you can shorten the <code>_@#!</code> to _@#-` and put it before the printing to save 3 bytes. \$\endgroup\$ – MildlyMilquetoast Dec 14 '16 at 0:46
5
\$\begingroup\$

JavaScript + HTML + Stack Snippets, 167 158 154 bytes

Abusing the fact that JavaScript in Stack Snippets gets placed in a web page inside a <script> element.

or(x="",i=405,d=document;--i;)d[i]||(x=d[i]=i+' '+x);d.write(x)</script></script><script>for(x="",i=405,d=document;--i;)d[i]||(x=d[i]=i+' '+x);d.write(x)

\$\endgroup\$
  • \$\begingroup\$ @Flp.Tkc Fixed. \$\endgroup\$ – user2428118 Dec 16 '16 at 9:57
4
\$\begingroup\$

Actually, 18 bytes

:404::404kMkMMRRSS

Actually is a stack based golfing language.

Explanation of the commands involved (as they work in the above context):

Numbers are indicated by round brackets () and lists by square brackets []

:         - pushes the longest string of characters in '0123456789+-.ij' as a numeric
k         - pop all the elements on the stack into a list [l] and push [l]
M         - pop [l], push max([l])
            pop (a), push (a)
R         - pop [l], push reverse([l])
            pop (a), push range([1,a])
S         - pop [a], push sorted([a])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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