| Bytes | Lang | Time | Link |
|---|---|---|---|
| 006 | Japt P | 240613T181819Z | Shaggy |
| 059 | Python 3.8 prerelease | 240613T143959Z | Jitse |
| 028 | Perl 5 + p0 | 231201T195956Z | Dom Hast |
| 016 | Uiua | 231130T100856Z | chunes |
| 155 | R | 240612T154755Z | int 21h |
| 096 | Desmos | 231215T121013Z | Aiden Ch |
| 059 | GNU sed E | 231208T133244Z | Philippo |
| 159 | Python | 231204T083551Z | Gáb |
| 099 | Bash | 231203T144905Z | David G. |
| 060 | C clang | 231130T162120Z | AZTECCO |
| 033 | Ruby | 231130T080416Z | G B |
| 012 | BQN | 231201T021408Z | att |
| 097 | Noulith | 231130T215510Z | bigyihsu |
| 032 | J | 231129T234028Z | Jonah |
| 133 | Go | 231130T213004Z | bigyihsu |
| 021 | x8664 machine code | 231130T211102Z | m90 |
| 048 | JavaScript Node.js | 231130T003103Z | l4m2 |
| 005 | 05AB1E | 231130T074310Z | Kevin Cr |
| 035 | Charcoal | 231130T005533Z | Neil |
| 026 | Retina 0.8.2 | 231130T001751Z | Neil |
| 062 | JavaScript ES11 | 231129T230457Z | Arnauld |
| 026 | Wolfram Language Mathematica | 231129T231707Z | att |
| 275 | Vyxal s | 231129T224724Z | lyxal |
| 003 | Jelly | 231129T224331Z | Jonathan |
Japt -P, 6 bytes
ËòÀÃÕc
ËòÀÃÕc :Implicit input of string array
Ë :Map
ò : Partition between characters
À : That are not equal
à :End map
Õ :Transpose
c :Flatten
:Implicitly join and output
Python 3.8 (pre-release), 59 bytes
f=lambda a,b:a+b and a[:a.rfind(s:=a.lstrip(a[:1]))]+f(b,s)
Uiua, 16 bytes
/◇⊂♭⍉⬚∘⊟□[]∩⊜□∩.
Takes lists of postive integers as input (because this saves a few bytes over strings).
∩.duplicate both inputs
∩⊜□split both into contiguous equal elements
⬚∘⊟□[]couple lists to matrix, filling with boxed empty list
⍉transpose
♭deshape
/◇⊂join all
R, 155 bytes
\(r,s,`!`=length,`?`=rep,`/`=\(x,n)rle(el(strsplit(x,"")))[[n]],`*`=\(x,n)c(x/n,0?max(!r/1,!s/1)-!x/1))o=Map(\(a,b,d,e)cat(a?b,d?e,sep=""),r*2,r*1,s*2,s*1)
The solution is based on rle function ("run length encoding") which transforms a character vector into two vectors by counting and removing the repeated adjacent characters.
The smaller vectors output by rle are matched with 0's in order to have the same length as the larger ones and to avoid the recycling of the characters. Then the elements of the both character vectors
are restored back to their original lengthes with the rep
and output one after one in the zip-lock fashion. Note that added extra elements rep("0",0) result in character(0) which are ignored by cat.
Desmos, 96 bytes
C=sgn(L[2...]-L)^2
g(L)=∑_{n=1}^{[1...C.count]}C[n]
f(A,B)=sort(join(A,B),join(0,g(A),0,g(B)))
Ok this can probably be improved somehow but for now this is good enough.
GNU sed -E, 63 59 bytes
The first approach did build a third string behind the two strings to be merged, but it turned out it's cheaper to invest in introducing a mark _ in the first string marking the position where to insert the chars from the second string:
s/^/_/
:1
s/_((.?)\2*)(.*,)((.?)\5*)/\1\4_\3/
/_,/!b1
s///g
The magic mainly takes place in the third line: The ERE (.?)\2* (and the same with 5) match zero or more occurences of the same character (using a backreference, which is not required for ERE by POSIX, but supported by the GNU implementation. Thus, we move those matches in front of the _.
We can't use t for looping, because an empty substitution is always possible, but that's not too expensive, because the addess pattern _, can be reused for removing.
Python, 160 159 bytes
from itertools import*
c,g,l=chain.from_iterable,groupby,list;f=lambda a,b:''.join(c(c(zip_longest([l(t)for r,t in g(a)],[l(t)for r,t in g(b)],fillvalue=''))))
Edit 1: removed an extra space
Bash, 103 99 bytes
The function. Input is parameters 1 and 2. Output is variable o.
f(){
o=
while [ "$1$2" ]
do
c=`expr "$1" : "${1::1}*"`
o+=${1::$c}
set -- "$2" "${1:$c}"
done
}
Uses expr to determine the repeats of the first character, "outputs" the repeats, swaps the strings, and repeats until empty.
Due to limitations in expr, it can't handle characters [, \, or ^, or trailing single parentheses. All of these cause infinite loops, possibly with errors spewing. See the TIO for details.
I could save two characters by removing the quotes on the while statement, but this would make space not be a valid character.
C (clang), 61 60 bytes
f(a,b)char*a,*b;{*a?putchar(*a)-*++a?f(b,a):f(a,b):puts(b);}
- saved 1 Byte thanks to @l4m2
Ruby, 33 bytes
f=->a,b{a[/(.)\1*/]?$&+f[b,$']:b}
How it works:
If the first queue is not empty, extract the first vehicle, then exchange the queues, and repeat.
If the first queue is empty, return the whole second queue.
BQN, 12 bytes
∾∾○(+`»⊸≠)⊔∾
Input 𝕨F𝕩. Assumes the leading character isn't a null character.
traffic jaaam
∾ join trafficjaaam
⊔ group by
+`»⊸≠ vehicle #s
∾○ (joined) 123445612223
tj raaa am ff i c
∾ join groups tjraaaamffic
Flattened group-by (∾⊔) is a little shorter than sort-by (⍋⊸⊏).
Noulith, 97 bytes
f:=\a,b->if (a.len<1)b else (c,d:=a.first,a.tail;c$if (d.len>0 and c==d.first)f(d,b)else f(b,d))
Explanation
f:=\a,b -> # recursive function
if (a.len<1) b # return early when first input is empty
else ( # otherwise
c,d:=a.first,a.tail; # get first c(h)ar and the rest
c $ # concat the first c(h)ar with
if (d.len>0 and c==d.first) # if next is a truck
f(d,b) # the rest of the truck
else f(b,d)) # otherwise the next lane
J, 32 bytes
[:;(,/:,&(#\))&(<;.1~2~:/\'_'&,)
The 2nd part of this is the only interesting insight:
[:;...&(<;.1~2~:/\'_'&,)This is the preprocessing step that breaks the lists down into the grouped sections. A standard J idiom which is unfortunately a bit verbose. I could save two bytes by using integers instead of strings as input.,/:,&(#\)The main logic. Since we can't zip uneven lists in J using,., we just cat the lists,and then sort them/:by1..ncatted with1..m(wherenandmare the two list lengths). This works because sort is stable.
Go, 133 bytes
func f(a,b string)string{if len(a)<1{return b}
c,a:=a[0],a[1:]
if len(a)>0&&c==a[0]{return string(c)+f(a,b)}
return string(c)+f(b,a)}
Recursive function.
Explanation
func f(a,b string)string{
if len(a)<1{return b} // return early when first input is empty
c,a:=a[0],a[1:] // get the current c(h)ar
if len(a)>0&&c==a[0]{ // if the next c(h)ar is the same...
return string(c)+f(a,b)} // recurse to get the truck
return string(c)+f(b,a)} // otherwise move on to the next lane
x86-64 machine code, 21 bytes
AA 3A 06 74 03 48 87 D6 AC 84 C0 75 F3 52 5E 3A 06 A4 75 FB C3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address at which to place the result, as a null-terminated byte string, and in RSI and RDX, addresses of the inputs, as null-terminated byte strings. The starting point is after the first 8 bytes.
In assembly, rearranged for easier reading:
f: lodsb # Load a byte from address RSI into AL, advancing the pointer.
test al, al # Check the value of that byte.
jnz r # Jump if it's nonzero.
(This section actually comes first.)
r: stosb # Write that byte to the output string, advancing the pointer.
cmp al, [rsi] # Compare that byte to the next byte in the string.
je f # Jump if they are equal.
xchg rsi, rdx # (Otherwise) Swap the pointers (in RSI and RDX).
(From here, continue at the beginning.)
(When the byte is zero, continue to here.)
push rdx; pop rsi # Transfer the other pointer from RDX to RSI.
r1: cmp al, [rsi] # Compare the value at that address to AL, which is 0.
movsb # Write the byte from address RSI to the output string,
# advancing both pointers.
jne r1 # Jump back if the byte was not equal to 0.
ret # Return.
JavaScript (Node.js), 52 48 bytes
f=([c,...a],b)=>c?[c,...c==a[0]?f(a,b):f(b,a)]:b
Array IO, though inputting string usually work
05AB1E, 5 bytes
€γõζS
Input as a pair of strings; output as a list of characters.
Try it online or verify all test cases.
Explanation:
€ # Map over both strings in the (implicit) input-pair:
γ # Split them into equal adjacent groups of substrings
ζ # Zip/transpose this list of lists,
õ # using an empty string ("") as filler for unequal length lists
S # Convert this list of pairs of strings to a flattened list of characters
# (which is output implicitly as result)
Charcoal, 35 bytes
F²⊞υ⪪⮌S¹W∧⌊υ⊟§υ⁰«ι¿⌕⮌§υ⁰ι≔⮌υυ»W⌈υ⊟ι
Try it online! Link is to verbose version of code. Explanation: Seems to be a similar idea to @l4m2's answer, but it just took me that long to think of it after writing my Retina answer that he got there first.
F²⊞υ⪪⮌S¹
Input the two strings, reversing them and splitting them into characters so that they can be processed more easily.
W∧⌊υ⊟§υ⁰«
Until one queue is empty, remove the next character from the first queue, ...
ι
... print it, and...
¿⌕⮌§υ⁰ι
... if the next character in this queue is from a different vehicle, then...
≔⮌υυ
... switch the queues.
»W⌈υ⊟ι
Output the remaining vehicles from the other queue.
Retina 0.8.2, 26 bytes
O$#`.(?<=(\2*(.))+)
$#1
¶
Try it online! Takes input on separate lines but link is to test suite that splits on commas for convenience. Explanation:
O$#`.(?<=(\2*(.))+)
$#1
Sort each character by its vehicle index in its queue. This is a stable sort so that the first vehicle of the first queue sorts first followed by the first vehicle of the second queue etc. The newline is not considered for the sort but it's used by the lookbehind to find the front of the second queue.
¶
The vehicles were sorted without regard for the placement of the newline which is just unnecessary at this point.
JavaScript (ES11), 62 bytes
Expects ([str1, str2]).
a=>[...a+a].map((_,i)=>a[i&1].match(/(.)\1*/g)?.[i>>1]).join``
Wolfram Language (Mathematica), 26 bytes
Flatten[Split/@#,{2,1,3}]&
Input and output lists of characters.
Vyxal s, 22 bitsv2, 2.75 bytes
vĠ∩
Bitstring:
1000110100011111100101
The footer inserts a newline for test case purposes. Port of the jelly answer.
Explained
vĠ∩
vĠ # Vectorise group-by
∩ # Transpose
💎
Created with the help of Luminespire.
Jelly, 3 bytes
ŒgZ
A full program that accepts a pair of the two strings as the first argument and prints the resulting single lane.
How?
ŒgZ - Main Link: pair of lists of characters
Œg - group runs of equal elements of each
Z - transpose
- implicit, smashing print





