17
\$\begingroup\$

Introduction

Long story short: a few days ago I accidentally started knitting a sock and the pretty logical structure of the plain stitches lead me to the idea: Why don't we just ASCII-knit a sock?

Input

The input is an even integer N in the range [2,30].

Output

The output is a sock, obviously.

Structure

Because we will only use plain stitches you will only use v for a downward stitch and > for a stitch to the right. Let me explain the full structure with an example for N=8:

vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvv>>>
vvvvvv>>>>>>
vvvvv>>>>>>>>>
vvvv>>>>>>>>>>>>
vvvv>>>>>>>>>>>>>>>>>>>>>>>>
vvvv>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvv>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvv>>>>>>>>>>>>>>>>>>>>>>>>
  • The first block is the upper part. It has the width N and the height N*1.5
  • Afterwards the heel begins. You subtract one v per row from the right side until you have N/2 as width. Then you add N/2 rows with the width N/2 beneath to finish the downward knits. Note that it gives a total of N/2 + 1 rows with the width N/2

first part:

vvvvvvvv    //begin with width N
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv
vvvvvvvv    // N*1.5 times
vvvvvvv     // reduce
vvvvvv
vvvvv
vvvv        // till here
vvvv        // repeat N/2 times
vvvv
vvvv
vvvv
  • Now the side wards knits begin. You start with the first reduced row, fill > till you have the width N and add two more. Continue that process including the first row with N/2 width
  • Fill the rows beneath to that level and add N*1.5 times > to that
  • Let the outer parts as they are and add on the row beneath the first and over the lase two more >'s until it is finished

second part:

vvvvvvvv
vvvvvvv>>>  //fill to N and add two >
vvvvvv>>>>>>     //repeat
vvvvv>>>>>>>>>
vvvv>>>>>>>>>>>>   //till here
vvvv>>>>>>>>>>>>>>>>>>>>>>>>       //fill to the row above and add N*1.5 times >
vvvv>>>>>>>>>>>>>>>>>>>>>>>>>>     //add two more '>' than the row above
vvvv>>>>>>>>>>>>>>>>>>>>>>>>>>     //add two more '>' than the row beneath
vvvv>>>>>>>>>>>>>>>>>>>>>>>>

Rules

Trailing new lines are ok.

You can choose between uppercase or lowercase V but it has to be consistent.

This is so the shortest code in bytes wins.

Test cases

N=2
vv
vv
vv
v>>>
v>>>>>>

N=4
vvvv
vvvv
vvvv
vvvv
vvvv
vvvv
vvv>>>
vv>>>>>>
vv>>>>>>>>>>>>
vv>>>>>>>>>>>>

N=6
vvvvvv
vvvvvv
vvvvvv
vvvvvv
vvvvvv
vvvvvv
vvvvvv
vvvvvv
vvvvvv
vvvvv>>>
vvvv>>>>>>
vvv>>>>>>>>>
vvv>>>>>>>>>>>>>>>>>>
vvv>>>>>>>>>>>>>>>>>>>>
vvv>>>>>>>>>>>>>>>>>>

N=12
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvvv>>>
vvvvvvvvvv>>>>>>
vvvvvvvvv>>>>>>>>>
vvvvvvvv>>>>>>>>>>>>
vvvvvvv>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vvvvvv>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
\$\endgroup\$
6
  • 2
    \$\begingroup\$ I hope a trailing newline is okay? (also, how does one accidentally start knitting a sock?) \$\endgroup\$ Jan 25, 2017 at 10:37
  • 3
    \$\begingroup\$ @GregMartin Visited my grandmother and she asked me. It's hard to say no to her so here we go :) Finished the first one two days ago. I think it's pretty fine but I really underestimated the amount of work. So be happy if someone made socks for you. \$\endgroup\$
    – izlin
    Jan 25, 2017 at 10:44
  • \$\begingroup\$ Can I use v or V interchangeably? I'm going to assume for the moment that v is the only one allowed, but allowing V will effect the byte count \$\endgroup\$ Jan 25, 2017 at 11:26
  • \$\begingroup\$ @GoldenRatio I changed the rules, now both is allowed. \$\endgroup\$
    – izlin
    Jan 25, 2017 at 12:12
  • \$\begingroup\$ @GurupadMamadapur As you can read in 'Input', only even numbers are allowed as input. Your program doesn't need to handle odd numbers \$\endgroup\$
    – izlin
    Jan 25, 2017 at 12:14

9 Answers 9

4
+200
\$\begingroup\$

Vyxal, 54 bytes

