16
\$\begingroup\$

Antiferromagnetic ordering

Antiferromagnetism is what IBM researchers used to jump from a 1 terabyte disk to a 100 terabyte disk in the same amount of atoms.

In materials that exhibit antiferromagnetism, the magnetic moments of atoms or molecules, usually related to the spins of electrons, align in a regular pattern with neighboring spins (on different sublattices) pointing in opposite directions.

Your job is to write a program that draws the ordering of antiferromagnetic atoms like the picture shown above. You must be able to have at least four sets of pairs, though you may have more.

Each pair must be shown as follows, though they must be actual arrows:

 up  down
down  up
 up  down

Your output can be in ascii art or graphical output.

You can make only a function or a whole program, but it must take an input and draw that many pairs. Examples with only words:

Input: 1

 up  down
down  up
 up  down

Input: 2

 up  down  up  down 
down  up  down  up  
 up  down  up  down

Acceptable arrows:

  • and
  • and
  • /|\ and \|/

Please put your answers in Language, X bytes format, as it's easy to read. The least amount of bytes wins!

\$\endgroup\$
  • 5
    \$\begingroup\$ "Any arrows" sounds pretty ambiguous to me - what about ^v? \$\endgroup\$ – Sp3000 Jul 10 '15 at 7:27
  • 1
    \$\begingroup\$ @Sp3000 No, cause they don't have the little tails. \$\endgroup\$ – phase Jul 10 '15 at 7:30
  • 2
    \$\begingroup\$ Are these arrows allowed: and ? (unicode code points U+21C5 and U+21F5) \$\endgroup\$ – Digital Trauma Jul 10 '15 at 16:28
  • 1
    \$\begingroup\$ @DigitalTrauma They are perfect! \$\endgroup\$ – phase Jul 10 '15 at 16:29
  • 11
    \$\begingroup\$ @Phase I rolled back your edit. Changing the scoring from bytes to chars will significantly change scores for a lot of these answers. Changing the rules after getting 15 answers is generally frowned upon. \$\endgroup\$ – Digital Trauma Jul 10 '15 at 16:40

30 Answers 30

16
\$\begingroup\$

APL, 18 12 bytes

⍉(2×⎕)3⍴'↑↓'

This constructs a 2n x 3 matrix, where n is the input (), filled with the characters and . The transpose () of this matrix is then printed.

You can try it online.

\$\endgroup\$
  • \$\begingroup\$ Nice abuse of the APL character set. I guess other answers can also use this character set, though. \$\endgroup\$ – jimmy23013 Jul 10 '15 at 15:26
  • 1
    \$\begingroup\$ @jimmy23013: The APL code page is EBCDIC-based. Not sure how many languages can handle that. \$\endgroup\$ – Dennis Jul 10 '15 at 15:38
  • \$\begingroup\$ @Dennis Can't the program itself be in ASCII (or some gibberish in EBCDIC) while it prints EBCDIC strings? The shortcut for newlines would be gone, though. Alternatively, the Windows console seemed to print \x18\x19 as ↑↓. \$\endgroup\$ – jimmy23013 Jul 10 '15 at 16:07
  • \$\begingroup\$ Or these codepages. \$\endgroup\$ – jimmy23013 Jul 10 '15 at 16:17
  • \$\begingroup\$ @jimmy23013: Yes, I just talked about old consoles in chat. The gibberish might work, but it's probably worth a meta discussion. \$\endgroup\$ – Dennis Jul 10 '15 at 16:31
12
\$\begingroup\$

Pyth, 15 bytes (11 chars)

V3.>*Q"↑↓"N

Try it online: Demonstration

Explanation:

              implicit: Q = input number
V3            for N in [0, 1, 2]:
      "↑↓"       string "↑↓"
    *Q           repeat Q times
  .>      N      rotate the string by N
\$\endgroup\$
12
\$\begingroup\$

Java, 313 296 bytes

Here's an example that displays arrows graphically:

