# Validate 2Col syntax!

As a couple of people may have noticed lately, I've largely abandoned development of Braingolf because it's boring and uninspired, and moved on to 2Col which is a little more interesting, and not designed to be a golfing language.

The defining feature of 2Col is that every line of code must be exactly 2 characters long, excluding the newline. This means that the length of a 2Col program can always be calculated as 3n-1 where n is the number of lines in the program.

So here's my challenge: Given 2Col code as a string, output truthy if it is valid 2Col code (Every line is exactly 2 characters and it conforms with the 3n-1 formula), and falsey otherwise.

## Input

Input should be taken as a single string, or an array of characters.

## Output

A truthy value if the input string is valid layout, and a falsey value otherwise.

Your code should be consistent in which truthy/falsey values it uses

## Testcases

======
F!
$^ ---- truthy ====== ====== *8 +1 Sq ---- truthy ====== ====== nop xt ---- falsey ====== ====== +1 +1 #^ ---- falsey ====== ====== <empty string> ---- falsey ====== ====== ye ---- truthy ====== ====== no<space> ---- falsey ====== ====== test ---- falsey ====== ====== puzzle ---- falsey ======  ## Scoring This is so fewest bytes wins! • @Shaggy no, not unless for some reason the language you use considers true falsey and false truthy – Skidsdev May 25 '17 at 18:13 • @StephenS Will do – Skidsdev May 25 '17 at 18:13 • @Mayube thanks, sorry, I forgot to add "could you" in front of that xD – Stephen May 25 '17 at 18:21 • I suggest you add a test case: puzzle. This will make solutions that do the whole length of the string modulo 3, then negated (which works for all the current test cases) invalid. – Comrade SparklePony May 25 '17 at 19:41 • @ComradeSparklePony will do – Skidsdev May 25 '17 at 21:10 ## 14 Answers # Brachylog (2), 4 bytes ṇl₂ᵐ  Try it online! Full program (because this is a ; Brachylog full programs output false. if there was an assertion failure, true. without one). ## Explanation ṇl₂ᵐ ṇ Split input into lines ᵐ For each line: l₂ Assert that that line has length 2  Subscripts on l are one of Brachylog's newest features (although still older than the challenge), and this is a good challenge to use them on. • Although ṇlᵛ2 and ṇlᵛ² would also work. – Unrelated String Mar 9 at 8:28 # JavaScript (ES6), 242825 24 bytes Fixed program and shaved of three bytes thanks to @PunPun1000 Shaved off one byte thanks to @Shaggy s=>/^(..\n)*..$/.test(s)


Returns true if valid and false if not.

f=
s=>/^(..\n)*..$/.test(s) t= 22 22 22 22 22 console.log(f(t)); console.log(f(t.slice(0, -1))); • Just looking at this I don't think it works for two of the test cases (the truthy one with only 2 characters and no new line, and the falsey one with a trailing newline). However s=>s.match(/^(..\n)*..$/) should correctly match both of those while also being shorter at 25 bytes – PunPun1000 May 25 '17 at 18:46
• @PunPun1000 thank you, you are correct. – Stephen May 25 '17 at 19:15
• Unfortunately this is invalid as the 2 output values aren't consistent. However, you should be able to fix that and save a byte by using test instead of match. – Shaggy May 28 '17 at 23:05
• @Shaggy thanks - the reason I didn't see that when I answered because that was just edited in – Stephen May 29 '17 at 1:05
• I know, that's why I pointed it out ;) You might want to update the note on your return values. – Shaggy May 29 '17 at 9:24

# Cubix, 20 bytes

Returns 1 for truthy and nothing for falsey

@1OuABq>;;?w-!$@;U_N  Cubified  @ 1 O u A B q > ; ; ? w - !$ @ ; U _ N
. .
. .

• ABq slurp in all the input, reverse it and push the EOI(-1) to the bottom of the stack
• >;; Step into the loop and remove items from the stack
• ? Test for EOI(-1).
• If found 1uO@ push 1 to the stack, u-turn onto integer output and halt
• Otherwise _ reflect back onto the ? which redirects to the w lane shift
• N-!$@;U push line feed (10) onto the stack, subtract, test result, skip the halt if false, remove the result and u-turn • ;;> remove the line feeds from the stack and redirect into the loop. Try it online! # Python, 51 lambda s:all(len(l)==2for l in(s+"\n").splitlines())  Test case runner: tcs = { "F!\n$^": 1,
"*8\n+1\nSq": 1,
"nop\nxt": 0,
"+1\n+1\n#^\n": 0,
"": 0,
"ye": 1,
"no ": 0,
"test": 0,
"puzzle": 0
}
f = lambda s:all(len(l)==2for l in(s+"\n").splitlines())
for tc, expected in tcs.items():
assert f(tc) == expected


all((==2).length).lines.(++"\n")


I got my inspiration from some other solutions, clever trick adding that "\n".

• I've fixed it, but RIP my short solution. – Program man May 26 '17 at 13:48

# Jelly, 6 bytes

ỴL€=2Ṃ


Try it online!

Explanation:

ỴL€=2Ṃ
Ỵ       Split at newlines
L€     Length of each...
=2   ...equals two.
Ṃ  Minimum.


^..(¶..)*$ Try it online! Outputs 1 or 0 as appropriate. # JavaScript (ES6), 35 24 bytes s=>!/^.?$|.../gm.test(s)


## Try it

f=
s=>!/^.?$|.../gm.test(s) oninput=_=>o.innerText=f(i.value) o.innerText=f(i.value=F!$^)
<textarea id=i></textarea><pre id=o>

• There's gotta be a shorter way to do this with RegEx! Yup (and mine is probably not optimal) – Stephen May 25 '17 at 16:49

# 05AB1E, 6 bytes

¶¡D2ùQ


Try it online!

¶¡D2ùQ   Argument s
¶¡       Split s on newlines
D      Duplicate
2ù    Keep only elements of length 2
Q   Compare


# J-uby, 19 18 bytes

:=~&/^(..\n*)..$/m  :=~& makes an anonymous function that takes x and returns 0 if it matches the regex /^(..\n*)..$/m, or nil otherwise.

# Java (OpenJDK 8), 25 bytes

s->s.matches("(..\n)*..")


Try it online!

Checks if the input string has any number of lines followed by a line feed and a final line without one (ensures at least one line)

# Bash + GNU utilities, 13

grep -qv ^..$ This sets the shell return value (accessible in $?) to 0 for false and 1 for true. This is actually the opposite sense compared to normal shell convention, so to make that right you'd need to do:

! grep -qv ^..$ # Ruby, 22 bytes ->s{s=~/^(..\n*)..$/m}


# Japt, 7 6 bytes

·eÈÊ¶2


Try it online

## Explanation

     :Implicit input of string "U"
·    :Split to array on newline
eÈ   :Maps over the array, checking that every item's ...
Ê    :length ...
¶2   :Equals 2
:Implicit output of result