14
\$\begingroup\$

Given an input list of non-empty strings, output an ASCII art representation of a tournament, based on the following drawing rules:

  • The number of strings is guaranteed to be of quantity 2,4,8,16,etc.
  • The first two strings play each other, and the next two play each other, and so on. This is the first round.
  • For each game, choose the winner randomly with equal probability.
  • For the next round, the winner of the first game plays the winner of the second game, the winner of the third game plays the winner of the fourth game, and so on. Subsequent rounds follow the pattern.
  • There is eventually one overall winner.
  • For pretty output (required) the strings must all be prepended and appended with an underscore _.
  • In order for the brackets to line up appropriately, each entry must be padded with _ to all be the same length for that round.
  • You can choose whether the padding is prepended or appended, so long as it's consistent.
  • Instead, you can choose to pre-pad all strings to be the same length, rather than on a per-round basis. Whichever is golfier for your code.

Further Rules

  • Leading or trailing newlines or whitespace are all optional, so long as the characters themselves line up correctly.
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Examples

Example with cities ['Boston', 'New York', 'Charlotte', 'Atlanta', 'St. Paul', 'Chicago', 'Los Angeles', 'Phoenix']:

_Boston______
             \_New York____
_New York____/             \
                            \_New York_
_Charlotte___               /          \
             \_Charlotte___/            \
_Atlanta_____/                           \
                                          \_St. Paul_
_St. Paul____                             /
             \_St. Paul____              /
_Chicago_____/             \            /
                            \_St. Paul_/
_Los Angeles_               /
             \_Los Angeles_/
_Phoenix_____/

Example with ['Lions', 'Tigers', 'Bears', 'Oh My']:

_Lions__
        \_Tigers_
_Tigers_/        \
                  \_Tigers_
_Bears__          /
        \_Bears__/
_Oh My__/
\$\endgroup\$
3
  • \$\begingroup\$ Related. \$\endgroup\$
    – Okx
    Commented Aug 11, 2017 at 15:22
  • \$\begingroup\$ Does the selection of the winner have to be Mersenne Twister random, or can it be pseudo-random? \$\endgroup\$
    – Zach Gates
    Commented Aug 16, 2017 at 22:57
  • \$\begingroup\$ @ZachGates So long as it's a 50-50 chance between the two "teams," whatever method you want to use is fine. \$\endgroup\$ Commented Aug 17, 2017 at 12:25

2 Answers 2

6
\$\begingroup\$

Charcoal, 92 79 bytes

A¹θWS⊞υ⪫__ιWυ«A⌈EυLκεA⁺θθδFυ«P×_εPκMδ↓»AE✂υ¹Lυ²⎇‽²κ§υ⁺λλυMε→Fυ«Mδ↑↗θ←↖θ→»Mθ↘Aδθ

Try it online! Link is to verbose version of code. Needs a blank line to mark the end of the input. Explanation:

A¹θ

Initialise the variable q. This holds the size of the zig-zags i.e. half the gap between rows.

WS⊞υ⪫__ι

Read nonblank input lines into the array u. The lines are automatically surrounded by _s as they are read in, although they are not padded yet.

Wυ«

Loop while there are still strings left.

A⌈EυLκε

Calculate the width of the largest string in e.

A⁺θθδ

Calculate the gap between rows in d.

Fυ«P×_εPκMδ↓»

For each team, print the padding, print the team, and then move down to the next team.

AE✂υ¹Lυ²⎇‽²κ§υ⁺λλυ

For every other team, randomly pick between that team or the previous team. (Note that if there is only one team left then this produces an empty list.)

Mε→Fυ«Mδ↑↗θ←↖θ→»Mθ↘

If there are still teams left, draw the zigzags joining them in pairs.

Aδθ

Double the length of the zigzags each time.

\$\endgroup\$
4
  • \$\begingroup\$ Do you think there would be any case in which a list input operator would be adcantageous? \$\endgroup\$
    – ASCII-only
    Commented Aug 12, 2017 at 9:20
  • \$\begingroup\$ @ASCII-only Well, I could have used it for the ASCII Venn Diagram question... \$\endgroup\$
    – Neil
    Commented Aug 12, 2017 at 9:39
  • \$\begingroup\$ :| do you think it's worth implementing? \$\endgroup\$
    – ASCII-only
    Commented Aug 12, 2017 at 10:06
  • \$\begingroup\$ @ASCII-only I think you can do ▷vS or something for that anyways. \$\endgroup\$ Commented Aug 12, 2017 at 17:38
2
\$\begingroup\$

Python 2, 379 364 bytes

exec r"""c=input();from random import*;R,L,d=range,len,0;u,s="_ ";r=[[""]*-~L(c)@R(2*L(c)-1)]
while c:
 W=2+max(map(L,c));j=1<<d;J=j/2;D=d+d;d+=1
 @r:l[D]=s*W;l[D-1]=s*J
 @R(L(c)):
	h=l*2*j+j-1;r[h][D]=(u+c[l]+u*W)[:W]
	@R(h-J,h+J):r[-~l][~-D]=("/\\"[l<h]+s*abs(h-l-(l<h))).rjust(J)
 c=[choice(l)@zip(c[::2],c[1::2])]
@r:print"".join(l)""".replace("@","for l in ")

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ You can replace your two-level indents with a singular tab and save three bytes. \$\endgroup\$ Commented Sep 19, 2017 at 11:43
  • \$\begingroup\$ 365 bytes. \$\endgroup\$ Commented Sep 19, 2017 at 11:50
  • \$\begingroup\$ 364 bytes. \$\endgroup\$ Commented Sep 19, 2017 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.