# Be the First 1 (leave only the first Truthy)

### Intro

Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. This is a language agnostic edition of this year's eighth problem.

I have explicit permission to post this challenge here from the original author of the competition. Feel free to verify by following the provided link and contacting the author.

### Problem

Given a Boolean* list, "turn off" all the Truthies after the first Truthy.

No Truthies? No problem! Just return the list unmodified.

### Examples

[falsy,truthy,falsy,truthy,falsy,falsy,truthy][falsy,truthy,falsy,falsy,falsy,falsy,falsy]

[][]

[falsy,falsy,falsy,falsy][falsy,falsy,falsy,falsy]

* All your truthies must be identical, and all your falsies must be identical. This includes output.

• Can we use bit lists or other truthy/falsy list representations that are more natural in our language of choice? – Martin Ender May 7 '17 at 20:32
• Well yeah, if you talk about "truthy" and "falsy" in the challenge instead of "booleans", "true" and "false". ;) – Martin Ender May 7 '17 at 20:36
• I'm not clear on the booleans. Can we use 0/1 even if our language has True/False? – xnor May 7 '17 at 20:59
• @xnor Ah, good point. I think it would be fair to allow choosing input, but output must match, don't you think so? – Adám May 7 '17 at 21:12
• @xnor I hear you, but if Haskell cannot treat numbers as Booleans, or cannot do arithmetic on Booleans, then that is a real limitation in the golfing power of Haskell, and ought to be reflected in the byte count by necessitating conversions or other work-arounds. What do you think of the footnote formulation? – Adám May 7 '17 at 21:19

# Cubix, 14 bytes

W;@.1I?>O;w..W


Try it online

Cubix doesn't have proper lists or booleans, so we take the input as a sequence of space-separated 1s and 0s terminated with a -1.

Unfolded

    W ;
@ .
1 I ? > O ; w .
. W . . . . . .
. .
. .


### Explanation

The instruction pointer starts at the top of the left face of the cube, moving to the right.

Initially, we push a 1 onto the stack. We then take one input at a time, with I and branch with ?.

If the input is 0, we see O;, which outputs 0 and pops it from the stack.
If the input is 1, we see ;O;, which pops the 1 from the stack, outputs the top of the stack (which will be 1 the first time around), then pops it from the stack.
If the input is -1, we see @, which ends the program.

# Ruby, 32 bytes

->a{i=1;a.map{|x|x&&[i,i=p][0]}}


Uses 1 for truthy and nil for falsy.

### Explanation

->a{           # take an array a
i=1;       # use i to keep track of if we've gotten a truthy value yet
a.map{|x|  # for each element x in a:

x&&[     # if x is truthy:
i,     # remember the old i,
i=p    # then set i to false
][0]      # and replace x with the old i
}
}


# x86 assembly instructions, 12 bytes

31 c0 0f bd cf 74 04 ff c0 d3 e0 c3


Or in gcc assembly:

    .globl  f
f:
xor     %eax, %eax
bsrl    %edi, %ecx
je  .L2
inc     %eax
sall    %cl, %eax
.L2:
ret


This is a translation of my c answer and has the same I/O specs.

# vim, 19 keystrokes

2/1<ENTER>i<ENTER><ESC>:s/1/0/g<ENTER>kJx


2/1<ENTER>i<ENTER><ESC> puts everything after the first 1 to the second line and moves the cursor to that line. :s/1/0/g<ENTER> replaces the 1s with 0s. kJx then merges the lines.

# Perl 6, 19 bytes

{@_ «&&»[\^^] @_}


This is an anonymous function that takes its arguments in @_. ^^ is the exclusive-or operator, and [\^^] does a scan using that operator, returning a copy of the input list where the first truthy value is replicated until the second truthy value, whereupon it and all remaining values become Nil (which is falsy). To falsify the replicated copies of the first truthy value, if any, the list is combined with the original using &&, the boolean and operator.

## Javascript, 25 bytes

x=>x.map(c=>c&&x&&!(x=0))

• x holds the original array.
• Once the first truthy has been found, x is overwritten with a false value.
• This makes c && x return false for all values except the first truthy.

# Scala, 44 bytes

s=>{var b=1>2;s.map{x=>if(b)!b else{b=x;x}}}


# PowerShell, 29 bytes

$args|%{$_-and!$b;$b=$b-or$_}


Try it online!

### OCaml, 85 60 bytes

let rec f?(b=true)=function h::t->(h&&b)::(f~b:(h<b)t)|_->[]


Ungolfed

let rec f ?(b=true) = function
| _            -> []


Explanation

