27
\$\begingroup\$

Your task is to build a program that identifies the shape of the input. The shapes to be identified can be any of the following:

Square

To be identified as a square, the source must have lines of all equal length, and the same number of lines as characters per line (newline characters excluded). An optional trailing newline is acceptable.

$_='
$_="
$_"'
;say

Rectangle

To be identified as a rectangle, the source must have lines of all equal length, but the number of lines does not match the number of characters per line (newline characters excluded). An optional trailing newline is acceptable. This can be either horizontal or vertical.

$_=
"no
t a
squ
are
";#
$_="but it
is still a
consistent
shape!";##

Triangle

To be identified as a triangle, the source must either start with one character, and each subsequent line must have one additional character (including the last), or after the first line, each subsequent line should have one character fewer until the last, which has only one.

$
_=
"So
this
"."".
shape;
$_="or
even,
this
way
!!
"

Mess

Anything that doesn't follow a consistent format as per the above, must be identified as a mess.

Rules

  • You may return any four consistent printable values to identify each shape.
  • Your source code must also adhere to one of the above shapes (no, not a mess).
  • A single trailing newline in your source is acceptable.
  • You can assume input does not contain any blank lines (including trailing newlines), is not empty, and does not consist only of newlines.
  • All shapes must have a height and width of>= 2, otherwise this is defined as a mess.
  • Standard loopholes are forbidden.
  • The shortest solution in bytes, in each language, wins.
asked Mar 23, 2018 at 8:30
\$\endgroup\$
20
  • \$\begingroup\$ "Your source code must also adhere to one of the above shapes" does it mean one liner is just fine? \$\endgroup\$ Commented Mar 23, 2018 at 8:44
  • 1
    \$\begingroup\$ @ tsh All shapes must have a height and width of >= 2. \$\endgroup\$ Commented Mar 23, 2018 at 8:45
  • 1
    \$\begingroup\$ The input can be an array? for example, a square ['abc','cfd','fgh']? \$\endgroup\$ Commented Mar 23, 2018 at 12:23
  • 1
    \$\begingroup\$ @recursive updated, thank you! \$\endgroup\$ Commented Mar 23, 2018 at 17:02
  • 3
    \$\begingroup\$ You are telling me my source code can't be a mess? why not?!?! \$\endgroup\$ Commented Mar 23, 2018 at 23:31

21 Answers 21

9
\$\begingroup\$

Jelly, 35 bytes

L€ṀR,Ṛ$ċƲȧ3
L,ドル;\LE€S+Ç
ỴμZL«L>1ȧÇ 

Try it online!

0 = Mess
1 = Rectangle
2 = Square
3 = Triangle

answered Mar 23, 2018 at 9:08
\$\endgroup\$
2
  • \$\begingroup\$ Is the space on your last line used by your code? Or is that just padding to meet the "rectangle" criteria? \$\endgroup\$ Commented Mar 23, 2018 at 18:00
  • \$\begingroup\$ @BradC The latter. I should probably add an explanation. \$\endgroup\$ Commented Mar 23, 2018 at 19:05
8
\$\begingroup\$

Brachylog, 45 bytes

lm{≥1|≤1}o{l>1&t>1&}↰3
lg,?=∧1w|=∧2w|t⟦1≡?∧3w

Try it online!

Code is a rectangle (despite the way it renders on my screen). Outputs: 1 for square, 2 for rectangle, 3 for triangle, and nothing for mess


Explanation:

lm{≥1|≤1}o{l>1&t>1&}↰3
lg,?=∧1w|=∧2w|t⟦1≡?∧3w
lm Get the length of each string
 { } Verify 
 ≥1 The list is non-increasing
 | or...
 ≤1 The list is non-decreasing
 o Sort it to be non-decreasing
 { } Verify
 l>1 The number of lines is greater than 1
 & and...
 t>1& The longest line is longer than 1 character
 ↰3 Call the following
lg,? Join the number of lines with the line lengths
 =∧1w If they are all equal, print 1 (Square)
 |=∧2w Or if just the line lengths are equal, print 2 (Rectangle)
 |t⟦1 Or if the range [1, 2, ... <longest line length>]
 ≡? Is the list of lengths
 ∧3w Print 3 (triangle)
 Otherwise print nothing (mess)
