Introduction
Given a set of text-based "screenshots" consisting of printable ASCII chars merge them so that all of them form one long screenshot so that nobody has to do it themselves when reading it.
Challenge
Take input as a list of strings (referred to as "screenshots"). Each screenshot consists of several lines of text.
To merge two screenshots:
Starting from the first line, check if that line occurs in the second screenshot
- If it does, cut the first screenshot so it ends at this duplicated line and cut the second screenshot so it begins one line after the duplicated line
- Concatenate these two together with the cuts
If they do not have a line of text in common, just append the second screenshot to the first (starting on a new line).
Your output should be a single string merging all of the screenshots together. Screenshot merging is not associative, so you should merge in the following order: merge the first and the second screenshot, then the resulting screenshot with the third, then the result with the fourth screenshot, and so on, until only one screenshot is left.
Example with explanation
Input:
Screenshot 1:
> hi
hi <
> how are you doing
good <
Screenshot 2:
hi <
> how are you doing
good <
> good
yes <
Output:
> hi
hi <
> how are you doing
good <
> good
yes <
Explanation:
Going from top to bottom, find the first line on both the first and second screenshot
> how are you doing
Take the first half of this screenshot above and including the line
> hi
hi <
> how are you doing
--remove everything below--
Find the first line where it is duplicated in the next screenshot and cut one below there
--remove everything including and above--
good <
> good
yes <
Concatenate the two sections
> hi
hi <
> how are you doing
--join them together--
good <
> good
yes <
Test Cases
Input:
line 1
line 2
line 3
line 2
line 3
line 4
Output:
line 1
line 2
line 3
line 4
Input:
line 1
line 2
line 10
line 10
line 2
line 3
Output:
line 1
line 2
line 3
Note that even though line 10 is duplicated, line 2 is duplicated first and split there.
Input:
words
really long repeated words
456
hi
really long repeated words
hmm
really long repeated words
kthxbye
Output:
words
really long repeated words
hmm
really long repeated words
kthxbye
Note that
really long repeated words
appears twice in the result because the one in the first screenshot and the other in the second screenshot is included
Input:
======= 10:42 ========
~~~ p1: hi
hi :p2 ~~~
~~~ p1: yes
no :p2 ~~~
v ----------- v
======= 10:43 ========
no :p2 ~~~
~~~ p1: why?
because :p2 ~~~
~~~ p1: ok fine
v ----------- v
======= 10:44 ========
because :p2 ~~~
~~~ p1: ok fine
~~~ ~~~
~~~ ~~~
v ----------- v
Output:
======= 10:42 ========
~~~ p1: hi
hi :p2 ~~~
~~~ p1: yes
no :p2 ~~~
~~~ p1: why?
because :p2 ~~~
~~~ p1: ok fine
~~~ ~~~
~~~ ~~~
v ----------- v
Note that the clock increases in time meaning that the clock is not the first duplicated input. Because it is before the first duplicated input, it gets ignored the second and third time.
Edge Case Input:
> hello
oh hi <
> how are you doing
good <
Output:
> hello
oh hi <
> how are you doing
good <
Note that nothing is duplicated here so just append them together
As this is code-golf, smallest submission in bytes wins.
1
/2
,2
/1
, and3
/2
, merging them left-to-right yields1
/3
/2
and merging them right-to-left yields1
/2
. \$\endgroup\$