Skip to main content
Bugfix
Source Link
Sara J
  • 3.8k
  • 17
  • 36

Ink, 120 119 116116 117 bytes

=f(n)
->g(n,3,2)
=g(n,b,s)
~n--
{n:{~{b:b->g(n,b-1,s)}|{s:s->g(n,b,s-1)}|}f->g(n,b,s&&s+1s-(s>0))|{~{b:h|b}|{s:h|s}|h}}->->

Try it online!Try it online!

=f(length) // Define a stitch f, with one parameter which specifies the length of the created string. This is the intended entry point.
->g(length,3,2) // Instantly divert to g, defined below, with some extra parameters

=g(length,balls_left,strikes_left) // Define a stitch g, with three parameters.
~ length--                         // Decrement remaining length
{
    - length: // If this is not to be the last character in the string
              // randomly do one of the following:
              // 1. If balls_left is nonzero, print a b and recurse
              // 2. If strikes_left is nonzero, print an s and recurse
              // 3. Do nothing
              // If we did not divert earlier, print an f and recurse.
        {~{balls_left:b->g(length,balls_left-1,strikes_left)}|{strikes_left:s->g(length,balls_left,strikes_left-1)}|}f->g(length,balls_left,strikes_left&&strikes_leftstrikes_left-1(strikes_left>0)) 
    - else: // Randomly do one of the following
            // 1. If a ball would result in a walk, print a b, otherwise an h.
            // 2. If a strike would result in a strikeout, print an s, otherwise an h.
            // 3. Just print an h.
            // And finally, halt.
        {~{balls_left:h|b}|{strikes_left:h|s}|h}}->->
  1. Saved a byte by finishing with ->-> instead of ->END.
  2. Saved three bytes by decrementing n earlier.
  3. Fixed a bug that caused strikeouts in incorrect places, thanks to @veskah for spotting it (+1 byte)

Ink, 120 119 116 bytes

=f(n)
->g(n,3,2)
=g(n,b,s)
~n--
{n:{~{b:b->g(n,b-1,s)}|{s:s->g(n,b,s-1)}|}f->g(n,b,s&&s+1)|{~{b:h|b}|{s:h|s}|h}}->->

Try it online!

=f(length) // Define a stitch f, with one parameter which specifies the length of the created string. This is the intended entry point.
->g(length,3,2) // Instantly divert to g, defined below, with some extra parameters

=g(length,balls_left,strikes_left) // Define a stitch g, with three parameters.
~ length--                         // Decrement remaining length
{
    - length: // If this is not to be the last character in the string
              // randomly do one of the following:
              // 1. If balls_left is nonzero, print a b and recurse
              // 2. If strikes_left is nonzero, print an s and recurse
              // 3. Do nothing
              // If we did not divert earlier, print an f and recurse.
        {~{balls_left:b->g(length,balls_left-1,strikes_left)}|{strikes_left:s->g(length,balls_left,strikes_left-1)}|}f->g(length,balls_left,strikes_left&&strikes_left-1) 
    - else: // Randomly do one of the following
            // 1. If a ball would result in a walk, print a b, otherwise an h.
            // 2. If a strike would result in a strikeout, print an s, otherwise an h.
            // 3. Just print an h.
            // And finally, halt.
        {~{balls_left:h|b}|{strikes_left:h|s}|h}}->->
  1. Saved a byte by finishing with ->-> instead of ->END.
  2. Saved three bytes by decrementing n earlier.

Ink, 120 119 116 117 bytes

=f(n)
->g(n,3,2)
=g(n,b,s)
~n--
{n:{~{b:b->g(n,b-1,s)}|{s:s->g(n,b,s-1)}|}f->g(n,b,s-(s>0))|{~{b:h|b}|{s:h|s}|h}}->->

Try it online!

=f(length) // Define a stitch f, with one parameter which specifies the length of the created string. This is the intended entry point.
->g(length,3,2) // Instantly divert to g, defined below, with some extra parameters

=g(length,balls_left,strikes_left) // Define a stitch g, with three parameters.
~ length--                         // Decrement remaining length
{
    - length: // If this is not to be the last character in the string
              // randomly do one of the following:
              // 1. If balls_left is nonzero, print a b and recurse
              // 2. If strikes_left is nonzero, print an s and recurse
              // 3. Do nothing
              // If we did not divert earlier, print an f and recurse.
        {~{balls_left:b->g(length,balls_left-1,strikes_left)}|{strikes_left:s->g(length,balls_left,strikes_left-1)}|}f->g(length,balls_left,strikes_left-(strikes_left>0)) 
    - else: // Randomly do one of the following
            // 1. If a ball would result in a walk, print a b, otherwise an h.
            // 2. If a strike would result in a strikeout, print an s, otherwise an h.
            // 3. Just print an h.
            // And finally, halt.
        {~{balls_left:h|b}|{strikes_left:h|s}|h}}->->
  1. Saved a byte by finishing with ->-> instead of ->END.
  2. Saved three bytes by decrementing n earlier.
  3. Fixed a bug that caused strikeouts in incorrect places, thanks to @veskah for spotting it (+1 byte)
deleted 30 characters in body
Source Link
Sara J
  • 3.8k
  • 17
  • 36

Probably still golfable. No online interpreter, sorry.

Probably still golfable. No online interpreter, sorry.

Probably still golfable.

Now on TIO
Source Link
Sara J
  • 3.8k
  • 17
  • 36

InkInk, 120 119 116 bytes

Try it online!

Ink, 120 119 116 bytes

Ink, 120 119 116 bytes

Try it online!

Found an improvement
Source Link
Sara J
  • 3.8k
  • 17
  • 36
Loading
added 76 characters in body
Source Link
Sara J
  • 3.8k
  • 17
  • 36
Loading
Fixed a comment in the explanation - the second stitch is called "g" not "f"
Source Link
Sara J
  • 3.8k
  • 17
  • 36
Loading
Source Link
Sara J
  • 3.8k
  • 17
  • 36
Loading