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.