f is defined as a recursive function taking an optional (?) boolean b and an unnamed list (function) and return according to the cases:

• if the first element of the list is false, returns it unchanged,
• if the first element if the list is true, sets it to b and flip b to false (id est, only let unchanged the first true since b is true by default and then set to false),
• if the list is empty (end of recursive call), returns the empty list.

Usage

Try it online (you'll need to copy/paste the function definition) !

# f [];;
- : bool list = []
# f [false];;
- : bool list = [false]
# f [true];;
- : bool list = [true]
# f [false;true;true;false;true;true;false;true;false;false];;
- : bool list =
[false; true; false; false; false; false; false; false; false; false]


History

25 (yes, twenty-five) bytes golfed off by Ørjan Johansen, suggesting to merge the first two cases (see explanation).

• You can merge the first two cases: let rec f?(b=true)=function c::t->(c&&b)::(f~b:(c<b)t)|_->[]. – Ørjan Johansen May 10 '17 at 17:03
• @ØrjanJohansen Thanks, that's 25 bytes golfed! – YSC May 11 '17 at 15:40

# PHP, 34 bytes

foreach($_GET as$g)echo$f?0:$f=$g;  Try it online! # F#, 7976 72 bytes let rec f o=function|[],_->o|x::y,1->f(o@[0])(y,1)|x::y,_->f(o@[x])(y,x) Try it online! ## Usage let input = [0;0;0;0;1;1;0;0;1] printfn "%A" f [] (input, 0)  ## Explanation This is a very straightforward implementation. f is a function with two arguments, first being the result list and the second a tuple of input and a value indicating if a truthy value has been found. Note: only 1 is considered truthy, every other number is falsy. Which sounds weird now that I think about it. This however, can easily be changed so that any value <> 0 is truthy. But I think it should be ok the way it is, as expected input is only 0 or 1 // int list -> int list * int -> int list let rec f output = function | [], _ -> output // return result | x::xs, 1 -> f (output@[0]) (xs,1) // if truthy was encountered before, append 0 to result, process rest of input | x::xs, _ -> f (output@[x]) (xs,x) // if not, append 0 or 1 to result, process rest of input  # PHP, 61 Bytes $n=[];foreach($_GET as$v)$n[]=$n&&max($n)?0:$v;print_r($n);  Try it online! # Japt, 7 bytes ®?!T°:Z  Try it online! # Idris, 98 bytes The code is a bit longer, but on the plus side you get the compile time guarantee that the output has the same size as the input! import Data.Vect f:Vect n Bool->Vect n Bool f[]=[] f(x::y)=x::if x then replicate _ False else f y  I'm going to give an extensive explanation to have you understand the basic workings of Idris. ### Explanation import Data.Vect  The type Vect : Nat -> Type -> Type is not imported by default f : Vect n Bool -> Vect n Bool  Contrary to Haskell, Idris uses a single colon : to specify types and also requires you to specify the type, as the dependent type checker can't possibly infer it in all cases. Since Vect takes a Nat (natural number) and a Type, we provide just that, n being a natural number and Bool being the type. Even though we didn't specify the type of n explicitly, Idris can infer it to be Nat. Since n occurs both in the argument and the resulting type, they need to be the same. f [] = []  As the base case, the empty list returns the empty list. This is the only possible implementation, anything else such as f[]=[True] would give a compiler error, since the type of [] is Vect 0 Bool but the type of [True] is Vect 1 Bool. f (x :: y) = x :: if x then  :: is used as the cons operator in Idris, similar to : in Haskell. We put the first element in the resulting list, unchanged.  replicate _ False else  If the first element is true, we want the rest of the list to be false in any case, so we just replicate the value False. Since Idris knows that the rest of the list has to have the same size as y, it can infer the value of _ (namely length y).  f y  If the first element is false, we just move on by recursively calling the function on the rest. ## 77 Bytes, Javascript var s=0;[0,0,0,1,0,0].forEach(function(c,i,a){if(c||s)s=c=1;console.log(c)}); I/O are zeros and ones. # F#, 88 bytes let c2 u= let n=ref(1<0) [for i in u do if !n||not i then yield 1<0 else n:=i;yield i]  Use as follows: let mutated = c2 [true; true; false; false; true]  or printfn "%A" (c2 [true; false; true; false; true])  # AWK, 31 bytes BEGIN{ORS=RS=" "}$1{$1=t-->-1}1  Try it online! Assumes input is space-separated 1 and 0. Most of the code is just splitting the records on for input and output. The variables could be assigned via command-line options, but it only saves a couple bytes and TIO doesn't count them. :( BTW BEGIN{ORS=RS=" "}$1&&t++{$1=0}1  also works, but I find it a bit harder to read and has the same byte-count, anyway. # Chip, 10 bytes ,. z. a\A  Try it online! ### How? The elements a\A will copy the lowest bit of input (A) to the lowest bit of output (a) so long as the switch is inactive (\). When A is powered, due to a truthy input, it activates the delay element (z) via a wire (.). This delay element will wait one cycle -- that is, until the next input byte -- at which point it will send the signal onward. This activates the switch, cutting off the input from the output, and via some more wires ( , .) activates itself for the next cycle. If you change the normally-closed switch \ for a normally-open switch /, you'd get a circuit that filters out only the first truthy value. # ReRegex, 18 bytes 1(0*)1/1$10/#input


Fairly simple solution, ReRegex takes all pairings of 1(0*)1, which is two 1s with any amount of 0s in between, and just leaves the first 1, replacing the second with a 0. As ReRegex keeps running the regex until no more change happens, which satisfies the challenge.

Try it online!

• This solution removes ones from the input, but I believe the challenge asks to turn them into zeros. This can be fixed by adding a final 0 to the substitution pattern – Leo May 8 '17 at 9:40

# Retina, 16 bytes

+T10(?<=1)0*1


Try it online! My first Retina submission ever.

• 0*1 can be replaced with .* – eush77 May 20 '17 at 22:38

# C++, 77 75 bytes

Yay pointers and memset!

void f(bool*a,int b){bool*c=&a[b];while(a<c)if(*a++)break;memset(a,0,c-a);}


Function takes in a pointer to a bool array, and the length of that array as parameters. It loops through the array until it finds the first true, increments one more cell, and memsets the remainder of the memory from that pointer until the end. 0 length arrays work too since the loop is skipped entirely and the memset gets 0 bytes as the length, however passing in null instead of an empty array will break things.

Ungolfed + tests

#include <iostream>
#include <cstdlib>
#include <cassert>

//void f(bool*a,int b){bool*c=&a[b];while(a<c)if(*a++)break;memset(a,0,c-a);}

void f(bool* a, int b)
{
bool* c = &a[b];
while (a < c)
if (*a++)
break;
memset(a, 0, c - a);
}

void print(bool* a, int b)
{
for (int i = 0; i < b; i++)
{
std::cout << a[i] << '\t';
}
std::cout << std::endl;
}

int main()
{
size_t s = sizeof(bool);
assert(s == 1);
bool* t1 = new bool[5] { false, false, false, false, false };
bool* t2 = new bool[0];
bool* t3 = new bool[6] { false, false, true, false, true, true };
bool* t4 = new bool[10] { true, true, true, true, true, true, true, true, true, true };
f(t1, 5);
f(t2, 0);
f(t3, 6);
f(t4, 10);
print(t1, 5);
print(t2, 0);
print(t3, 6);
print(t4, 10);
delete[] t1;
delete[] t2;
delete[] t3;
delete[] t4;
}


# Befunge-98, 19 bytes

&:1#@_1*:.70g\-70p


Try it online!

Input: stream of numbers, 0 (false), 1 (true), or 2 (EOF).

Output: stream of numbers, 0/1.

## Explanation

The program works by reading the next number in a loop, multiplying it by a constant, and printing the result to stdout. The operation described in the problem statement is performed by modifying that constant:

&:1#@_1*:.70g\-70p
^          v
[ P[7]-= x ]


# CJam, 10

{_1#\,,f=}


Try it online!

Explanation:

This is a function that takes an array of 0 and 1 from the stack, and pushes the resulting array on the stack.

_      duplicate the array
1#     find the index of the first 1 (it is -1 if not found)
\      swap with the other copy of the array
,      get the array length, let's call it n
,      make an array [0 1 … n-1]
f=     compare each element with the index we got earlier


## PowerShell, 25 bytes

$args|%{$_-and!$f;$f+=$_}  Previous "non legal" version {$_*!$f;if($_){$f=1}}  The one more version with the same length of 25 bytes $args|%{!!$f-lt$_;$f+=$_}


Try it online!

• Is this a legal answer? I am honestly not sure. You cannot run this on its own as a function or program. It is a process scriptblock for the foreach-object cmdlet. I would have expected you to use \$args` – Matt May 8 '17 at 18:34
• @Matt I would say no, it's not legal. – briantist May 9 '17 at 20:05
• @briantist Well that is good then. I don't feel the need to go back to my old answers now. – Matt May 9 '17 at 20:06
• – briantist May 9 '17 at 20:08