answered Mar 23, 2018 at 19:09
\$\endgroup\$
8
\$\begingroup\$

Java 10, (削除) 231 (削除ここまで) (削除) 221 (削除ここまで) (削除) 219 (削除ここまで) (削除) 217 (削除ここまで) (削除) 213 (削除ここまで) (削除) 211 (削除ここまで) (削除) 207 (削除ここまで) 203 bytes

s->{var a=s.split("\n");int r=a.length,l=a[0].length(),R=0,i=1,L,D;if(r>1){for(L=a[1].length(),D=L-l;
++i<r;R=L-a[i-1].length()!=D?1:R)L=a[i].length();R=R<1?D==0?r==l?1:2:D*D>1|l>1&L>1?0:3:0;}return R;};

Function is a rectangle itself.
1 = Squares; 2 = Rectangles; 3 = Triangles; 0 = Mess.

-14 bytes thanks to @OlivierGrégoire
-4 bytes thanks to @ceilingcat

Explanation:

Try it online.

s->{ // Method with String parameter and integer return-type
 var a=s.split("\n"); // Input split by new-lines
 int r=a.length, // Amount of lines
 l=a[0].length(), // Length of the first line
 R=0, // Result-integer, initially 0
 i=1, // Index integer, starting at 1
 L,D; // Temp integers
 if(r>1){ // If there are at least two lines:
 for(L=a[1].length(), // Set `L` to the length of the second line
 D=L-l; // And set `D` to the difference between the first two lines
 ++i<r; // Loop over the array
 ; // After every iteration:
 R=L-a[i-1].length()// If the difference between this and the previous line
 !=D? // is not equal to the difference of the first two lines:
 1 // Set `R` to 1
 : // Else:
 R) // Leave `R` the same
 L=a[i].length(); // Set `L` to the length of the current line
 R=R<1? // If `R` is still 0:
 D==0? // And if `D` is also 0:
 r==l? // And the amount of lines and length of each line is equal:
 1 // It's a square, so set `R` to 1
 : // Else:
 2 // It's a rectangle, so set `R` to 2
 :D*D>1& // Else-if `D` is NOT 1 nor -1,
 l>1&L>1? // and neither `l` nor `L` is 1:
 0 // Set `R` to 0, since it's a mess
 : // Else (`D` is 1 or -1, and `l` or `L` is 1):
 3 // It's a triangle, so set `R` to 3
 :0;} // In all other cases it's a mess, so set `R` to 0
 return R;} // Return the result `R`
