17
\$\begingroup\$

I think everyone is familiar with the game of snakes & ladders1. Imagine we have a 10x10 board like this:

=============================================================
|     |     |     |     |     |     |     |     |     |     |
| 100 | 99  | 98  | 97  | 96  | 95  | 94  | 93  | 92  | 91  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 81  | 82  | 83  | 84  | 85  | 86  | 87  | 88  | 89  | 90  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 80  | 79  | 78  | 77  | 76  | 75  | 74  | 73  | 72  | 71  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 61  | 62  | 63  | 64  | 65  | 66  | 67  | 68  | 69  | 70  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 60  | 59  | 58  | 57  | 56  | 55  | 54  | 53  | 52  | 51  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 41  | 42  | 43  | 44  | 45  | 46  | 47  | 48  | 49  | 50  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 40  | 39  | 38  | 37  | 36  | 35  | 34  | 33  | 32  | 31  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 21  | 22  | 23  | 24  | 25  | 26  | 27  | 28  | 29  | 30  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 20  | 19  | 18  | 17  | 16  | 15  | 14  | 13  | 12  | 11  |
------+-----+-----+-----+-----+-----+-----+-----+-----+------
|     |     |     |     |     |     |     |     |     |     |
| 1   | 2   | 3   | 4   | 5   | 6   | 7   | 8   | 9   | 10  |
=============================================================

The task is to (sort of) paint a ladder on this board from a smaller number to a bigger one. Kind of an ASCII art using these characters /\ | _ only.

The board's dimensions are 61x31 characters fixed like above. To illustrate the task, let's draw some ladders e.g. from 7 to 31, 22 to 77, 54 to 96, and 68 to 89 like this:

=============================================================
|     |     |     |     |     |     |     |     |     |     |
| 100 | 99  | 98  | 97  | 96  | 95  | 94  | 93  | 92  | 91  |
------+-----+-----+-----+-\_\-+-----+-----+-----+-----+------
|     |     |     |     |  \_\|     |     |     |     |     |
| 81  | 82  | 83  | 84  | 85\_\ 86  | 87  | 88  | 89  | 90  |
------+-----+-----+-----+----\_\----+-----+-----+/_/--+------
|     |     |     |     |     \_\   |     |     /_/   |     |
| 80  | 79  | 78  | 77  | 76  |\_\  | 74  | 73 /_/72  | 71  |
------+-----+-----+/_/--+-----+-\_\-+-----+---/_/-----+------
|     |     |     /_/   |     |  \_\|     |  /_/|     |     |
| 61  | 62  | 63 /_/64  | 65  | 66\_\ 67  | / / | 69  | 70  |
------+-----+---/_/-----+-----+----\_\----+-----+-----+------
|     |     |  /_/|     |     |     \_\   |     |     |     |
| 60  | 59  | /_/ | 57  | 56  | 55  |\ \  | 53  | 52  | 51  |
------+-----+/_/--+-----+-----+-----+-----+-----+-----+------
|     |     /_/   |     |     |     |     |     |     |     |
| 41  | 42 /_/43  | 44  | 45  | 46  | 47  | 48  | 49  | 50  |
------+---/_/-----+-----+-----+-----+-----+-----+-----+------
|     |  /_/|     |     |     |     |     |     |     |     |
| 40  | /_/ | 38  | 37  | 36  | 35  | 34  | 33  | 32  | 31  |
------+-|_|-+-----+-----+-----+-----+-----+-----+-----+/_/---
|     | |_| |     |     |     |     |     |     |     /_/   |
| 21  | | | | 23  | 24  | 25  | 26  | 27  | 28  | 29 /_/30  |
------+-----+-----+-----+-----+-----+-----+-----+---/_/------
|     |     |     |     |     |     |     |     |  /_/|     |
| 20  | 19  | 18  | 17  | 16  | 15  | 14  | 13  | /_/ | 11  |
------+-----+-----+-----+-----+-----+-----+-----+/_/--+------
|     |     |     |     |     |     | __________/_/   |     |
| 1   | 2   | 3   | 4   | 5   | 6   | _________/_/9   | 10  |
=============================================================

Challenge

Given the two distinct numbers that are both in the range of 1..100, draw a ladder from the smaller number to the bigger one, assuming that their arrangements and positions are fixed like above. You are not required to print the whole table. Only the ladder would suffice, but it must be placed in the right position on a 31x61 ASCII sheet.

Note that sometimes the ladder has two parts, i.e. direct (horizontal or vertical) and slant. The direct part must always come first.

Rules

Standard rules apply. If any clarifications or more rules are needed, please inform me in the comments. Happy golfing!

1. In case you have never heard of it (which would be really odd), please refer to the linked Wikipedia article. I skipped elaborating the game rules for the sake of brevity and being on topic.

