It’s apparently not super efficient in either space or time, but I wrote this one as a state machine for each input line:

There are two states, ZERO and ONE (standing for the last value read). Transitioning from ZERO to ONE outputs the number of that input line, while any other transition outputs 0. Then, to produce the desired output, all we need to do is add the outputs of all four state machines.