Error 404: Character Not Found

74

6

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!

FlipTack

Posted 2016-12-13T21:14:48.550

Reputation: 13 242

1Related. – Martin Ender – 2016-12-13T21:19:22.397

2Related – FlipTack – 2016-12-13T21:30:20.387

2Are command-line flags subject to radiation (e.g. Perl's -p flag)? – Gabriel Benamy – 2016-12-13T21:38:29.373

16Does the original program (without any characters removed) also need to work? – Adnan – 2016-12-13T21:43:23.913

@Adnan “removing any single character will have no effect” sounds like the one-character-removed programs should do the same thing as the original program, so yes, I’d say – Lynn – 2016-12-13T22:01:23.830

2@Adnan Yes, the original program also needs to work - I'll make that clear in the spec :) – FlipTack – 2016-12-13T22:04:19.860

The -p flag wraps the program in while(<>){ ... ; print$_}. Does that count as logic? – Gabriel Benamy – 2016-12-13T22:06:29.773

@GabrielBenamy I'd say it does, sorry – FlipTack – 2016-12-13T22:28:27.770

5Can errors be ignored if the program still prints 1 - 404? – jrich – 2016-12-13T23:17:17.243

@jrich yep, stray error output is allowed by default :) – FlipTack – 2016-12-14T05:45:45.960

4Maybe there should be a tag like [tag:radiation-hardering]? – Vi. – 2016-12-14T18:10:26.150

5Voting to close, we are not doing your homework for you XD – チーズパン – 2016-12-16T09:05:50.780

@ケセブロト crap, you got me. – FlipTack – 2016-12-16T19:39:35.710

Answers

16

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!

Adnan

Posted 2016-12-13T21:14:48.550

Reputation: 41 965

34

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.

jrich

Posted 2016-12-13T21:14:48.550

Reputation: 3 898

3I 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. – ETHproductions – 2016-12-14T14:01:39.067

What happens if part of "top" gets lost? – Scimonster – 2016-12-15T12:11:03.320

@Scimonster It still parses as a function, returning 4, just with the unused parameter named "to" or "op" or "tp" – jrich – 2016-12-15T12:12:46.803

1@jrich of course. Brain cramp. :P – Scimonster – 2016-12-15T12:14:38.360

What happens if you lose part of this? – dkudriavtsev – 2016-12-16T19:51:34.173

@wat Causes an error trying to access a property of an undefined variable such as "ths", which doesn't halt the program execution due to the setInterval. – jrich – 2016-12-16T20:00:19.133

@jrich

you must make it so that removing any single character will have no effect

– dkudriavtsev – 2016-12-16T20:32:32.450

@wat OP has clarified that stray error output is ok.

– jrich – 2016-12-16T20:40:49.707

25

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.

Maltysen

Posted 2016-12-13T21:14:48.550

Reputation: 25 023

23

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).

Martin Ender

Posted 2016-12-13T21:14:48.550

Reputation: 184 808

16

><>, 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)

mbomb007

Posted 2016-12-13T21:14:48.550

Reputation: 21 944

Can you use o" e"? – Martin Ender – 2016-12-14T06:11:12.030

And instead of +1 you can use l. Also the fishlanguage.com interpreter is a bit weird (and annoying to use, although the debugging feature is useful of course). TIO uses the somewhat saner Python interpreter, I think: https://tio.run/nexus/fish#@m9oqIAMysq4AMuxtrfVMslXUkhVssqzsrHBEPj/HwA

– Martin Ender – 2016-12-14T08:11:23.333

@MartinEnder That doesn't work if you remove one of the 1s on the first line. – mbomb007 – 2016-12-14T14:56:53.927

Ah right, those were bothering me anyway: https://tio.run/nexus/fish#@q@ACsrKuKztAbPVMslXUkhVysnLybGxwRD4/x8A

– Martin Ender – 2016-12-14T14:57:49.893

12

><>, 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.

MildlyMilquetoast

Posted 2016-12-13T21:14:48.550

Reputation: 2 907

1Really 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. – Teal pelican – 2016-12-14T09:43:03.990

1You can also replace the :+1 and the other : with one l each. – Martin Ender – 2016-12-14T10:06:02.387

Yes, thank you both! – MildlyMilquetoast – 2016-12-14T16:25:54.680

;?=*4o" e"n:ll works too. – mbomb007 – 2016-12-14T18:40:40.317

11

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.

user62131

Posted 2016-12-13T21:14:48.550

Reputation:

9

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!)

MildlyMilquetoast

Posted 2016-12-13T21:14:48.550

Reputation: 2 907

what happens to empty lines in the code?. Maybe you could double the linefeed to make it immune, too – masterX244 – 2016-12-14T12:04:46.073

You're completely correct @masterX244 thanks! Didn't think about that. – MildlyMilquetoast – 2016-12-14T16:17:22.907

lol, never used the language at all but for some reason a 6th sense gave me that idea... – masterX244 – 2016-12-14T19:45:44.070

5

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.

James Holderness

Posted 2016-12-13T21:14:48.550

Reputation: 8 298

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. – MildlyMilquetoast – 2016-12-14T00:46:11.673

5

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)

user2428118

Posted 2016-12-13T21:14:48.550

Reputation: 2 000

@Flp.Tkc Fixed. – user2428118 – 2016-12-16T09:57:39.087

4

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])

Cobi

Posted 2016-12-13T21:14:48.550

Reputation: 111