Challenge
Write a program or function that takes in 4 non-negative integers, A, B, C, and D, that represent two fractions, A/B and C/D, where B and D are non-zero and A <= B and C <= D.
Output an ASCII art depiction of the fractions made of |x-
characters on two lines as follows:
The lines will always be the same, minimal length for the input, and both always start and end with
|
.The top line will depict A/B by having B sections of
-
's separated by|
's with A of the sections replaced byx
's as if they are filling up the 1 whole of the full line.Likewise, the bottom line will depict C/D by having D sections of
-
's separated by|
's with C of the sections replaced byx
's as if they are filling up the 1 whole of the full line.
It will probably make the most sense if I show an example. Suppose A/B = 2/5 and C/D = 1/3, then the output should be:
|xx|xx|--|--|--|
|xxxx|----|----|
Here the lines are the same length, as required, and the top one depicts 2 out of 5 sections filled with x
's while the bottom one depicts 1 out of 3 sections filled with x
's.
As another example, A/B = 1/2 and C/D = 5/8 would give:
|xxxxxxx|-------|
|x|x|x|x|x|-|-|-|
Notice how the number of x
's or -
's in each section between |
's is the same in any one line but can vary depending on the fractions and the requirement that the two lines are the same length overall.
In the first example it'd be impossible to just have 1 character between the |
's for 2/5
|x|x|-|-|-| (2/5 with 1 character between bars)
|xx|--|--| (invalid for 1/3 paired with 2/5 since line too short)
|xxx|---|---| (invalid for 1/3 paired with 2/5 since line too long)
but with 2 characters between |
's it works as shown above.
Your program must use the shortest lines possible. This keeps them easy to read.
The cool idea here is that it's super easy to look as these ASCII art depictions of fractions and see which one is greater or if they are equal, just by how the sections line up.
So for the first A/B = 2/5 and C/D = 1/3 example, this output
|xxxxx|xxxxx|-----|-----|-----|
|xxxxxxxxx|---------|---------|
would be invalid, as, even though the lines are the same length and depict the correct fractions, they can be shorter as shown above.
Scoring
This is a code-golf challenge so the shortest program wins!
Additional notes and rules:
As stated, B and D will be positive, and A will be from 0 to B inclusive, and C will be from 0 to D inclusive.
There must be at least one
x
or-
between each pair of|
as otherwise it's impossible to tell how much of the fraction is filled.The output can have a trailing newline or not, doesn't matter.
You can take in two fractions directly instead of 4 integers if it makes sense for your language.
Testcases
Each testcase is 3 lines, the input A B C D
on one line, followed by the two lines of the output. Empty lines separate testcases.
2 5 1 3
|xx|xx|--|--|--|
|xxxx|----|----|
1 2 5 8
|xxxxxxx|-------|
|x|x|x|x|x|-|-|-|
0 1 0 1
|-|
|-|
0 1 0 2
|---|
|-|-|
1 3 1 2
|x|-|-|
|xx|--|
1 2 1 3
|xx|--|
|x|-|-|
1 2 2 4
|xxx|---|
|x|x|-|-|
1 2 2 2
|x|-|
|x|x|
3 3 1 9
|xxxxx|xxxxx|xxxxx|
|x|-|-|-|-|-|-|-|-|
3 5 4 7
|xxxxxx|xxxxxx|xxxxxx|------|------|
|xxxx|xxxx|xxxx|xxxx|----|----|----|
28 30 29 30
|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|-|-|
|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|-|
7 28 2 8
|x|x|x|x|x|x|x|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|xxxxxx|xxxxxx|------|------|------|------|------|------|
1 7 2 13
|xxxxxxxxxxxx|------------|------------|------------|------------|------------|------------|
|xxxxxx|xxxxxx|------|------|------|------|------|------|------|------|------|------|------|
1 7 2 14
|xxx|---|---|---|---|---|---|
|x|x|-|-|-|-|-|-|-|-|-|-|-|-|
0 10 0 11
|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|
|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
3 10 4 11
|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|----------|----------|----------|----------|----------|----------|----------|
|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|---------|---------|---------|---------|---------|---------|---------|
10 10 11 11
|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|xxxxxxxxxx|
|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|