11
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ @Shaggy no, not unless for some reason the language you use considers true falsey and false truthy \$\endgroup\$ – Skidsdev May 25 '17 at 18:13
  • 1
    \$\begingroup\$ @StephenS Will do \$\endgroup\$ – Skidsdev May 25 '17 at 18:13
  • \$\begingroup\$ @Mayube thanks, sorry, I forgot to add "could you" in front of that xD \$\endgroup\$ – Stephen May 25 '17 at 18:21
  • \$\begingroup\$ 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. \$\endgroup\$ – Comrade SparklePony May 25 '17 at 19:41
  • \$\begingroup\$ @ComradeSparklePony will do \$\endgroup\$ – Skidsdev May 25 '17 at 21:10

14 Answers 14

6
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Although ṇlᵛ2 and ṇlᵛ² would also work. \$\endgroup\$ – Unrelated String Mar 9 at 8:28
3
\$\begingroup\$

JavaScript (ES6), 24 28 25 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)));

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – PunPun1000 May 25 '17 at 18:46
  • \$\begingroup\$ @PunPun1000 thank you, you are correct. \$\endgroup\$ – Stephen May 25 '17 at 19:15
  • \$\begingroup\$ 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. \$\endgroup\$ – Shaggy May 28 '17 at 23:05
  • \$\begingroup\$ @Shaggy thanks - the reason I didn't see that when I answered because that was just edited in \$\endgroup\$ – Stephen May 29 '17 at 1:05
  • \$\begingroup\$ I know, that's why I pointed it out ;) You might want to update the note on your return values. \$\endgroup\$ – Shaggy May 29 '17 at 9:24
2
\$\begingroup\$

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!

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

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
\$\endgroup\$
2
\$\begingroup\$

Haskell, 23 52 32 bytes

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

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

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

Jelly, 6 bytes

ỴL€=2Ṃ

Try it online!

Explanation:

ỴL€=2Ṃ
Ỵ       Split at newlines
 L€     Length of each...
   =2   ...equals two.
     Ṃ  Minimum.
\$\endgroup\$
1
\$\begingroup\$

Retina, 10 bytes

^..(¶..)*$

Try it online! Outputs 1 or 0 as appropriate.

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

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>

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

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
\$\endgroup\$
1
\$\begingroup\$

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.

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

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)

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

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:

Bash + GNU utilities, 15

! grep -qv ^..$
\$\endgroup\$
0
\$\begingroup\$

Ruby, 22 bytes

->s{s=~/^(..\n*)..$/m}
\$\endgroup\$
0
\$\begingroup\$

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
\$\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.