Read a date in xkcd notation In his xkcd about the ISO 8601 standard date format Randall snuck in a rather curious alternative notation:



The large numbers are all the digits that appear in the current date in their usual order, and the small numbers are 1-based indices of the occurrences of that digit. So the above example represents 2013-02-27.

Let's define an ASCII representation for such a date. The first line contains the indices 1 to 4. The second line contains the "large" digits. The third line contains the indices 5 to 8. If there are multiple indices in a single slot, they are listed next to each other from smallest to largest. If there are at most m indices in a single slot (i.e. on the same digit, and in the same row), then each column should have be m+1 characters wide and left-aligned:

2  3  1  4
0  1  2  3  7
5     67    8


See also the companion challenge for the opposite conversion.

The Challenge

Given a date in xkcd-notation, output the corresponding ISO 8601 date (YYYY-MM-DD).

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

You may assume that the input is any valid date between years 0000 and 9999, inclusive.

There won't be any leading spaces in the input, but you may assume that the lines are padded with spaces to a rectangle, which contains at most one trailing column of spaces.

Standard code-golf rules apply.

Test Cases

2  3  1  4
0  1  2  3  7
5     67    8
2013-02-27

2  3  1     4
0  1  2  4  5
   5  67 8
2015-12-24

     1234
1    2
5678
2222-11-11

   1     3  24
0  1  2  7  8
57    6     8
1878-02-08

2   4   1   3
0   1   2   6
5       678
2061-02-22

      1 4 2 3
0 1 2 3 4 5 6 8
6 5 7         8
3564-10-28

1234
1
5678
1111-11-11

1 2 3 4
0 1 2 3
8 5 6 7
0123-12-30
CJam, 35 bytes

ll{1$e&gt;}*](l+eeWf%$-8&gt;Wf=\f=2/(o'-*


Try it here. It expects the input lines to be padded with spaces.

Explanation

ll reads two lines of input, and {1$e&gt;}* performs a "scan" on the second one: it it takes all prefixes of its input and computes the maximum of each prefix. For the input line "0  1  2  7  8", this pushes "0001112227778". Our stack now looks like this:

"first line" '0 '0 '0 '1 '1 '1 ...


We need to re-capture the values into a list ourselves using ]; this captures our first line as well, so we pop it back out using (, to get

"0001112227778" "first line"


as expected.

eelee+ enumerates this line, then does the same for a third input line, and concatenates the results, leaving something like this on the top of the stack:

[[0 '5] [1 ' ] [2 ' ] [3 ' ] [4 ' ] [5 ' ] [6 ' ] [7 ' ] [8 '6] [9 '7] [10 '8] [11 ' ] [12 ' ]
 [0 '2] [1 ' ] [2 ' ] [3 ' ] [4 '4] [5 ' ] [6 ' ] [7 ' ] [8 '1] [9 ' ] [10 ' ] [11 ' ] [12 '3]]


Now our stack is ["0001112227778" X] where X is the enumerated list above.

We flip each pair in X (Wf%), sort the pairs lexicographically ($), and leave the last 8 pairs -8&gt;. This gets us something like:

[['1 8] ['2 0] ['3 12] ['4 4] ['5 0] ['6 8] ['7 9] ['8 10]]


This works, because the sort places all the pairs with key ' (space) before all the digits in ascending order.

These are the "x-positions" of the characters 12345678 on the first and third lines: we only need to retrieve the characters from our (modified) second line that are vertically aligned with them.

To do this, we take each position (Wf=), index into the string we made earlier (\f=). We have "20610222" on the stack now: to add the dashes, first we split into segments of length two (2/), print the first segment without a newline ((o) and join the remaining segments with dashes ('-*).

EDIT: cool scan trick, Martin! Saved four bytes.

EDIT 2: saved two more bytes by replacing eelee+ with l+ee; this works, because the lines all have the same lengths, and list indexing in CJam is automatically modulo the list length, so the indices n+0, n+1, n+2... nicely map to 0, 1, 2... 

EDIT 3: Martin saved another byte in the final step of the process. Nice!