# Polygons challenge

**Problem statement**

Identify whether four sides (given by four integers) can form a square, a rectangle, or neither.

**Input**

You will receive an list of strings, each containing four space-separated integers, which represent the length of the sides of a polygon. The input lines will follow the ‘A B C D’ order as in the following representation:

1 2 3 4 5 6 7 |
|-----A-----| | | | | D B | | | | |-----C-----| |

**Output**

A single line which contains 3 space-separated integers; representing the number of squares, number of rectangles, and number of other polygons with 4 sides. Note that squares shouldn’t be counted as rectangles. Invalid polygons should also be counted as ‘other polygons’.

**Constraints**

The four integers representing the sides will be such that: (Where represents the integer)

**Sample Input**

1 2 3 4 5 6 |
36 30 36 30 15 15 15 15 46 96 90 100 86 86 86 86 100 200 100 200 -100 200 -100 200 |

**Sample Output**

1 |
2 2 2 |

**Solution**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public class Polygons { enum Shape { Square, Rectangle, Other } private static Shape shape(int a, int b, int c, int d) { if (a<=0 || b<=0 || c<=0 || d <= 0) { return Shape.Other; } if (a==b && b==c && c==d) { return Shape.Square; } if (a==c && b==d) { return Shape.Rectangle; } return Shape.Other; } public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream(System.getProperty("user.home") + "/" + "in.txt")); Scanner in = new Scanner(System.in); Map<Shape, Integer> enumMap = new EnumMap<Shape, Integer>(Shape.class); enumMap.put(Shape.Other,0); enumMap.put(Shape.Square,0); enumMap.put(Shape.Rectangle,0); while (in.hasNext()) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int d = in.nextInt(); Shape shp = shape(a,b,c,d); enumMap.put(shp, enumMap.get(shp)+1); } System.out.println(enumMap.get(Shape.Square)+" "+enumMap.get(Shape.Rectangle)+" "+ enumMap.get(Shape.Other)); } } |