4
\$\begingroup\$

Introduction

I crochet a lot, and lately I've started writing crochet patterns. However, while I work very well from notes such as rnds 2-6 (round number = x, original number of stitches = y): *dc x-2 stitches, 2 dc in next stitch* around (xy stitches), most published patterns spell out each row in more detail. I don't like writing out each individual row/round myself though, so I want to have a program do it for me.

Challenge

The challenge is to write a program or function that takes three mandatory inputs (r, s, and y) and returns a string of the pattern.

Inputs

The inputs can go in any appropriate order or format, just please point that out so others can run your code.

R is an integer corresponding to the number of rounds/rows (use whichever designation you want) to generate. S is a string corresponding to the type of stitch. This can be guaranteed to be one of the following strings: "tc", "dc", "hdc", "sc", or "ch". Y is the number of stitches in row/round 1, and the number of increases in each following row (for instance, if y=6, row 1 has 6 stitches, row 2 has 12 stitches, row 3 has 18 stitches, etc). In the general case: row x has xy stitches.

Outputs

The output is a list of strings with length r, in the following pattern.

O N: \*s in next N-2 stitches, 2 s in next stitch\* y times (NY stitches).

To clarify: it should return r strings, in order from least row/rnd number to greatest row/rnd number. The first row/rnd should be row 1, and the last row should be row r. In the template, replace s and y with the matching inputs, and N is the row number of the current row (N isn’t an input, just an integer that represents the current row). O is used to denote one of the following strings, your choice: “Row”, “Round”, or “Rnd”.

Each line of output should be separated by a newline. The period is necessary and should follow the parentheses.

Edge Cases

You can assume s is one of the listed strings. R will always be a positive integer, as will y.

You don’t have to do anything fancy for row 1, it can be Row 1: \*s in next -1 stitches, 2 s in next stitch\* y times (y stitches). Row 2 can also be outputted in the default format, as Row 2: \*s in next 0 stitches, 2 s in next stitch\* y times (2y stitches). Thusly, it can be outputted with a for loop. But, if you output rows 1 and 2 as Row 1: s in next y stitches (y stitches).\nRow 2: 2 s in next y stitches (2y stitches)., you can have -10% or -10 bytes, whichever is smaller, removed from your score.

Scoring

This is code golf, so fewest bytes wins. If you output rows 1 and 2 as shown in the Output section above, your score is lowered by -10% or -10 bytes, whichever is smaller.

Example Input and Output

Input [the format doesn’t matter so much as the answer, so it could be any format the language supports]: (r,s,y)

  1. (5, “dc”, 6)
  2. (3, “sc”, 1)
  3. (6, “hdc”, 100)
  4. (0, “ch”, 1)
  5. (5, “tc”, 0)

Output [this format must be fairly exact, hyphens are used to show negative numbers]:

  1. Row 1: \*dc in next -1 stitches, 2 dc in next stitch\* 6 times (6 stitches).\nRow 2: \*dc in next 0 stitches, 2 dc in next stitch\* 6 times (12 stitches).\nRow 3: \*dc in next 1 stitches, 2 dc in next stitch\* 6 times (18 stitches).\nRow 4: \*dc in next 2 stitches, 2 dc in next stitch\* 6 times (24 stitches).\nRow 5: \*dc in next 3 stitches, 2 dc in next stitch\* 6 times (30 stitches).
  2. Rnd 1: \*sc in next -1 stitches, 2 sc in next stitch\* 1 times (1 stitches).\nRnd 2: \*sc in next 0 stitches, 2 sc in next stitch\* 1 times (2 stitches).\nRnd 3: \*sc in next 1 stitches, 2 sc in next stitch\* 1 times (3 stitches).\n
  3. Rnd 1: \*hdc in next -1 stitches, 2 hdc in next stitch\* 100 times (100 stitches).\nRnd 2: \*hdc in next 0 stitches, 2 hdc in next stitch\* 100 times (200 stitches).\nRnd 3: \*hdc in next 1 stitches, 2 hdc in next stitch\* 100 times (300 stitches).\nRnd 4: \*hdc in next 2 stitches, 2 hdc in next stitch\* 100 times (400 stitches).\nRnd 5: \*hdc in next 3 stitches, 2 hdc in next stitch\* 100 times (500 stitches).\nRnd 6: \*hdc in next 4 stitches, 2 hdc in next stitch\* 100 times (600 stitches).\n
  4. This can error, or not produce any output, or anything. R will always be positive and nonzero.
  5. This can either error OR produce Rnd 1: \*tc in next -1 stitches, 2 tc in next stitch\* 0 times (0 stitches).\nRnd 2: \*tc in next 0 stitches, 2 tc in next stitch\* 0 times (0 stitches).\nRnd 3: \*tc in next 1 stitches, 2 tc in next stitch\* 0 times (0 stitches).\nRnd 4: \*tc in next 2 stitches, 2 tc in next stitch\* 0 times (0 stitches).\nRnd 5: \*tc in next 3 stitches, 2 tc in next stitch\* 0 times (0 stitches).

Suggestions for more test cases welcomed! Please let me know how to improve it. If you have an input/output that produces a pattern you like, add it to your answer and maybe I’ll crochet it when I have time.