import java.awt.*;void f(int n){new Frame(){public void paint(Graphics g){for(int k=0,l,m,o;k<n*6;o=k%6,l=o/2*10+32,m=k/6*20+(k++%2==0?19:29),g.fillPolygon(new int[]{m+4,m,m+4,m+4,m+6,m+6,m+10},o==1|o==2|o==5?new int[]{l+9,l+5,l+5,l,l,l+5,l+5}:new int[]{l,l+5,l+5,l+9,l+9,l+5,l+5},7));}}.show();}

In a more readable format:

import java.awt.*;
void f(int n) {
    new Frame() {
        public void paint(Graphics g) {
            for (int k = 0, l, m, o; k < n*6;){
                o = k % 6;
                l = o / 2 * 10 + 32;
                m = k / 6 * 20 + (k++ % 2 == 0 ? 19 : 29);
                g.fillPolygon(new int[] {m+4,m,m+4,m+4,m+6,m+6,m+10},
                              o == 1 || o == 2 || o == 5 ?
                                  new int[] {l+9,l+5,l+5,l,l,l+5,l+5} :
                                  new int[] {l,l+5,l+5,l+9,l+9,l+5,l+5},
                              7);
            }
        }
    }.show();
}

The display for 5 as input:

Display for 5 as input

You'll have to resize the window that appears to see the arrows. I tried to make it so that none of them would appear "chopped off" by the window's inside border, but it may appear that way on certain platforms.

\$\endgroup\$
9
\$\begingroup\$

CJam, 18 bytes (14 chars)

ri3*"↑↓"*3/zN*

Generate the columns (which form a repeating pattern) then transpose.

Try it online.


Alternative 18 bytes:

3,ri"↑↓"*fm>N*

Rotate the string "↑↓"*n by 0, 1 or 2 times.

\$\endgroup\$
7
\$\begingroup\$

CJam (15 chars, 19 bytes)

ri"↑↓"*_(+1$]N*

Online demo

\$\endgroup\$
  • 1
    \$\begingroup\$ Win condition is bytes, not chars. \$\endgroup\$ – isaacg Jul 10 '15 at 6:29
  • \$\begingroup\$ @PeterTaylor: The challenge specified Language, X bytes format. You have it in char format, but unicode characters are worth 2 bytes so your actual score is 17 bytes \$\endgroup\$ – Levi Jul 10 '15 at 6:40
  • \$\begingroup\$ @Levi According to this they're 3 bytes each. \$\endgroup\$ – isaacg Jul 10 '15 at 7:10
  • \$\begingroup\$ @isaacg ah my bad \$\endgroup\$ – Levi Jul 10 '15 at 7:18
7
\$\begingroup\$

Befunge, 71 bytes

My first answer, so please be gentle with me :o)

Annoying alignment issues resulted in a few wasted bytes, if you have any improvements for me I'd love to hear them!

&::3>:2% #v_0#v" \|/ "<
  >\^,*52<> 0#v" /|\ "<
:#^_$1-:#^_@  >:#,_$\1-

Input: 4

 /|\  \|/  /|\  \|/  /|\  \|/  /|\  \|/ 
 \|/  /|\  \|/  /|\  \|/  /|\  \|/  /|\ 
 /|\  \|/  /|\  \|/  /|\  \|/  /|\  \|/ 
\$\endgroup\$
6
\$\begingroup\$

CJam, 14 bytes

0000000: 332c 7269 2218 1922 2a66 6d3e 4e2a       3,ri".."*fm>N*

This requires a supporting terminal that renders code page 850 like this:

The non-pointy part of the code turned out to be identical to @Sp3000's alternative version.


CJam, 17 bytes

ri"⇅⇵⇅"f*N*

Cheaty double arrow version, with credits to @DigitalTrauma.

Try it online.

\$\endgroup\$
5
\$\begingroup\$

Pyth, 16 bytes (12 chars)

J"↑↓"V3*~_JQ