\$\endgroup\$
5
  • \$\begingroup\$ May we align the digits to the right instead? \$\endgroup\$ Commented Aug 2, 2020 at 18:32
  • \$\begingroup\$ @JonathanAllan Yes that would be fine \$\endgroup\$ Commented Aug 2, 2020 at 18:33
  • \$\begingroup\$ Great challenge! Since it deserves attention, I recommend calling it something slightly more captivating. Catchiness is more important than accuracy in a title! Something like, "Paint Snakes & Ladders," maybe include "Board" at the end. I know the challenge is not quite that; it's more subtle. But that's why you have the body! Minimally, I'd at least recommend getting rid of the parenthetical "(not an actual one)" you have now :) \$\endgroup\$
    – AviFS
    Commented Aug 2, 2020 at 20:14
  • \$\begingroup\$ @AviFS Thanks, duly noted. At first there was no parenthetical, but the system complained that the title is too short. Hope this one is more catchy! \$\endgroup\$ Commented Aug 2, 2020 at 20:27
  • \$\begingroup\$ @polfosolఠ_ఠ 'Course! I do think the Snakes & Ladders in the title would appeal even more as PPCG'ers seem to love board games & games in general, not to mention everyone's a sucker for nostalgia! And "Build Me a Ladder" is more something than "Give Me," but I don't know the word for that... \$\endgroup\$
    – AviFS
    Commented Aug 2, 2020 at 20:33

2 Answers 2

5
\$\begingroup\$

Python 2, 316 bytes

def f(m):
 r=[list(' '*61)for y in range(31)]
 (y,x),(b,a)=[[(109-p)/10*3,1+min(p%20,19-p%20)*6]for p in[max(m)-1,min(m)-1]]
 while y<b:c=cmp(a,x);r[y][x:x+4]=r"  /|\___/|\ "[c%3::3];x+=c;y+=1
 if x==a:r[y-1][x+2-(r[y-1][x]>' ')]=" "
 while x-a:c=cmp(a,x)+1;r[y-2][x+c:x+2+c]="__";r[y-1][x+c*3/2]="_";x+=c-1
 print r

Try it online! (footer paints over grid for pretty output)

Function that inputs a list of two numbers and outputs a list of lists of characters.

How it works

We find character coordinates of the higher point (e.g. 96), then translate it along diagonal, vertical, or horizontal movements to the coordinates of the lower point (e.g. 54), modifying the character matrix r as we go.

def f(m):
    r = [list('.'*61) for y in range(31)]
    m = [max(m)-1,min(m)-1]
    # (x,y) is the higher point
    # (a,b) is the lower point
    # x increases right; y increases down
    (y,x),(b,a) = [[(109-p)/10*3,1+min(p%20,19-p%20)*6] for p in m]
    # move (x,y) to (a,b) via 1-unit translations
    # \ diagonal
    while x < a and y < b:
        r[y][x+1:x+4] = "\\_\\"
        x += 1
        y += 1
    # / diagonal
    while x > a and y < b:
        r[y][x:x+3] = "/_/"
        x -= 1
        y += 1
    # straight down
    while x == a and y < b:
        r[y][x+1:x+4] = "|_|"
        y += 1
    # remove space at the bottom of the ladder
    # (r[y-1][x]!='.' deals with off-by-one error in the case of
    # \ diagonals with no straight segment)
    if x == a:
        r[y-1][x+2-(r[y-1][x]!='.')] = " "
    # left
    while x > a:
        r[y-2][x+1] = "_"
        r[y-2][x] = "_"
        r[y-1][x] = "_"
        x -= 1
    # right
    while x < a:
        r[y-2][x+2] = "_"
        r[y-2][x+3] = "_"
        r[y-1][x+3] = "_"
        x += 1
    return r
\$\endgroup\$
4
\$\begingroup\$

Charcoal, 130 bytes

FχFχ⊞υ×⁶⎇﹪ι²⁻⁹κκ≔⁻LυNθ≔⁻LυNη≔⊕§υηζ≔׳÷ηχηJ§υθ׳÷θχ F›ζⅈ«≦⊕ζ »F⁻ζ⁺ⅈ⁻ⅉηG←←↑→↘→²_F⁻ⅈ⁺ζ⁻ⅉηG↑←↙→²_W›ⅉη«≔⎇›⁻ⅉη↔⁻ζⅈ²∨›ζⅈ³ιP✳ι¹←←✳ι¹→¿›ⅉη_

Try it online! Link is to verbose version of full 198-byte program that includes drawing the entire background. Explanation:

FχFχ⊞υ×⁶⎇﹪ι²⁻⁹κκ

Get the X-coordinates of all 100 squares (since each row reverses direction).

≔⁻LυNθ≔⁻LυNη

Input the starting and ending square, but subtract from 100.

≔⊕§υηζ≔׳÷ηχη

Calculate the destination character.

J§υθ׳÷θχ

Jump to the start character.

 F›ζⅈ«≦⊕ζ »

Erase the start character, and if this is a right-leaning ladder, then erase the next character too (this is because right-leaning ladders start 1 character further right than left-leaning ladders for some reason).

F⁻ζ⁺ⅈ⁻ⅉηG←←↑→↘→²_

Draw the horizontal part of a right-leaning ladder, if applicable. This is complicated slightly because the horizontal part does not start 1 character further right.

F⁻ⅈ⁺ζ⁻ⅉηG↑←↙→²_

Draw the horizontal part of a left-leaning ladder, if applicable.

W›ⅉη«

Repeat until the top of the ladder is reached.

≔⎇›⁻ⅉη↔⁻ζⅈ²∨›ζⅈ³ι

Work out which direction we're going in (3 = up left, 2 = up, 1 (default) = up right).

P✳ι¹←←✳ι¹→

Draw the next section of ladder.

¿›ⅉη_

Draw the next rung, unless we've got to the top of the ladder.

\$\endgroup\$

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.