; // No-op to make the method a rectangle
answered Mar 23, 2018 at 8:45
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Fixed for 221 bytes: s->{var a=s.split("\n");int S=a.length,l=a[0].length(),L,D,b=0,i=1;if(S<2)return 0;for(L=a[1].length(),D=L-l; b<1&++i<S;)if((L=a[i].length())-a[i-1].length()!=D)b=1;return b<1?D==0?S==l?1:2:D==-1|D==1?l==1|L==1?3:0:0:0;} (double space after var, line break after D=L-l;. \$\endgroup\$ Commented Mar 23, 2018 at 11:25
  • \$\begingroup\$ @OlivierGrégoire Thanks. And I golfed two more bytes by changing D==-1|D==1 to D>-2|D<2. That one and the l==1|L==1 might be more golfable with some bitwise operations, but that's not really my expertise. \$\endgroup\$ Commented Mar 23, 2018 at 12:43
  • 1
    \$\begingroup\$ 207 bytes: s->{var a=s.split("\n");int r=a.length,l=a[0].length(),L,D,b=0,i=1;if(r>1){for(L=a[1].length(),D=L-l;++ i<r;b=L-a[i-1].length()!=D?1:b)L=a[i].length();b=b<1?D==0?r==l?1:2:D>-2&D<2&(l<2|L<2)?3:0:0;}return b;} (break after D=L-l;++). Still golfable by merging the loop and the statement afterwards in one, but I don't see how right now. \$\endgroup\$ Commented Mar 23, 2018 at 13:02
7
\$\begingroup\$

Jelly, (削除) 32 (削除ここまで) 27 bytes

,U=€JẸ,E;SÆ2$
ZL«L’aL€Ç$æAƝ

Try it online!

Now taking input at a list of lines and switched &g×ばつ with ’a and using SÆ2 after L€ instead of FLÆ2Ɗ. These allowed me to condense into two lines and I saved 5 bytes in total. The following values are the same as before.

[0.0, 0.0]=Mess
[0.0, 1.5707963267948966]=Rectangle
[0.0, 0.7853981633974483]=Square
[1.5707963267948966, 0.0]=Triangle


ZL«L gets the minimum of height and width and subtracts 1 from it. Ç calls the second link and at the end if the input is a single line the result of Ç gets logical ANDed with the previous number if there is only a single line the output will be [0.0, 0.0].

In the second link: ,U yields a list of line lengths paired with it's reverse. J is range(number of lines) and =€ checks whether each of them are equal to the result of J. (Any) yields 1 if the input is a triangle.

E checks if all line lengths are equal (rectangle/square).

SÆ2 with a $ to group them into a single monad checks whether the total number of characters is a square number.

So at the end of the second link we have [[a,b],c] where each number is 0 or 1 indicating whether the input is a triangle, rectangular, and has square number of characters respectively.

However a square number of elements doesn't imply the input is a square since an messy input like

a3.
4

has a square number of elements but isn't a square.

This is where æA (arctan2) comes in. 0æA0 == 0æA1 == 0. In other words, if the input has square number of elements but is not a rectangle, then it is not a square. There are certainly more clear ways to do this but what does that matter when we have bytes to think about and we are allowed consistent arbitrary output.

Note I was previously using æA/ instead of æAƝ (and a , instead of a ; in the second link) but the former method distinguishes between triangles that have square number of elements and those that don't but they should obviously be counted as the same thing.

answered Mar 23, 2018 at 21:05
\$\endgroup\$
6
  • \$\begingroup\$ I was looking at the numbers thinking, they seem vaguely familiar... \$\endgroup\$ Commented Mar 23, 2018 at 21:21
  • \$\begingroup\$ @DomHastings Haha. I was having trouble distinguishing squares from square-number-of-element messes and arctan2 was exactly what I needed. \$\endgroup\$ Commented Mar 23, 2018 at 22:29
  • 1
    \$\begingroup\$ Funny that I don't think this would be any shorter if there was no source restriction \$\endgroup\$ Commented Mar 27, 2018 at 17:33
  • \$\begingroup\$ ... Are you sure this is valid? As newline in Jelly is 0x7F, not 0x0A. \$\endgroup\$ Commented Apr 2, 2018 at 2:16
  • \$\begingroup\$ @DomHastings Is this valid? (see reason above) \$\endgroup\$ Commented Apr 2, 2018 at 2:16
6
\$\begingroup\$

Python 2, (削除) 129 (削除ここまで) (削除) 114 (削除ここまで) (削除) 109 (削除ここまで) (削除) 107 (削除ここまで) 113 bytes

l=map(len,input().split('\n'));m=len(
l);r=range(1,m+1);print[[1],0,r,r[::-
1],[m]*m,0,[max(l)]*m,l].index(l)%7/2

Try it online!


Prints

  • 0 = Mess
  • 1 = Triangle
  • 2 = Square
  • 3 = Rectangle
answered Mar 23, 2018 at 9:30
\$\endgroup\$
0
4
\$\begingroup\$

Java 10, (削除) 274 (削除ここまで) (削除) 323 (削除ここまで) (削除) 298 (削除ここまで) 229 bytes

First triangle submission.

s
->
{ 
var 
a=s. 
split 
("\n");
int i,l=
a.length,
c,f=a[0]. 
length(),r=
l<2||f<2&a[1
].length()<2?
0:f==l?7:5;var
b=f==1;for(i=1;
i<l;){c=a[i++]. 
length();r&=c!=f?
4:7;r&=(b&c!=f+1)|
(!b&c!=f-1)?3:7;f=c
;}return r;} 

0 Mess

1 Rectangle

3 Square

4 Triangle

Try it online here.

Edited multiple times to golf it a bit more.

Of course I could save a lot of bytes by turning this into a rectangle as well ((削除) 281 (削除ここまで) (削除) 267 (削除ここまで) (削除) 259 (削除ここまで) 200 bytes, see here).

The result of the identification is manipulated using bitwise AND, yielding a bitmask as follows:

1 1 1
triangle square rectangle

Ungolfed version:

s -> {
 var lines = s.split("\n"); // split input into individual lines
 int i, // counter for the for loop
 numLines = lines.length, // number of lines
 current, // length of the current line
 previous = lines[0].length(), // length of the previous line
 result = numLines < 2 // result of the identification process; if there are less than two lines
 || previous < 2 & lines[1].length() < 2 // or the first two lines are both shorter than 2
 ? 0 : previous == numLines ? 7 : 5; // it's a mess, otherwise it might be a square if the length of the first line matches the number of lines
 var ascending = previous == 1; // determines whether a triangle is in ascending or descending order
 for(i = 1; i < numLines; ) { // iterate over all lines
 current = lines[i++].length(); // store the current line's length
 result &= current != previous ? 4 : 7; // check if it's not a rectangle or a square
 result &= (ascending & current != previous+1)|(!ascending & current != previous-1) ? 3 : 7; // if the current line is not one longer (ascending) or shorter (descending) than the previous line, it's not a triangle
 previous = current; // move to the next line
 }
 return result; // return the result
}
answered Mar 25, 2018 at 0:14
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Commented Mar 25, 2018 at 0:50
  • \$\begingroup\$ Hooray for triangles! Thanks! \$\endgroup\$ Commented Mar 25, 2018 at 4:55
  • \$\begingroup\$ Hi, welcome to PPCG! Great first answer. I tried making my answer a triangle before as well, but it would cost too many bytes in comparison to rectangle, and some key-words were a bit too long in my initial answer as well. :) Great answer though, +1 from me. And I took the liberty to edit your post to add highlighting to the entire post, so the comments in your ungolfed version are easier to read. Enjoy your stay! \$\endgroup\$ Commented Mar 25, 2018 at 10:39
  • \$\begingroup\$ @KevinCruijssen Thanks for the upvote and edit, it looks much better now. My answer could be shortened by turning it into a rectangle as well, 281 bytes. But where's the fun in that? \$\endgroup\$ Commented Mar 25, 2018 at 12:43
