Be the First 1 (leave only the first Truthy)
Python 2, 35 bytes
while 1:b=input();print b;True&=b<1
Try it online! Input and output are lines of True/False.
Based on Dennis's solution. Redefines the variable True
to be False
after a True
input is encountered. That way, any further inputs of True
will evaluate to False
and be printed as such. Python 3 no longer allows True
and False
to be redefined, so this answer uses Python 2.
The redefinition is True&=b<1
, i.e. True = True & (b<1)
. When the input b
is True
, then (b<1)
is False (since True==1
), so True
becomes False
. See Kevin Cruijssen's answer for a shorter way to handle this redefinition.
APL, 2 bytes
<\
Evaluates to the function "scan using less-than". Try it online!
Explanation
In APL, the operator \
(scan) reduces each nonempty prefix of an array from the right using the provided function.
For example, given the array 0 1 0
, it computes 0
(prefix of length 1), 0<1
(prefix of length 2) and 0<(1<0)
(prefix of length 2) and puts the results into a new array; the parentheses associate to the right.
Reducing by <
from the right results in 1
exactly when the last element of the array is 1
and the rest are 0
, so the prefix corresponding to the leftmost 1
is reduced to 1
and the others to 0
.
Aceto, 19 17 bytes
New version (17 bytes):
This new version takes the characters one at a time and is best executed with the -F
option. It works similar, but not identical to the previous solution:
>,
Op0
p|1u
,ip^
Old answer (19 bytes):
|p1u
iOp<
|!`X
rd!r
This is the first Aceto answer that highlights what it can do relatively well, I would say. The "lists" are input streams, with one input per line, "1" for true, and "0" for false, with an empty string signifying the end of the list.
Aceto programs run on a Hilbert curve, starting on the bottom left, and ending on the bottom right. First, we r
ead a string, d
uplicate, and negate (!
) it, turning empty strings into True, everything else into False. Then there's a conditional horizontal mirror (|
): If the top element on the stack is truthy, mirror horizontally. This happens when the string was empty. If we do the mirroring, we land on the X
, which kills the interpreter.
Otherwise, we convert the remaining copy on the stack to an i
nteger and do another conditional horizontal mirror: This time, because 1 is truthy and 0 is falsy, we mirror if we see the (first) true value. If we don't mirror (so we saw a 0) we p
rint what's on the stack (since the stack is empty, a zero) and jump to the O
rigin of the curve, where we started, starting the whole process again.
Otherwise, when we saw a 1, we mirror and land on the u
, which reverses the direction we move on the Hilbert curve. 1p
prints a 1, and now we go on the same O
we would have gone if we had seen a 0, but since we're in "reversed mode", our origin is at the bottom right, so we jump there.
Now we r
ead another string, and negate it. If the string was empty, and therefore the top stack element is truthy, `
will not escape the next command (X
), making us quit.
Otherwise (if the string wasn't empty), we do escape the X
and ignore it. In that case, we go to the left (<
), p
rint 0 (because the stack is empty), and jump back to the O
rigin.