14
1
I've been recently playing through 'The Weaver' and I think it presents an interesting challenge for code-golf.
Premise:
The Weaver is a game wherein you are given a number of ribbons coming from 2 directions 90 degrees apart and your goal is to swap them at certain intersections to achieve a desired output.
Like this: This is a swap: This isn't:
Input:
3 arrays:
- Top ribbons (left to right)
- Left ribbons (top to bottom)
- The coordinates of the intersections to swap
Output:
2 arrays:
- Bottom ribbons (left to right)
- Right ribbons (top to bottom)
Examples:
I'll use the above image as the first example:
Input: [r, y, b], [r, y, b], [(0, 1), (2, 1), (2, 2)]
What happens:
r y b
r y b
r r r r•y y y y
r r b
y y y y y y y y
r r b
b b b b•r r•b b
r b r
r b r
Where •
represents a swap.
Output: [r, b, r], [y, y, b]
Input: [a, b, c], [d, e, f], [(0, 0), (2, 1)]
What happens:
a b c
a b c
d d•a a a a a a
d b c
e e e e e e e e
d b c
f f f f•b b b b
d f c
d f c
Output: [d, f, c], [a, e, b]
Input: [a, b], [a, b, c], [(0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 1)]
What happens:
a b
a b
a a a a•b b
a a
b b•a a•a a
b a
c c•b b•a a
c b
c b
Output: [c, b], [b, a, a]
Notes:
- The examples show coordinates given as
(row, column)
though you may take them as(column, row)
. - The top row and left column may have ribbons of the same color
- The board can be rectangular
- All coordinates will be non-negative (
>=0
) (or strictly positive (>=1
) if you choose 1-indexing) - Ignore any swaps that are outside the board
- You may choose to work with letters (
[a-zA-Z]
), integers ([0-9]
) or both - The ribbons in your output must match the ribbons in the input exactly (
a -> a
) - You may assume the list of swaps is sorted in any way you want, as long as it's consistent (if you do, please specify how it should be sorted)
- You may take the swap coordinates as 0 or 1-indexed
- Default loopholes are forbidden
More examples:
Input:
[b], [r], []
Output:
[b], [r]
Input:
[b], [r], [(0, 0)]
Output:
[r], [b]
Input:
[r, p, y], [r, y, p], [(0, 0), (1, 2), (2, 1), (3, 2)]
Output:
[r, p, y], [r, y, p]
Input:
[b, y, o, r],
[r, o, b, y],
[(0, 0), (2, 0), (3, 2)]
Output:
[b, y, y, r],
[b, o, r, o]
The last example relates to this case (if that makes it easier to visualize):
This is code-golf so the shortest answer in bytes for each language wins.
1Re "Ignore any swaps that are outside the board" - does that mean we cannot assume that all swap coordinates are on the board, and we need to filter them for validity (ignore the invalid ones), or does that mean that we can ignore the case of coordinates being outside of the board because the input will always be valid? – Bergi – 2018-04-19T21:05:01.827
@Bergi it means that the input may include swaps outside the board and you have to filter or ignore them. (The 3rd example includes such a swap) – Asone Tuhid – 2018-04-19T21:10:34.577
Oh. I think the challenge would have been more interesting if the swaps would only have had valid coordinates, but that we could not assume they were sorted in an order that fits our solution. – Bergi – 2018-04-19T21:13:21.827
@Bergi maybe, but if that was the challenge, most answers would simply sort the list (which is a boilerplate action just like filtering invalid coordinates). Personally, I think that filtering (or ignoring) invalid coordinates can be done more cleverly than sorting. – Asone Tuhid – 2018-04-19T21:18:41.843
Yeah, they're both boilerplate, so I would have expected both or neither. Depending on the language, sorting takes some cleverness or many bytes as well - and I would have loved to see clever algorithms that don't require a particular order :-) – Bergi – 2018-04-19T21:29:19.350
Regarding invalid coordinates, can they be negative (or
0
in 1-indexed solutions)? – Bergi – 2018-04-19T21:30:23.5171@Bergi You're probably right, well, it's too late to change now. And no, all coordinates will be positive, I'll update the question. – Asone Tuhid – 2018-04-19T21:34:35.983
"positive" is the same as ">0". Maybe you meant this? – user202729 – 2018-04-20T01:19:18.733
@user202729 No, I mean positive (
>=0
) if you take. 0-indexed coordinates and (>0
) otherwise. Or in other words, no coordinate will be above or left of the board. – Asone Tuhid – 2018-04-20T04:33:33.833No, >=0 is non-negative. According to this.
– user202729 – 2018-04-20T05:13:03.457Missing comma in first example input – Sunny Patel – 2018-04-20T14:02:00.877
1@AsoneTuhid If coords are (row,col), it makes more sense to i/o the left ribbons first and the top ribbons second. Is that allowed? – ngn – 2018-04-23T05:27:16.320
@ngn I'm leaning towards no. Would it significantly simplify your solution? – Asone Tuhid – 2018-04-23T05:44:35.167
@AsoneTuhid It would save some precious bytes. – ngn – 2018-04-23T05:49:39.093
@ngn Alright, it was probably an unnecessary restriction anyway. – Asone Tuhid – 2018-04-23T05:59:36.330
@AsoneTuhid Thank you! I think it makes more sense this way - axes come in a consistent order. – ngn – 2018-04-23T06:02:58.770