4
\$\begingroup\$

Jelly, 15 bytes

Jm0w,ET
ỴẈμLe|Ç

Try it online!

Returns [1] for triangles, [2] for rectangles, [3] for squares and [] for messes

How it works

ỴẈμLe|Ç - Main link. Takes a string S on the left
Ỵ - Split S into a list of lines
 Ẉ - Take the length of each line
 μ - Use this list of lengths W as the left and right argument:
 L - Length of W
 e - This length is in W?
 This returns 1 for triangles and squares and for messes with
 one (or more) lines of a length of the number of lines in the mess
 Ç - Call the helper link on W
 | - Logical OR
Jm0w,ET - Helper link. Takes a list of lengths W on the left
J - Replace W with it's indices ([1, 2, ..., len(W)])
 m0 - Reflect; [1, 2, ..., len(W), len(W), ..., 2, 1]
 w - Index of W as a sublist in this array, or 0
 This returns a non-zero number for triangles
 E - Are all elements of W equal?
 This returns 1 for squares and rectangles
 , - Pair
 T - Return the indices of non-zero elements
 This yields:
 - [1] for triangles
 - [2] for rectangles/squares
 - [] for messes

Taking the logical or between these results yields:

  • 1 | [1] = [1 | 1] = [1] for triangles
  • 0 | [2] = [0 | 2] = [2] for rectangles
  • 1 | [2] = [1 | 2] = [3] for squares
  • 1 | [] = [] or 0 | [] = [] for messes
answered Jan 13, 2021 at 18:05
\$\endgroup\$
3
\$\begingroup\$

Javascript 125 bytes

_=>(g=(l=_.split('\n').map(a=>a.length)).
length)<3?0:(r=l.reduce((a,b)=>a==b?a:0))
?r==g?2:1:l.reduce((a,b)=>++a==b?a:0)?3:0
0 = Mess
1 = Rectangle
2 = Square
3 = Triangle