Ungolfed solution in Python 2: Try it online

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Perhaps you should be more lenient than this format must be exact. Would lists of the given strings be ok? Also there's a lot of *, -, and () dispersed through-out the output - can this be optional or removed? \$\endgroup\$
    – Noodle9
    Commented Jun 12, 2020 at 18:34
  • \$\begingroup\$ A list of the strings is an acceptable output. The * ( ) characters I don’t think should be optional, they are part of the format of crochet patterns and show repeats and stitch count. \$\endgroup\$ Commented Jun 12, 2020 at 18:38
  • \$\begingroup\$ Great! I was more concerned about the commas, the periods, the colons and the dashes - can they be optional? \$\endgroup\$
    – Noodle9
    Commented Jun 12, 2020 at 18:40
  • \$\begingroup\$ This was initially posted in the sandbox but I don’t know if I can post a link to a comment on a deleted post: reference implementation in Python 2 for an older version (should still work for the current specs). Try It Online \$\endgroup\$ Commented Jun 12, 2020 at 18:41
  • \$\begingroup\$ I think the periods and commas are probably optional, as long as the spacing is ok. I don’t see any dashes — only the hyphens showing subtraction, which are part of computing the output. What dashes do you see, @Noodle9? \$\endgroup\$ Commented Jun 12, 2020 at 18:44

3 Answers 3

1
\$\begingroup\$

perl -M5.010 -a, 124 bytes

say"Row $_: \\*$F[1] in next ${\($_-2)} stitches, 2 $F[1] in next stitch\\* $F[2] times (${\($_*$F[2])} stitches)."for 1..$_

Try it online!

Pretty trivial. The bulk of the program consists of the line which needs to be printed, with some variable interpolation.

\$\endgroup\$
1
\$\begingroup\$

Retina, 132 bytes

^\d+
*
Lv^$`(_+) (.+) (.+)
Row $.1: \$*$2 in next $1 stitches, 2 $2 in next stitch\$* $3 times ($.($3*$1) stitches).
__(_*)
$.1
_
-1

Try it online! Takes input in the order r, s, y. Explanation:

^\d+
*

Convert r to unary.

Lv^$`(_+) (.+) (.+)

Count down from r to 1, then reverse the results, and substitute in the output pattern:

Row $.1: \$*$2 in next $1 stitches, 2 $2 in next stitch\$* $3 times ($.($3*$1) stitches).

$.1 is the row, $* is a literal *, $2 is s, $1 is the row in unary, $3 is y, and $.($3*$1) is the product of the row with y (in decimal).

__(_*)
$.1

Subtract 2 from the row for the final substitution.

_
-1

Fix up the first row.

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 62 bytes

EN³*³²NͲN“ ÿ: \*ÿ€†‚š ÿÀŠ, 2 ÿ€†‚šïŠ\* ÿ„Æ (ÿÀŠ).“'w't:'¢ã™ì,

Input in the same order as the challenge description, as three loose inputs \$r,s,y\$.

I've used Row for the optional first word, although Round and Rnd could both also be used for the same byte-count, by replacing '¢ã™ with '퉙 or …Rnd respectively.

Try it online or verify all test cases.

Explanation:

E        # Loop `N` in the range [1, (implicit) first input `r`]:
 N³*     #  Push `N` multiplied by the third input `y`
 ³       #  Push the third input `y`
 ²       #  Push the second input `s`
 NÍ      #  Push `N-2`
 ²       #  Push the second input `s` again
 N       #  Push `N`
 “ ÿ: \*ÿ€†‚š ÿÀŠ, 2 ÿ€†‚šïŠ\* ÿÀŠ (ÿ„Æ).“
         #  Push dictionary string " ÿ: \*ÿ in next ÿ switches, 2 ÿ in next switch\* ÿ times (ÿ switches).",
         #  where the `ÿ` are automatically replaced with the values on the stack
  'w't:  #  Replace all "w" with "t" ("switch" to "stitch")
  '¢ã   '#  Push dictionary string "row"
     ™   #  Titlecase it to "Row"
      ì  #  Prepend it in front of the string
       , #  And output it with trailing newline

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why “ ÿ: \*ÿ€†‚š ÿÀŠ, 2 ÿ€†‚šïŠ\* ÿ„Æ (ÿÀŠ).“ is " ÿ: \*ÿ in next ÿ switches, 2 ÿ in next switch\* ÿ times (ÿ switches)." and '¢ã is "row".

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'm not sure that your output is correct - it says (Ny times) instead of y times (Ny stitches). \$\endgroup\$
    – Neil
    Commented Jun 20, 2020 at 19:43
  • 1
    \$\begingroup\$ And there should be \s before the *s... (fortunately I spotted that error in my code before the grace period expired!) \$\endgroup\$
    – Neil
    Commented Jun 20, 2020 at 19:45
  • \$\begingroup\$ ... except I forgot to update my byte count, oops... \$\endgroup\$
    – Neil
    Commented Jun 20, 2020 at 19:50
  • \$\begingroup\$ @Neil Thanks for noticing. Should be fixed now. EDIT: After I've fixed the slashes. EDIT2: Done \$\endgroup\$ Commented Jun 20, 2020 at 19:51

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.