1.*(\v*,)½(n-\v*₴n:d+\>*,)½½ʁ(½\v*?T\>*Jnd\>*J:⅛,)¾(¼,

Boy oh boy, this is my new favorite answer now.
The satisfaction when I got it down from 79 bytes to 54 bytes was immense.
I bet you can't golf this anymore.
Try it Online!

Explanation

1.*(\v*,)½(n-\v*₴n:d+\>*,)½½ʁ(½\v*?T\>*Jnd\>*J:⅛,)¾(¼,

1.*(\v*,)     <Top part>
1.            Evaluates to 1.5
  *           Times input i
   (    )     Loop (with loop var n)
    \v*,      Repeat "v" n times and print it

½(n-\v*₴n:d+\>*,)     <Diagonal part>
½                     Halve input i
 (              )     Loop (with loop var n)
  n-\v*₴              Repeat "v" i minus n times and print it (without a newline)
        n:d           Duplicate n and double it
           +\>*,      Repeat ">" 2n plus n times

½½ʁ(½\v*?3*\>*Jnd\>*J:⅛,)     <Top half bumpy part>
½½ʁ                           Create range 0 to input i/4
   (                    )     Loop
    ½\v*                      Repeat "v" i/2 times
        ?T\>*J                Repeat ">" 3i times and join it
               nd\>*J:        Repeat ">" 2n times, merge and duplicate it
                      ⅛,      Push it to the global array and print it

¾(¼,                          <Bottom half bumpy part>
¾(                            Loop through the length of the global array
  ¼,                          Pop from the global array (to reverse it)
\$\endgroup\$
2
\$\begingroup\$

Pyth - 93 Bytes

J/Q2K"V"P*s*Q1.5+*QKbVJp*+Q_+1NK*+3*3N">";InQ2VJp*JK*+s*2aaNc-J 1 2c-J 1 2+3*Q3">";IqQ2pK*6">

Explanation:

J/Q2                             sets J to half of input
K"V"                             sets K to "V"
P                                prevents newline after main block of sock
*                                multiply the next 2 items
s*Q1.5                           rounds up input*1.5 (columns in main block)
+                                adds the next 2 item (this item gets multiplied by the previous item)
*QK                              multiplies "V" by input (resulting in each row)
b                                New line
VJ                               For all the numbers 0-((Q/2)-1) (as represented by N)
p                                print
*                                multiply the next 2 items
+Q_+1N                           input + -1*(1+N)
K                                "V"
*+3*3N">"                        3N+3 many ">"'s
;                                ends for statement
InQ2                             if input is not 2
VJ                               For all the numbers 0-((input/2)-1) (as represented by N)
p                                print
*JK                              input/2 many ">"'s
*+s*2aaNc-J 1 2c-J 1 2+3*Q3">"   int(abs(abs(N-((input/2)-1)/2)-((input/2)-1)/2)))+(input*3)+3 many ">"'s
;                                end of for statement
IqQ2                             if input is 2
pK*6">                           print "V" and 6 ">"'s

You can try it here!

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 104 bytes

""<>{t=Table;c=t[{"v"~t~#,">"~t~#2,"
"},#3]&;h=#/2;c[#,0,3h],c[#-i,3i,z={i,h}],c[h,6h+2Min[i-1,h-i],z]}&

Unnamed function taking a positive even integer as input and returning a string (with a trailing newline). Note that the newline between the two lines of code above is part of the code. The main work is done by the function c, whose ungolfed definition

c[#1, #2, #3] = Table[{Table["v", #1], Table[">", #2], "\n"}, #3]

creates a table of lists, each one consisting of several "v"s in a row followed by several ">"s followed by a newline. Fortunately, the range #3 for the outer Table can have a variable name, which can be referred to in the expressions #1 and #2; this allows the function c to be called with both constant and variable arguments. The rest is just computation, and the string-joining operator ""<> flattens the nested lists that arise for free.

\$\endgroup\$
2
\$\begingroup\$

Python, 3.5 183 177 bytes

n=int(input())
h=n//2
z=h%2
l=['v'*h+'>'*(2*n-h+3*h+i*2)for i in range(1*z+n//4)]
*map(print,['v'*n]*(3*h)+['v'*i+'>'*(3*(n-i))for i in range(h,n)][::-1]+[l,l[:-1]][z]+l[::-1]),

Explanation incoming.

Try it Online!

\$\endgroup\$
4
  • \$\begingroup\$ Just so that you know, TIO Nexus works better than TIO v2 at the moment as v2 is in "the early alpha stage" \$\endgroup\$
    – user63571
    Jan 26, 2017 at 23:04
  • \$\begingroup\$ To save one byte, you could assign 'v' top a variable. \$\endgroup\$
    – nedla2004
    Jan 27, 2017 at 1:47
  • \$\begingroup\$ @JackBates Duly noted. \$\endgroup\$ Jan 27, 2017 at 4:28
  • \$\begingroup\$ @nedla2004 Do you mean this way : n,v=int(input()),'v' ? Then it saves nothing. \$\endgroup\$ Jan 27, 2017 at 4:28
1
\$\begingroup\$

Groovy, 168 bytes

{n->h=n/2;z=(int)h%2;l=(0..<1*z+n/4).collect{'v'*h+'>'*(2*n-h+3*h+it*2)};(['v'*n]*(3*h)+(n-1..h).collect{'v'*it+'>'*(3*(n-it))}+(z==0?l:l[0..<-1])+l[-1..0]).join('\n')}

This is an unnamed closure. The approach is completely similar to my Python 3.5 answer.

Try it Online!

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

Batch, 279 bytes

@echo off
set i=for /l %%i in (2,2,%1)do 
set v=
%i%call set v=v%%v%%v
%i%echo %v%&echo %v%&echo %v%
%i%call:a
%i%call set v=%%v%%]]]
%i:,2,=,4,%call:c&if %%i lss %1 call set v=%%v%%]]
%i:2,=4,%call:b
exit/b
:a
set v=%v:~1%]]]]]
:b
set v=%v:~0,-2%
:c
echo %v:]=^>%

