8
\$\begingroup\$

In this challenge, the goal is to create a pristine truth machine in as few bytes as possible. For reference, a truth machine does the following:

  • Takes input, which is always 0 or 1
  • If the input is 0, it is printed and the program terminates
  • If the input is 1, it is printed forever

A pristine program is one that does not error on its own, but will error if any single substring with a length \$1\le n<p\$ is removed (where \$p\$ is the length of the program).

For example, if the main did not error in some language, it would be pristine if all of the following produced an error:

mai
man
min
ain
ma
mn
in
m
n

The goal of this challenge is to write the shortest program or function (in bytes, per language) which is a pristine truth machine.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Related (truth machine, doesn't have to be pristine) \$\endgroup\$
    – Luis Mendo
    Oct 9 '20 at 21:18
  • \$\begingroup\$ Do warnings count as pristine? Also, are functions allowed to return either 0 or an infinite iterator that always returns 1? And if they are allowed, may their pristineness depend on them being a specific type (that is to say if the function changes return types may we assume something downstream will break)? \$\endgroup\$
    – Aiden4
    Oct 9 '20 at 22:55
  • \$\begingroup\$ Are empty programs supposed to error or do those not count? \$\endgroup\$
    – lyxal
    Oct 9 '20 at 23:00
  • 1
    \$\begingroup\$ @Lyxal No, it says 1 <= n < p for the length of the segment removed, so there will always be one byte remaining at minimum. With a one byte solution, it's automatically pristine. \$\endgroup\$ Oct 9 '20 at 23:29
  • 1
    \$\begingroup\$ Wow, nice! This is quite a tough challenge especially because we might run into an infinite loop when trying to verify the pristinity of our solution. And it requires knowing a language very well. \$\endgroup\$
    – Domino
    Oct 15 '20 at 2:42
8
\$\begingroup\$

Rust, 109 106 bytes

fn t(n:i8)->Result<Box::<Iterator::<Item=i8>>,i8>{if n==0{Err(0)}else{Ok(Box::new(std::iter::repeat(1)))}}

Try it online!

Returns a result whose ok variant is a dynamically typed iterator that always yields one wrapped in a smart pointer so the compiler can know its size. The error variant is simply zero. The code does emit a deprecation warning, so it may randomly stop working at some point in the future. Rust's type inference is too good for me to get a pristine closure, and as verbose as this solution is it is still shorting than fully listing the returned iterator's type and much shorter than using the main function and reading from stdin.

-3 bytes because there is a method to create an endlessly repeating iterator

\$\endgroup\$
2
  • \$\begingroup\$ I think it doesn't error by itself if I remove the entire substring if n==0...else. \$\endgroup\$
    – Bubbler
    Oct 15 '20 at 0:56
  • \$\begingroup\$ @Bubbler it would because then the function would have a return type of () which is not what I specified. \$\endgroup\$
    – Aiden4
    Oct 15 '20 at 1:08
6
\$\begingroup\$

W s, 1 byte

w

The s flag tells the program to get it's input from STDIN rather than arguments.

It's pristine because there is only one subset of program to remove.

The power of triviality.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You might want to clarify with OP, but \$1\le n<p\$ I think implies that \$p>1\$ i.e. 1 byte programs cannot be pristine. Furthermore, can you provide a link to an interpreter? \$\endgroup\$ Oct 9 '20 at 22:56
  • \$\begingroup\$ @cairdcoinheringaahing > No, it says 1 <= n < p for the length of the segment removed, so there will always be one byte remaining at minimum. With a one byte solution, it's automatically pristine. \$\endgroup\$
    – lyxal
    Oct 9 '20 at 23:29
  • 1
    \$\begingroup\$ Interesting. I‘ve always seen pristine programs be a minimum of 2 bytes long (or the task not possible in a single byte). \$\endgroup\$ Oct 9 '20 at 23:37
  • 2
    \$\begingroup\$ I didn't realize Flowey golfed.. \$\endgroup\$
    – booshlinux
    Oct 10 '20 at 16:06
  • \$\begingroup\$ @cairdcoinheringaahing I think 1 byte programs are a special case, I probably should have disallowed them, but luckily I doubt there're more than 20 or so languages where there's a 1 byte solution to this. \$\endgroup\$ Oct 10 '20 at 17:05
2
\$\begingroup\$

C# (.NET Core), 159 bytes

class P{static void Main(string[]a){System.Console.Write(
#region
a[0]);while(1.ToString()==a[0]){System.Console.Write(a[
#region
0]);}}}
#endregion
#endregion

Try it online!

\$\endgroup\$
9
  • \$\begingroup\$ There are unnecessary whitespaces, and l can be removed from #region (all of which make it not pristine). Removing them gives this. But unfortunately removing do does not error, so it's still invalid. \$\endgroup\$
    – Bubbler
    Oct 13 '20 at 8:56
  • \$\begingroup\$ @Bubbler thanks for the input, I've addressed your concerns and this should be better \$\endgroup\$
    – LiefdeWen
    Oct 13 '20 at 9:29
  • \$\begingroup\$ I think you didn't change the TIO link? And the space in string[] a is still there, and I can still remove do without erroring. \$\endgroup\$
    – Bubbler
    Oct 13 '20 at 9:32
  • \$\begingroup\$ I'll delete for now then, while I improve \$\endgroup\$
    – LiefdeWen
    Oct 13 '20 at 10:02
  • \$\begingroup\$ I'm pretty sure this should be good now \$\endgroup\$
    – LiefdeWen
    Oct 14 '20 at 8:22

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.