Example:

Input: 4
Output:
↑↓↑↓↑↓↑↓
↓↑↓↑↓↑↓↑
↑↓↑↓↑↓↑↓
\$\endgroup\$
5
\$\begingroup\$

Python 2, 131 122 bytes

from turtle import*
for k in range(input()*6):z=k/3+k%3&1;pu();goto(k/3*32,z*32^k%3*64);pd();seth(z*180+90);fd(32);stamp()

Well... I beat C Java I guess?

enter image description here


I've chosen height 32 for the arrows, which is pretty large, so after a while the turtle starts drawing offscreen. If you want everything to fit for large inputs, you can either make the arrows smaller by replacing the 32s, or use screensize() (I'm not sure if there's a meta post on offscreen output...)

\$\endgroup\$
  • 1
    \$\begingroup\$ So... when are going to add turtle graphics to Pyth? \$\endgroup\$ – Digital Trauma Jul 11 '15 at 4:37
  • \$\begingroup\$ Surely for golfing purposes you should choose a single digit number for the sizing... \$\endgroup\$ – Beta Decay Jul 13 '15 at 12:35
  • \$\begingroup\$ @BetaDecay For single digit sizing the tail is barely visible, since it's obscured by the turtle \$\endgroup\$ – Sp3000 Jul 13 '15 at 12:37
4
\$\begingroup\$

GNU sed, 25 bytes

I found the and unicode arrow symbols, which allow more shortening and they have been allowed by this comment:

h
s/1/⇅/g
H
G
s/1/⇵/g

Input is in unary, so e.g. 4 is 1111:

$ echo 1 | sed -f antiferro.sed
⇅
⇵
⇅
$ echo 1111 | sed -f antiferro.sed
⇅⇅⇅⇅
⇵⇵⇵⇵
⇅⇅⇅⇅
$ 

Previous answer in case and are disallowed:

GNU sed, 39 bytes

s/1/↑↓/g
s/.*/&a&↑\n&/
s/a↑/\n/
\$\endgroup\$
  • 3
    \$\begingroup\$ Whenever I see "GNU sed" at the top of a post, I don't even need to scroll down to know who posted it. \$\endgroup\$ – Alex A. Jul 10 '15 at 17:22
  • \$\begingroup\$ @AlexA. This guy? ;-) \$\endgroup\$ – Digital Trauma Jul 10 '15 at 17:30
  • \$\begingroup\$ Input is in unary?! Is that a general for the language or something you programmed in? \$\endgroup\$ – Beta Decay Jul 12 '15 at 7:46
  • 1
    \$\begingroup\$ What a nefarious answer :-) \$\endgroup\$ – xebtl Jul 13 '15 at 8:16
  • 1
    \$\begingroup\$ @BetaDecay thats the unique property of unary strings - their numeric value is equal to their length. The meta question/answer allows for this strictly for languages that don't have native arithmetic (e.g. sed). This is particularly handy for this question, because the output of the required length can easily be generated from the unary input. Cheaty? perhaps - but the meta answer consensus seems to be OK with it. \$\endgroup\$ – Digital Trauma Jul 13 '15 at 15:46
3
\$\begingroup\$

Swift 2, 66 bytes

let f={n in(0..<n*3).map{print("↑↓",appendNewline:$0%n==n-1)}}

If Swift would be just a liiiitle bit less verbose, it wouldn't even be that bad for golfing (I'm looking at you, named parameter appendNewline)

\$\endgroup\$
3
\$\begingroup\$

Ruby 39 (or 44) characters, 43 (or 48) bytes

According to https://mothereff.in/byte-counter the arrow characters are 3 bytes each!

->(n){a=['↑↓'*n]*3;a[1]=a[1].reverse;a}

An anonymous function which returns an array. If the function has to print the array, it should end with puts a for 5 more bytes.

Example use

f=->(n){a=['↑↓'*n]*3;a[1]=a[1].reverse;a}