fa=_=>(g=(l=_.split('\n').map(a=>a.length)).length)<3?0:(r=l.reduce((a,b)=>a==b?a:0))?r==g?2:1:l.reduce((a,b)=>++a==b?a:0)?3:0
var square = `asd
asd
asd`
var rectangle = `asd
asd
asd
asd
asd
asd`
var triangle = `asd
asdf
asdfg
asdfgh`
var mess = `asd
dasdasd
sd
dasasd`
console.log(fa(square), fa(rectangle), fa(triangle), fa(mess))

answered Mar 23, 2018 at 13:29
\$\endgroup\$
6
  • 3
    \$\begingroup\$ The byte count is 125 (including the newlines) \$\endgroup\$ Commented Mar 23, 2018 at 13:40
  • 1
    \$\begingroup\$ Triangle should go to a 1? not a 3456 \$\endgroup\$ Commented Mar 23, 2018 at 13:47
  • \$\begingroup\$ @l4m2 what do you mean? \$\endgroup\$ Commented Mar 23, 2018 at 13:48
  • 2
    \$\begingroup\$ triangle should always start at 1? \$\endgroup\$ Commented Mar 23, 2018 at 13:49
  • 3
    \$\begingroup\$ I think what @l4m2 is pointing out is that a triangle must have only one character on its first or last line, otherwise it's a "mess". \$\endgroup\$ Commented Mar 23, 2018 at 13:58
3
\$\begingroup\$

Perl 5 -p, 83 bytes

  • Mess: nothing
  • Square: 0
  • Triangle: 1
  • Rectangle: 2
