Skip to main content
added 115 characters in body
Source Link

C (gcc), 96 7171 70 bytes

Awesome -25 solution by ceilingcat

And another -1 by ceilingcat. ASCII bit hacks ftw

c;p;main(i){for(;read(0,&c,1);printf("%*c\n",i,p=c))c-p?:c-47c&1?++i:--i;i:++i;}

Try it online!Try it online!

Ungolfed

c; /* character */
p; /* previous character */

main(i /* indent */)
{
    for (; read(0, &c, 1); printf("%*c\n", i, p = c))
        c - p ? : c -& 471 ? ++i--i : --i;++i;
}

Explanation

read takes 1 byte from the file descriptor 0 (stdin) and stores it in c.

c - p checks if c and p are different. c -& 471 checks if c is not a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes.

Then we print c with i leading spaces. We also assign p = c for the next iteration.

C (gcc), 96 71 bytes

Awesome -25 solution by ceilingcat

c;p;main(i){for(;read(0,&c,1);printf("%*c\n",i,p=c))c-p?:c-47?++i:--i;}

Try it online!

Ungolfed

c; /* character */
p; /* previous character */

main(i /* indent */)
{
    for (; read(0, &c, 1); printf("%*c\n", i, p = c))
        c - p ? : c - 47 ? ++i : --i;
}

Explanation

read takes 1 byte from the file descriptor 0 (stdin) and stores it in c.

c - p checks if c and p are different. c - 47 checks if c is not a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes.

Then we print c with i leading spaces. We also assign p = c for the next iteration.

C (gcc), 96 71 70 bytes

Awesome -25 solution by ceilingcat

And another -1 by ceilingcat. ASCII bit hacks ftw

c;p;main(i){for(;read(0,&c,1);printf("%*c\n",i,p=c))c-p?:c&1?--i:++i;}

Try it online!

Ungolfed

c; /* character */
p; /* previous character */

main(i /* indent */)
{
    for (; read(0, &c, 1); printf("%*c\n", i, p = c))
        c - p ? : c & 1 ? --i : ++i;
}

Explanation

read takes 1 byte from the file descriptor 0 (stdin) and stores it in c.

c - p checks if c and p are different. c & 1 checks if c is a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes.

Then we print c with i leading spaces. We also assign p = c for the next iteration.

added 46 characters in body
Source Link

C (gcc), 9696 71 bytes

Awesome -25 solution by ceilingcat

i;p;j;mainc;p;main(ci){for(;~(c=getchar;read()0,&c,1);printf("%c\n""%*c\n",i,p=c))for(j=cc-p?i:c-47?++i:--i;j--;)printf(" ");i;}

Try it online!Try it online!

Ungolfed

i;c; /* character */
p;
j; /* previous character */

main(ci /* indent */)
{
    for (; ~read(c0, =&c, getchar()1); printf("%c\n""%*c\n", i, p = c))
        for (j = c - p ? i : c - 47 ? ++i : --i; j--;)
            printf(" ");
}

Explanation

iread (indentation),takes p1 (previous) andbyte from the file descriptor j0 (loop variable for printing spaces) are all integers initialised to 0

Get a character from stdin) and storestores it in c (character). Break if it's EOF (-1)

If c and - p are different, i stays the same. Ifchecks if c and p are the same, increment i ifdifferent. c is a backslash and decrement i - 47 checks if c is not a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes.

Then we print j = ic

Print with ji leading spaces

 . We also assign p = c

Print c and a newline

Get another character from stdinfor the next iteration.

C (gcc), 96 bytes

i;p;j;main(c){for(;~(c=getchar());printf("%c\n",p=c))for(j=c-p?i:c-47?++i:--i;j--;)printf(" ");}

Try it online!

Ungolfed

i;
p;
j;

main(c)
{
    for (; ~(c = getchar()); printf("%c\n", p = c))
        for (j = c - p ? i : c - 47 ? ++i : --i; j--;)
            printf(" ");
}

Explanation

i (indentation), p (previous) and j (loop variable for printing spaces) are all integers initialised to 0

Get a character from stdin and store it in c (character). Break if it's EOF (-1)

If c and p are different, i stays the same. If c and p are the same, increment i if c is a backslash and decrement i if c is a forward slash

j = i

Print j spaces

 p = c

Print c and a newline

Get another character from stdin

C (gcc), 96 71 bytes

Awesome -25 solution by ceilingcat

c;p;main(i){for(;read(0,&c,1);printf("%*c\n",i,p=c))c-p?:c-47?++i:--i;}

Try it online!

Ungolfed

c; /* character */
p; /* previous character */

main(i /* indent */)
{
    for (; read(0, &c, 1); printf("%*c\n", i, p = c))
        c - p ? : c - 47 ? ++i : --i;
}

Explanation

read takes 1 byte from the file descriptor 0 (stdin) and stores it in c.

c - p checks if c and p are different. c - 47 checks if c is not a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes.

Then we print c with i leading spaces. We also assign p = c for the next iteration.

deleted 5 characters in body
Source Link

C (gcc), 96 bytes

i;p;j;main(c){for(;~(c=getchar());printf("%c\n",p=c))for(j=c-p?i:c-47?++i:--i;j--;)printf(" ");}

Try it online!

Ungolfed

i;
p;
j;

main(c)
{
    for (; ~(c = getchar()); printf("%c\n", p = c))
        for (j = c - p ? i : c - 47 ? ++i : --i; j--;)
            printf(" ");
}

Explanation

i (indentation), p (previous), and j (loop variable for printing spaces) and c (character) are all integers initialised to 0

Get a character from stdin and store it in c (character). Break if it's EOF (-1)

If c and p are different, i stays the same. If c and p are the same, increment i if c is a backslash and decrement i if c is a forward slash

j = i

Print j spaces

p = c

Print c and a newline

Get another character from stdin

C (gcc), 96 bytes

i;p;j;main(c){for(;~(c=getchar());printf("%c\n",p=c))for(j=c-p?i:c-47?++i:--i;j--;)printf(" ");}

Try it online!

Ungolfed

i;
p;
j;

main(c)
{
    for (; ~(c = getchar()); printf("%c\n", p = c))
        for (j = c - p ? i : c - 47 ? ++i : --i; j--;)
            printf(" ");
}

Explanation

i (indentation), p (previous), j (loop variable for printing spaces) and c (character) are all integers initialised to 0

Get a character from stdin and store it in c. Break if it's EOF (-1)

If c and p are different, i stays the same. If c and p are the same, increment i if c is a backslash and decrement i if c is a forward slash

j = i

Print j spaces

p = c

Print c and a newline

Get another character from stdin

C (gcc), 96 bytes

i;p;j;main(c){for(;~(c=getchar());printf("%c\n",p=c))for(j=c-p?i:c-47?++i:--i;j--;)printf(" ");}

Try it online!

Ungolfed

i;
p;
j;

main(c)
{
    for (; ~(c = getchar()); printf("%c\n", p = c))
        for (j = c - p ? i : c - 47 ? ++i : --i; j--;)
            printf(" ");
}

Explanation

i (indentation), p (previous) and j (loop variable for printing spaces) are all integers initialised to 0

Get a character from stdin and store it in c (character). Break if it's EOF (-1)

If c and p are different, i stays the same. If c and p are the same, increment i if c is a backslash and decrement i if c is a forward slash

j = i

Print j spaces

p = c

Print c and a newline

Get another character from stdin

Source Link
Loading