puts f.call(6)

Gives

↑↓↑↓↑↓↑↓
↓↑↓↑↓↑↓↑
↑↓↑↓↑↓↑↓
\$\endgroup\$
3
\$\begingroup\$

J, 41 35 32 bytes (28 characters)

3$(,:|.)(2*".1!:1[1)$ucp'↑↓'

I have never programmed anything in J so this took me a while, and it's most definitely not the best way to do it.

This waits for you to input a number when run before outputing the arrows.

\$\endgroup\$
  • \$\begingroup\$ What do you mean you haven't programmed anything in J? I seem to recall a certain J answer which got you over 1k rep. ;) \$\endgroup\$ – Alex A. Jul 10 '15 at 14:19
  • \$\begingroup\$ @AlexA. Doing simple arithmetics is not really what I would call programming. When I posted that answer I really didn't know anything about J besides the right to left priority \$\endgroup\$ – Fatalize Jul 10 '15 at 14:21
2
\$\begingroup\$

Javascript (ES6), 66 63 53 47 bytes (62 55 49 41 characters)

f=n=>`⇅
⇵
⇅`.replace(/./g,'$&'.repeat(n))

Props to Digital Trauma for finding the ⇅ and ⇵ characters and allowing me to shave off more bytes.

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

J, 30 bytes

|:((2*".1!:1<1),3)$ucp'↑↓'
\$\endgroup\$
2
\$\begingroup\$

C, 169 170 162 125 123 105 119 107 bytes

So, I though I might as well give this a go, even though this is obviously not the winner :)

Golfed:

n,i,j;main(){n=getchar();n=atoi(&n);for(;j++<3;){for(i=0;i++<n;)printf("%.3s ","⇅⇵"+(j%2)*3);puts("");}}

Ungolfed:

#include <stdio.h>
#include <stdlib.h>

/* n -> Number of columns, i & j -> Loop counters */
n,i,j;

main()
{
    /* Get the number of iterations from stdin */
    n = getchar();
    n = atoi(&n); /* Thanks @AndreaBiondo */

    for (; j++ < 3;)
    {
        /* Print rows of arrows */
        for (i = 0; i++ < n;)
            printf("%.3s ","⇅⇵" + (j % 2) * 3);

        /* Print a newline */
        puts("");
    }
}

Example:

Input: 4
⇵ ⇵ ⇵ ⇵ 
⇅ ⇅ ⇅ ⇅ 
⇵ ⇵ ⇵ ⇵ 

Update:

See it run here

\$\endgroup\$
  • \$\begingroup\$ You can do for(j=0;j++<3;) and the same with i \$\endgroup\$ – lirtosiast Jul 11 '15 at 1:26
  • \$\begingroup\$ @ThomasKwa aha... well spotted. thanks \$\endgroup\$ – Levi Jul 11 '15 at 1:30
  • \$\begingroup\$ i and j are globals, so they're initialized to zero. You can drop i=0 and j=0. \$\endgroup\$ – Andrea Biondo Jul 12 '15 at 12:36
  • \$\begingroup\$ Also, you can exploit little-endianess and zero initialization to use n as a buffer: n=getchar();n=atoi(&n); \$\endgroup\$ – Andrea Biondo Jul 12 '15 at 12:46
  • \$\begingroup\$ @AndreaBiondo when i remove i=0 and j=0, all the output is on one line. can you reproduce this? i'm using gcc 4.9.2 \$\endgroup\$ – Levi Jul 12 '15 at 12:52
2
\$\begingroup\$

Octave, 37 bytes

EDIT: corrected from the earlier stripe-antiferromagnetic version. Thanks @beta-decay for catching my mistake.

f=@(n)repmat(["⇅";"⇵";"⇅"],1,n)

Defines a function f(n). Sample output:

octave:4> f(1)
ans =

⇅
⇵
⇅

octave:5> f(5)
ans =

⇅⇅⇅⇅⇅
⇵⇵⇵⇵⇵
⇅⇅⇅⇅⇅
\$\endgroup\$
1
\$\begingroup\$

CoffeeScript, 60 bytes (58 chars)

Comprehensions make it easy without recursion:

f=(n,x='')->x+='\n⇵⇅'[i%(n+1)&&1+i%2]for i in[1..n*3+2];x
\$\endgroup\$
1
\$\begingroup\$

Ruby, 33 bytes

As a function:

f=->n{[s="↑↓"*n,s.reverse,s]}

Example:

> puts f[3]
↑↓↑↓↑↓
↓↑↓↑↓↑
↑↓↑↓↑↓

Ruby, 37 bytes

Full program which takes input from stdin:

puts s="↑↓"*gets.to_i,s.reverse,s
\$\endgroup\$
  • \$\begingroup\$ You can make only a function or a whole program, but it must take an input and draw that many pairs. \$\endgroup\$ – Dennis Jul 10 '15 at 17:27
  • \$\begingroup\$ @Dennis ok, I'm on it \$\endgroup\$ – daniero Jul 10 '15 at 17:37
  • \$\begingroup\$ We seem to have a misunderstanding. I posted the quote to show that a function is in fact valid, since you implied in your original revision that a full program was required by the question. \$\endgroup\$ – Dennis Jul 10 '15 at 17:49
  • \$\begingroup\$ @Dennis No problem. I was just thinking that returning 3 strings wasn't really "drawing", but I guess it doesn't matter. Anyways, got both versions golfed down a bit :) \$\endgroup\$ – daniero Jul 10 '15 at 18:22
1
\$\begingroup\$

><>, 55 Bytes

"⇅⇵⇅"{:&3*1-:0(?;\
|.!09v!?%&:&:{o:}/
oa{~}/|.!09

Try it online here, inputting the desired length as initial stack value.

Non ⇅⇵ solution, 59 Bytes:

"↓↑"{:&3*>1-:0(?;{:{\
 |.!09v!?%&:&:oo}}@:/
9oa{$}/|.!0
\$\endgroup\$
1
\$\begingroup\$

BBC BASIC, 70 bytes

INPUTx:n$=STRING$(x,"/|\\|/"):PRINTn$:PRINTSTRING$(x,"\|//|\"):PRINTn$

This can probably be golfed more

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

C, 97 bytes

Takes the input from the first command-line parameter, e.g. main 4. Supports up to 357913940 pairs. In C you can't use multibyte characters as chars but they work fine as strings.

i,n;main(c,v)char**v;{n=atoi(v[1]);for(i=6*n+3;i--;)printf("%s",i%(2*n+1)?i%2?"↓":"↑":"\n");}

It is smaller as a function, but the other C answers were complete programs so I did that too. It would be 69 bytes:

i;f(n){for(i=6*n+3;i--;)printf("%s",i%(2*n+1)?i%2?"↓":"↑":"\n");}
\$\endgroup\$
1
\$\begingroup\$

Python 2, 47 bytes

Port of my BBC BASIC answer, taking advantage of how Python can easily reverse strings.

n=r"/|\\|/"*input();print n+"\n"+n[::-1]+"\n"+n
\$\endgroup\$
1
\$\begingroup\$

C, 117 89 85 bytes

i;main(j,v)char**v;{j=2*atol(v[1])+1;for(;i++<3*j;)printf(i%j?i%2?"↑":"↓":"\n");}

Ungolfed:

i;
main(j,v)
char**v; // Credit to @AndreaBiondo for brilliant idea that I will use a lot in future golfed programs :)
{
    j = 2*atol(v[1])+1;
    for(;i++<3*j;)
        printf(i%j?i%2?"↑":"↓":"\n");
}
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 66 bytes (62 chars)

That includes the Unicode character counted as three bytes each as well as the mandatory newline counted as one byte.

Uses recursion as inspired by this answer. I tried it non-recursively but generating a defined array took too many characters, although someone else might know how to do it better than me.

f=n=>(g=(a,i)=>i?g(`
↓↑`[i%(n*2+1)&&1+i%2]+a,i-1):a)('',n*6+2)

Demo

As with all ES6 answers, they are demonstrable in Firefox, Edge, and Safari 9 only at time of writing:

f = n => (g = (a, i) => i ? g(`
↓↑` [i % (n * 2 + 1) && 1 + i % 2] + a, i - 1) : a)('', n * 6 + 2)

console.log = x => document.getElementById('O').innerHTML += x + '\n';

console.log(f(1));
console.log(f(2));
console.log(f(4));
console.log(f(32));
<pre><output id=O></output></pre>

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

Java, 150 bytes

static void g(int n){n*=2;f(n,0);f(n,1);f(n,0);}static void f(int n,int d){String l="";for(n+=d;n-->d;)l+=(n%2==0)?"↓":"↑";System.out.println(l);}

Output of g(2):

↑↓↑↓
↓↑↓↑
↑↓↑↓
\$\endgroup\$
  • 1
    \$\begingroup\$ I think that you're required to output arrows, so I'd suggest that you change your example output and code to make it absolutely clear that you aren't breaking the rules :) \$\endgroup\$ – Beta Decay Jul 12 '15 at 18:51
  • \$\begingroup\$ @BetaDecay I see some approved arrows have been listed and I haven't made the cut! So following your advice I've changed it \$\endgroup\$ – DeadChex Jul 12 '15 at 18:58
0
\$\begingroup\$

Python 2, 45 55 bytes

edit: modified arrows

Pretty straightforward approach. Doesn't work with unicode arrows, though.

def f(n):x=" /|\\ \\|/"*n;print x+"\n "+x[::-1]+"\n"+x
\$\endgroup\$
  • \$\begingroup\$ if you look at the comments in the original post, you will see that you are specifically not allowed to use ^v arrows as they do not have a tail \$\endgroup\$ – Levi Jul 10 '15 at 8:29
  • 1
    \$\begingroup\$ Thanks, I haven't noticed that. Changed it into "/|\", hope it's ok now. \$\endgroup\$ – heo Jul 10 '15 at 9:19
0
\$\begingroup\$

R, 60 62 bytes

Takes input from STDIN and outputs to STDOUT. Creates a 3 x (2 * input) array, adds a column of carriage returns and then outputs the transposed array.

cat(t(cbind(array(c('↑','↓'),c(3,2*scan())),'\n')),sep='')

Test run

cat(t(cbind(array(c('↑','↓'),c(3,2*scan())),'\n')),sep='')
1: 25
2: 
Read 1 item
↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓
↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑
↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓
\$\endgroup\$
0
\$\begingroup\$

Tcl 49 bytes

No winner but at least it's competitive:

foreach x {⇅ ⇵ ⇅} {puts [string repeat $x $argv]}
\$\endgroup\$
  • \$\begingroup\$ You can save bytes using lmap instead of foreach \$\endgroup\$ – sergiol Nov 20 '17 at 0:19
  • \$\begingroup\$ And string repeatcan be abbreviated to string re \$\endgroup\$ – sergiol Nov 20 '17 at 0:21
  • \$\begingroup\$ And I am not sure, but implementing it as a proc retrieving a side effect of lmap returning values and replacing $argv by someone one-letter variable may save bytes. \$\endgroup\$ – sergiol Nov 20 '17 at 0:24
-1
\$\begingroup\$

Swift 2.0, 79 bytes

Nothing clever...

let p=3;for x in 0...2{print((0..<p*2).reduce(""){$0+["↑","↓"][($1+x)%2]})}
\$\endgroup\$
  • 3
    \$\begingroup\$ This would need to be put in a function, having to change the code for input isn't allowed \$\endgroup\$ – Downgoat Jul 11 '15 at 5:29

protected by Community Jul 13 '15 at 13:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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