Note: the second line ends in a space. Batch has problems with echoing >s in variables so I have to a) use a substitute b) call a subroutine to echo the corrected string. In fact I have three subroutines:

  • a is used for the heel. One v is removed each time and three >s are added. Two extra >s are added so that the subroutine can fall through into the b subroutine.
  • b is used for the bottom of the foot. Two >s are removed each time. The subroutine then falls through into the c subroutine.
  • c prints the current row of the sock, replacing the placeholder character so that the >s print properly.

The fourth line handles the width of the leg while the fifth line handles its length. The seventh line handles the length of the foot while the eighth line handles the top half of the foot height by adding two >> to each line after printing it. This works for sock sizes that are multiples of 4, and an adjustment is made for other sock sizes so that the longest line does not get repeated.

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

C (gcc), 185 bytes

#define L for(i=0;i<h;i++,p(10,1))
p(c,n){n&&putchar(--n?p(c,n):c);}
f(n,i,k,h){h=n/2;for(k=3;k--;)L p(118,n);L{p(118,n-i-1);p(62,3*i+3);}L{p(118,h);p(62,3*n);p(62,2*(4*i/n?h-i-1:i));}}

Try it online!

Mostly boring, except I use a recursive function to print repeated characters.

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

PHP, 229 bytes

a quite lazy approach

for($m=$n=$p=$argv[1];$i++<$q=$n*1.5;)echo($d=str_pad)("
",$n+1,v);for(;$p-->$n/2;)echo$s="
".$d($d("",$p,v),$m+=2,">");$r=[$s.=$d("",$q,">")];for($k=$n>>2;--$k;)$r[]=$s.=">>";echo join($r),$n&2?"$s>>":"",join(array_reverse($r));

prints a leading newline. Run with -nr.

breakdown

// print first part + preparing some variables for later
for($m=$n=$p=$argv[1];$i++<$q=$n*1.5;)echo str_pad("\n",$n+1,v);

// print second part + prepare $s for third part
for(;$p-->$n/2;)echo$s="\n".str_pad(str_pad("",$p,v),$m+=2,">");

// third part: create an array of strings ...
$r=[$s.=str_pad("",$q,">")];
for($k=$n>>2;--$k;)$r[]=$s.=">>";
// ... then print it and its reverse
echo join($r),$n&2?"$s>>":"",join(array_reverse($r));
\$\endgroup\$
0
\$\begingroup\$

dc, 269 bytes

1k?d1.5*sadsdsb[[[v]nlb1-dsb0<f]dsfx[]pldsbla1-dsa0<c]dscxldd1-sisb2su[[[v]nlb1-dsbldli-<g]dsgx[[>]nlb1-dsblu+0<t]dstx[]plu2+suli1-dsilddsb2/!>m]dsmxld3*dsdsolidswst[[[v]nlw1-dsw0!>x]dsxx[2-]sa[2+]sy[[>]nlo1-dso0<g]dsgxldltli1+2/<yltli1+2/>adsdsolisw[]plt1-dst0!>f]dsfx

All right, well...this is massive. It wasn't easy, I'll tell you that much, what with all the register references, comparisons, macros, lack of string manipulation abilities, etc. Nonetheless, I had fun putting this together, and that's all that really matters, right? ;)

Try It Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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