18
\$\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\$
12
  • 6
    \$\begingroup\$ "Any arrows" sounds pretty ambiguous to me - what about ^v? \$\endgroup\$
    – Sp3000
    Commented Jul 10, 2015 at 7:27
  • 1
    \$\begingroup\$ @Sp3000 No, cause they don't have the little tails. \$\endgroup\$
    – jado
    Commented Jul 10, 2015 at 7:30
  • 3
    \$\begingroup\$ Are these arrows allowed: and ? (unicode code points U+21C5 and U+21F5) \$\endgroup\$ Commented Jul 10, 2015 at 16:28
  • 1
    \$\begingroup\$ @DigitalTrauma They are perfect! \$\endgroup\$
    – jado
    Commented Jul 10, 2015 at 16:29
  • 13
    \$\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\$ Commented Jul 10, 2015 at 16:40

33 Answers 33

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\$
5
  • \$\begingroup\$ Nice abuse of the APL character set. I guess other answers can also use this character set, though. \$\endgroup\$
    – jimmy23013
    Commented Jul 10, 2015 at 15:26
  • 1
    \$\begingroup\$ @jimmy23013: The APL code page is EBCDIC-based. Not sure how many languages can handle that. \$\endgroup\$
    – Dennis
    Commented Jul 10, 2015 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
    Commented Jul 10, 2015 at 16:07
  • \$\begingroup\$ Or these codepages. \$\endgroup\$
    – jimmy23013
    Commented Jul 10, 2015 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
    Commented Jul 10, 2015 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\$
0
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\$
4
  • 1
    \$\begingroup\$ Win condition is bytes, not chars. \$\endgroup\$
    – isaacg
    Commented Jul 10, 2015 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
    Commented Jul 10, 2015 at 6:40
  • \$\begingroup\$ @Levi According to this they're 3 bytes each. \$\endgroup\$
    – isaacg
    Commented Jul 10, 2015 at 7:10
  • \$\begingroup\$ @isaacg ah my bad \$\endgroup\$
    – Levi
    Commented Jul 10, 2015 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\$
0
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\$
3
  • 1
    \$\begingroup\$ So... when are going to add turtle graphics to Pyth? \$\endgroup\$ Commented Jul 11, 2015 at 4:37
  • \$\begingroup\$ Surely for golfing purposes you should choose a single digit number for the sizing... \$\endgroup\$
    – Beta Decay
    Commented Jul 13, 2015 at 12:35
  • \$\begingroup\$ @BetaDecay For single digit sizing the tail is barely visible, since it's obscured by the turtle \$\endgroup\$
    – Sp3000
    Commented Jul 13, 2015 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\$
8
  • 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.
    Commented Jul 10, 2015 at 17:22
  • \$\begingroup\$ @AlexA. This guy? ;-) \$\endgroup\$ Commented Jul 10, 2015 at 17:30
  • \$\begingroup\$ Input is in unary?! Is that a general for the language or something you programmed in? \$\endgroup\$
    – Beta Decay
    Commented Jul 12, 2015 at 7:46
  • 1
    \$\begingroup\$ What a nefarious answer :-) \$\endgroup\$
    – xebtl
    Commented Jul 13, 2015 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\$ Commented Jul 13, 2015 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\$
0
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\$
2
  • \$\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.
    Commented Jul 10, 2015 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
    Commented Jul 10, 2015 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\$
8
  • \$\begingroup\$ You can do for(j=0;j++<3;) and the same with i \$\endgroup\$
    – lirtosiast
    Commented Jul 11, 2015 at 1:26
  • \$\begingroup\$ @ThomasKwa aha... well spotted. thanks \$\endgroup\$
    – Levi
    Commented Jul 11, 2015 at 1:30
  • \$\begingroup\$ i and j are globals, so they're initialized to zero. You can drop i=0 and j=0. \$\endgroup\$ Commented Jul 12, 2015 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\$ Commented Jul 12, 2015 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
    Commented Jul 12, 2015 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\$
0
2
\$\begingroup\$

05AB1E, 17 11 bytes

ƵηSnŽXæ+ç×»

Bug-fixed and -6 bytes thanks to @ovs.

Uses and .

Try it online or verify some more test cases.

Slightly larger simplified approach - 15 bytes in UTF-8 (8 characters):

"⇅⇵"ĆS×»

Try it online or verify some more test cases.

Explanation:

Unfortunately the 05AB1E codepage doesn't contain the arrow characters, so we have to create them ourselves or use UTF-8 encoding for the entire program.

Ƶη           # Push compressed integer 171
  S          # Convert it to a list of digits: [1,7,1]
   n         # Square each: [1,49,1]
    ŽXæ+     # Add compressed integer 8644 to each: [8645,8693,8645]
        ç    # Convert them to characters with these codepoints: ["⇅","⇵","⇅"]
         ×   # Repeat each character the (implicit) input amount of times as strings
          »  # And join this list of strings by newlines
             # (after which it is output implicitly as result)

"⇅⇵"        # Push string "⇅⇵"
     Ć       # Enclose, appending it's own head: "⇅⇵⇅"
      S      # Convert it to a list of characters: ["⇅","⇵","⇅"]
       ×»    # Same as above

See this 05AB1E tip of mine (section How to compress large integers?) to understand why Ƶη is 171 and ŽXæ is 8644.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think is meant as two arrows, one up and one down. This allows for a shorter solution: tio.run/##AR0A4v9vc2FiaWX//8a1zrdTbsW9WMOmK8Onw5fCu///Mw \$\endgroup\$
    – ovs
    Commented Oct 9, 2020 at 10:39
  • \$\begingroup\$ @ovs Oops.. I read it correctly, but implement it incorrectly. Not sure how I didn't notice that.. And thanks. I especially like that ƵηSn. \$\endgroup\$ Commented Oct 9, 2020 at 11:42
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\$
4
  • \$\begingroup\$ You can make only a function or a whole program, but it must take an input and draw that many pairs. \$\endgroup\$
    – Dennis
    Commented Jul 10, 2015 at 17:27
  • \$\begingroup\$ @Dennis ok, I'm on it \$\endgroup\$
    – daniero
    Commented Jul 10, 2015 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
    Commented Jul 10, 2015 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
    Commented Jul 10, 2015 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\$
0
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\$
2
  • 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
    Commented Jul 12, 2015 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
    Commented Jul 12, 2015 at 18:58
1
\$\begingroup\$

Japt -R, 13 bytes

Went through a good few different methods but couldn't do better than 16 then I realised a port of Kevin's solution would come in a bit shorter.

#«ì £çX²d8644

Try it

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

Vyxal Dj, 11 bytes

‛↑↓3*½ÞTvṅ*

Try it Online!

-1 thanks to Aaron Miller

\$\endgroup\$
1
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\$
2
  • \$\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
    Commented Jul 10, 2015 at 8:29
  • 1
    \$\begingroup\$ Thanks, I haven't noticed that. Changed it into "/|\", hope it's ok now. \$\endgroup\$
    – heo
    Commented Jul 10, 2015 at 9:19

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.