($z)=grep++$$_{"@+"-$_*~-$.}==$.,0,/$/,-1
}{$.<2or$_=$$z{$z>0||$.}?$z%2:@F>1&&2x!$z

Try it online!

answered Mar 23, 2018 at 12:14
\$\endgroup\$
0
3
\$\begingroup\$

PHP, (削除) 195 (削除ここまで) 205 bytes

<?$a=$argv[1];$r=substr($a,-2,1)=="\n"?strrev($a):$a;foreach(explode("\n",$r)as$l){$s=strlen($l);$x[$s
]=++$i;$m=$i==$s?T:M;}$z=count($x);echo$i*$z>2?$z==1&&key($x)==$i?S:($z==1&&$i>2?R:($i==$z?$m:M)):M;?>

The upside down triangle adds an expensive 56 bytes to this!

Outputs are S,R,T,M

Saved a few bytes thanks to Dom Hastings.

Try it online!

Fixed a few issues now... Test runs produce this.

$_="
$_="
$_""
;say
RESULT:S
=============
$_=
"no
t a
squ
are
";#
RESULT:R
=============
$
_=
"So
this
"."".
shape;
RESULT:T
=============
$_="or
even,
this
way
!!
"
RESULT:T
=============
as
smiley
asd
A
RESULT:M
=============
X
RESULT:M
=============
XX
RESULT:M
=============
cccc
a
aa
cccc
RESULT:M
=============
answered Mar 23, 2018 at 9:58
\$\endgroup\$
2
  • \$\begingroup\$ Omit ?> should just be fine \$\endgroup\$ Commented Mar 23, 2018 at 10:02
  • \$\begingroup\$ This seems to return T for cccc\na\naa\ncccc Try it online! \$\endgroup\$ Commented Mar 23, 2018 at 12:44
3
\$\begingroup\$

Perl 6, 81 bytes

{.lines>>.chars.&{($_==.[0],3)[2*(2>.max
)+($_ Z- .skip).&{.[0].abs+.Set*2+^2}]}}

Try it online!

Returns True for square, False for rectangle, 3 for triangle, Nil for mess.

answered Mar 23, 2018 at 16:53
\$\endgroup\$
1
  • \$\begingroup\$ Very good, would you mind unpacking it a bit, in particular $_ Z- .skip? \$\endgroup\$ Commented Mar 24, 2018 at 19:03
3
\$\begingroup\$

Stax, 39 bytes

L{%m~;:-c:u{hJchC; 
|mb1=-C;%a\sI^^P}M0

Run and debug online!

Shortest ASCII-only answer so far.

0 - Mess
1 - Rectangle
2 - Square
3 - Triangle

Explanation

The solution makes use of the following fact: If something is explicitly printed in the execution of the program, no implicit output is generated. Otherwise, the top of stack at the end of the execution is implicitly output.

L{%m~;:-c:u{hJchC;|mb1=-C;%a\sI^^P}M0
L Collect all lines in an array
 {%m Convert each line to its length
 ~; Make a copy of the length array, put it on the input stack for later use
 :- Difference between consecutive elements.
 If the original array has only one line, this will be an empty array
 c:u Are all elements in the array the same?
 Empty array returns false
 { }M0 If last test result is true, execute block
 If the block is not executed, or is cancelled in the middle, implicitly output 0
 hJ The first element of the difference array squared (*)
 chC Cancel if it is not 0 or 1
 ;|m1= Shortest line length (**) is 1
 - Test whether this is the same as (*)
 Includes two cases:
 a. (*) is 1, and (**) is 1, in which case it is a triangle
 b. (*) is 0, and (**) is not 1, in which case it is a square or a rectangle
 C Cancel if last test fails
 ;% Number of lines
 a\ [Nr. of lines, (*)]
 I Get the 0-based index of (**) in the array
 0-> Square, 1->Triangle -1(not found) -> Rectangle
 ^^P Add 2 and print
answered Mar 23, 2018 at 13:55
\$\endgroup\$
3
\$\begingroup\$

Haskell, (削除) 113 (削除ここまで) (削除) 107 (削除ここまで) (削除) 103 (削除ここまで) 101 bytes

((#)=<<k).map k.lines;k=length;1#x=0;l#x|x==[1..l]
 ||x==[l,l-1..1]=3;l#x=k[1|z<-[l,x!!0],all(==z)x]

Try it online!

Returns 0, 1, 2 and 3 for mess, rectangle, square and triangle, respectively.

Edit: -2 bytes thanks to Lynn!

answered Mar 25, 2018 at 11:58
\$\endgroup\$
0
3
\$\begingroup\$

05AB1E, (削除) 35 (削除ここまで) (削除) 29 (削除ここまで) 27 bytes

Saved 8 bytes thanks to Magic Octopus Urn

DgV€g©ZU\ÄP®Y
QP®ËJCXY‚1›P*

Try it online!

0 = Mess
4 = Triangle
1 = Rectangle
3 = Square

answered Mar 23, 2018 at 13:40
\$\endgroup\$
8
  • \$\begingroup\$ This looks to fail on some messy code: Try it online! \$\endgroup\$ Commented Mar 23, 2018 at 17:33
  • \$\begingroup\$ @DomHastings: Thanks for catching that. I thought that golf was a bit iffy. Should be okay now. \$\endgroup\$ Commented Mar 23, 2018 at 18:17
  • \$\begingroup\$ Try it online! - 19 bytes - 1 (Rectangle), 2 (Triangle), 5 (Square) and 0 (Mess) [Using binary numbers]. Possibly not acceptable lol. gs€g©QP®¥ ÄP®1å&®ËJC can add a space char and a C for 21 though. \$\endgroup\$ Commented Mar 26, 2018 at 14:56
  • \$\begingroup\$ @MagicOctopusUrn: It still needs to check for length/height>=2, but it should still save bytes. Clever trick building the output numbers from binary! \$\endgroup\$ Commented Mar 26, 2018 at 15:33
  • 1
    \$\begingroup\$ @MagicOctopusUrn: I used your delta and binary tricks to save some bytes on my original version. Could probably save a few more rewriting it a bit more. \$\endgroup\$ Commented Mar 26, 2018 at 15:49
2
\$\begingroup\$

R, 101 bytes

"if"(var(z<-nchar(y<-scan(,"",,,"
","")))==0,"if"(length(y)==z,1,2
),"if"(all(abs(diff(z))==1),3,4))

1=Square
2=Rectangle
3=Triangle
4=Random

Code cannot deal with 'NEGATIVE ACKNOWLEDGE' (U+0015) or the square in the code above. This byte can be switched to something different if the input requires contains this byte.

Try it online!

answered Mar 24, 2018 at 3:26
\$\endgroup\$
3
  • \$\begingroup\$ maybe you could use readLines() instead of scan()? \$\endgroup\$ Commented Mar 25, 2018 at 16:54
  • \$\begingroup\$ @Giuseppe Can't/too noob to get readLines to work \$\endgroup\$ Commented Mar 26, 2018 at 5:50
  • \$\begingroup\$ Hmm, looks like you have to specify file("stdin") to get it to read from console (rather than the next lines of code). That means it'll probably be less golfy. ah well. \$\endgroup\$ Commented Mar 26, 2018 at 10:48
2
\$\begingroup\$

Snails, 29 bytes

ada7A
.2,lr
?!(t.
rw~)z
.+~o~

Output key:

  • 0 - Mess
  • 3 - Triangle
  • 6 - Rectangle
  • 7 - Square

It would be 23 bytes without source layout:

zA
.2,dun!(t.rf~)z.+~o~
answered Mar 27, 2018 at 9:47
\$\endgroup\$
1
  • \$\begingroup\$ I've always been keen to play with this language since reading the question that spawned it! \$\endgroup\$ Commented Mar 27, 2018 at 12:04
1
\$\begingroup\$

Wolfram Language (Mathematica), 119 bytes

(x=StringLength/@#~StringSplit~"\n")/.{{1}->3,s~(t=Table)~{
s=Tr[1^x]}:>0,x[[1]]~t~s:>1,(r=Range@s)|Reverse@r:>2,_->3}&

Using Replace /. and pattern matching on the character count by line. Replace will kick out the first RHS of a rule that is matched, so the ordering is to test for the 1 character input, then squares, rectangles, triangles, and a fall-through for messes.

square=0,rectangle=1,triangle=2,mess=3

Try it online!

answered Mar 23, 2018 at 16:30
\$\endgroup\$
1
  • \$\begingroup\$ @DomHastings, it's fixed. \$\endgroup\$ Commented Mar 23, 2018 at 16:49
1
\$\begingroup\$

Red, 209 bytes

func[s][c: copy[]foreach a split s"^/"[append c length? a]d: unique c
r: 0 if 1 < l: length? c[if 1 = length? d[r: 2 if(do d)= l[r: 1]]n: 0
v: copy[]loop l[append v n: n + 1]if(v = c)or(v = reverse c)[r: 3]]r]

Try it online!

0 Mess

1 Square

2 Rectangle

3 Triangle

answered Mar 23, 2018 at 17:08
\$\endgroup\$
1
\$\begingroup\$

AWK, 119 bytes

{p=l;l=L[NR]=length(0ドル)
D=d}{d=p-l;x=x?x:NR>2?\
d!=D:0}END{print x==1?\
3:d*d==1?(L[NR]+L[1]==\
NR+1)?2:3:p!=NR}#######

Try it online!

Output:

0 = Square
1 = Rectangle
2 = Triangle
3 = Mess

answered Mar 23, 2018 at 18:31
\$\endgroup\$
1
\$\begingroup\$

Ruby, (削除) 115 (削除ここまで) 111 bytes

->s{m=s.split(?\n).map &:size;r=*1..s=m.size;s<2?4:(m|[
]).size<2?m[0]<2?4:s==m[0]?1:2:r==m.reverse||r==m ?3:4}

Try it online!

Anonymous lambda. Outputs:

  1. Square
  2. Rectangle
  3. Triangle
  4. Mess
answered Mar 23, 2018 at 16:56
\$\endgroup\$
2
  • \$\begingroup\$ This looks to fail on some that should be flagged as mess: Try it online! \$\endgroup\$ Commented Mar 23, 2018 at 17:33
  • \$\begingroup\$ Ouch, I guess this will have to go as a quick fix. Probably will need to try golfing it a bit more... \$\endgroup\$ Commented Mar 23, 2018 at 18:00
1
\$\begingroup\$

C (gcc), (削除) 125 (削除ここまで) 123 bytes

Thanks to ceilingcat for -2 bytes.

f(L,n)int**L;{int i,l,c,F=strlen(*L),s=-F;for(l=i=0;i<n;l=c)c
=strlen(L[i++]),s+=c-l;s=n>1?s||F<2?~abs(s)+n?0:3:n^F?2:1:0;}

Try it online!

answered Nov 24, 2018 at 14:06
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.