Introduction
I have a room full of magic mirrors. They are mysterious artifacts that can duplicate any item, except another magic mirror. More explicitly, a duplicate version of the item will appear on the other side of the mirror, at the same distance. However, if there is another magic mirror in the way on either side, between the duplicating mirror and either item (original or duplicate), the duplicate is not formed. The original item can be either left or right of the mirror, and the duplicate will appear on the other side. Also, the duplicate item can itself be duplicated by another mirror. Items never block the duplication of other items (except by being directly on the position of the would-be duplicate).
Input
Your input is a string consisting of the characters .#|
, which represent empty space, items, and magic mirrors.
There will always be at least one magic mirror in the input.
Output
Your output shall be another string where each magic mirror has duplicated every item it can, according to the rules above. You can assume that there will always be an empty space on the spot where a duplicate item appears (so they will no go out of bounds).
Examples
Consider the input string
.#.|.....|......#
A B C D
where we have marked some positions for clarity.
The mirror B
duplicates item A
, which ends up to its right:
.#.|.#...|......#
A B C D
Mirror C
then duplicates the new item:
.#.|.#...|...#..#
A B C D
Mirror C
cannot duplicate item A
, since mirror B
is in the way.
It also cannot duplicate item D
, since mirror B
is in the way on the other side.
Likewise, mirror B
cannot duplicate item D
or the duplicate next to it, since mirror C
is in the way, so this is the correct output.
For another example, consider the input
.##..#...|#..##...|..##....#.
AB C DE FG H IJ K
Mirror D
can duplicate A
and B
to the right, and E
and G
to the left.
C
and F
are already duplicates of each other.
The string becomes
.##.##..#|#..##.##|..##....#.
AB C DE FG H IJ K
Mirror H
can duplicate E
, F
, and the duplicates of A
and B
to the right, and I
to the left.
G
and J
are already duplicates of each other, and mirror D
is in the way of K
.
Now we have
.##.##..#|#..#####|#####..##.
AB C DE FG H IJ K
Finally, mirror D
can duplicate the duplicate of I
to the left.
We end up with
.#####..#|#..#####|#####..##.
AB C DE FG H IJ K
Rules and scoring
You can write either a full program or a function. The lowest byte count wins. Submissions that don't use regex engines compete separately from those that do, and may be marked with (no regex).
Test cases
"|" -> "|"
"..|.." -> "..|.."
".#.|..." -> ".#.|.#."
"..#|.#." -> ".##|##."
".#..|....|.." -> ".#..|..#.|.#"
".|..|.#....." -> "#|#.|.#....."
"...|.#...|....#" -> ".##|##...|...##"
"......#|......." -> "......#|#......"
".#.|.....|......#" -> ".#.|.#...|...#..#"
".......|...#.##|...." -> "##.#...|...#.##|##.#"
"...#..||.......#..#...#" -> "...#..||.......#..#...#"
".##|.#....||#||......#|.#" -> ".##|##....||#||.....##|##"
".##..#...|#..##...|..##....#." -> ".#####..#|#..#####|#####..##."
".#|...||...|#...|..##...|#...." -> ".#|#..||.##|##..|..##..#|#..##"
"....#.|...#.|..|.|.....|..#......" -> "..#.#.|.#.#.|.#|#|#.#..|..#.#...."
"..|....|.....#.|.....|...|.#.|..|.|...#......" -> ".#|#...|...#.#.|.#.#.|.#.|.#.|.#|#|#..#......"