Last active
August 29, 2015 14:27
-
-
Save peanutwolf/0fe13e587fa9e6417f3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.SortedSet; | |
import java.util.TreeSet; | |
/** | |
* Created by vigursky on 17.08.2015. | |
*/ | |
public class testApp { | |
private final static int upperBound = 10000; | |
private final static int lowerBound = 1; | |
private final static int maxRects = 100; | |
private SortedSet<Integer> X = new TreeSet<>(); | |
private SortedSet<Integer> Y = new TreeSet<>(); | |
public int calcRectsSquare(String inpFile, String outFile) throws TestAppException { | |
List<MyRectangle> rects; | |
Integer S = 0; | |
Integer [] x; | |
Integer [] y; | |
rects = readInpFile(inpFile); | |
x = X.toArray(new Integer[X.size()]); | |
y = Y.toArray(new Integer[Y.size()]); | |
for (int i=0; i<x.length-1; i++) { | |
for (int j = 0; j < y.length-1; j++) { | |
MyRectangle rectNode = new MyRectangle(x[i].intValue(), y[j].intValue(), x[i + 1].intValue(), y[j + 1].intValue()); | |
for (int k = 0; k < rects.size(); k++) { | |
if(rects.get(k).isBelong(rectNode)){ | |
S += rectNode.getSquare(); | |
break; | |
} | |
} | |
} | |
} | |
writeOutFile(outFile, S); | |
System.out.println(S); | |
return 0; | |
} | |
private List<MyRectangle> readInpFile(String inpFile) throws TestAppException { | |
List<MyRectangle> rectList = new ArrayList<>(); | |
int [] rectNode = new int[4]; | |
String [] values; | |
int lineCounter = 0; | |
if(inpFile == null) | |
throw new TestAppException("Error: input file is not set"); | |
try(BufferedReader reader = new BufferedReader(new FileReader(inpFile))) { | |
for(String line; (line = reader.readLine()) != null;) { | |
values = line.trim().split("[ ]+"); | |
if(values.length != 4) | |
throw new TestAppException("Error: wrong input on line=" + lineCounter); | |
for(int n = 0; n < values.length; n++){ | |
rectNode[n] = Integer.parseInt(values[n]); | |
if(rectNode[n] < lowerBound || rectNode[n] > upperBound) | |
throw new TestAppException("Error: wrong input on line=" + lineCounter); | |
} | |
X.add(rectNode[0]); | |
X.add(rectNode[2]); | |
Y.add(rectNode[1]); | |
Y.add(rectNode[3]); | |
rectList.add(new MyRectangle(rectNode[0], rectNode[1], rectNode[2], rectNode[3])); | |
if(++lineCounter > maxRects){ | |
throw new TestAppException("Error: too many inputs, sorry"); | |
} | |
} | |
}catch(IOException|TestAppException|NumberFormatException e){ | |
if(TestAppException.class.isInstance(e)) | |
throw new TestAppException(e.getMessage()); | |
else | |
throw new TestAppException("Error: wrong input on line=" + lineCounter); | |
} | |
return rectList; | |
} | |
private void writeOutFile(String outFile, int data) throws TestAppException { | |
try (Writer writer = new BufferedWriter(new FileWriter(outFile))) { | |
writer.write(data+""); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
throw new TestAppException("Error: cannot write output file"); | |
} | |
} | |
public static void main(String [] args){ | |
if(args.length != 2){ | |
System.err.println("Error: usage (java testApp input.txt output.txt)"); | |
System.exit(0); | |
} | |
testApp test = new testApp(); | |
try { | |
test.calcRectsSquare(args[0], args[1]); | |
} catch (TestAppException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class MyRectangle{ | |
private int x1 = 0; | |
private int x2 = 0; | |
private int y1 = 0; | |
private int y2 = 0; | |
private int width = 0; | |
private int height = 0; | |
public MyRectangle(int x1, int y1, int x2, int y2) { | |
// normalize points | |
this.x1 = x1 < x2 ? x1 : x2; | |
this.x2 = x2 > x1 ? x2 : x1; | |
this.y1 = y1 < y2 ? y1 : y2; | |
this.y2 = y2 > y1 ? y2 : y1; | |
width = x2 - x1; | |
height = y2 - y1; | |
} | |
public int getSquare(){ | |
return width * height; | |
} | |
public boolean isBelong(MyRectangle rectNode){ | |
return x1 <= rectNode.x1 && rectNode.x1 <= x2 && | |
x1 <= rectNode.x2 && rectNode.x2 <= x2 && | |
y1 <= rectNode.y1 && rectNode.y1 <= y2 && | |
y1 <= rectNode.y2 && rectNode.y2 <= y2; | |
} | |
} | |
class TestAppException extends Exception { | |
TestAppException(){ | |
super(); | |
} | |
TestAppException(String msg){ | |
super(msg); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.SortedSet; | |
import java.util.TreeSet; | |
/** | |
* Created by vigursky on 17.08.2015. | |
*/ | |
public class testApp { | |
private final static int upperBound = 10000; | |
private final static int lowerBound = 1; | |
private final static int maxRects = 100; | |
public void calcRectsSquare(String inpFile, String outFile) throws TestAppException { | |
List<MyRectangle> rects; | |
Integer S = 0; | |
Integer [] x; | |
Integer [] y; | |
rects = readInpFile(inpFile); | |
x = getXArray(rects); | |
y = getYArray(rects); | |
for (int i=0; i<x.length-1; i++) { | |
for (int j = 0; j < y.length-1; j++) { | |
MyRectangle rectNode = new MyRectangle(x[i].intValue(), y[j].intValue(), x[i + 1].intValue(), y[j + 1].intValue()); | |
for (int k = 0; k < rects.size(); k++) { | |
if(rects.get(k).isBelong(rectNode)){ | |
S += rectNode.getSquare(); | |
break; | |
} | |
} | |
} | |
} | |
writeOutFile(outFile, S); | |
System.out.println(S); | |
} | |
private List<MyRectangle> readInpFile(String inpFile) throws TestAppException { | |
List<MyRectangle> rectList = new ArrayList<>(); | |
int [] rectNode = new int[4]; | |
int lineCounter = 0; | |
String [] values; | |
if(inpFile == null) | |
throw new TestAppException("Error: input file is not set"); | |
try(BufferedReader reader = new BufferedReader(new FileReader(inpFile))) { | |
for(String line; (line = reader.readLine()) != null;) { | |
values = line.trim().split("[ ]+"); | |
if(values.length != 4) | |
throw new TestAppException("Error: wrong input on line=" + lineCounter); | |
for(int n = 0; n < values.length; n++){ | |
rectNode[n] = Integer.parseInt(values[n]); | |
if(rectNode[n] < lowerBound || rectNode[n] > upperBound) | |
throw new TestAppException("Error: wrong input on line=" + lineCounter); | |
} | |
rectList.add(new MyRectangle(rectNode[0], rectNode[1], rectNode[2], rectNode[3])); | |
if(++lineCounter > maxRects){ | |
throw new TestAppException("Error: too many inputs, sorry"); | |
} | |
} | |
}catch(IOException|TestAppException|NumberFormatException e){ | |
if(TestAppException.class.isInstance(e)) | |
throw new TestAppException(e.getMessage()); | |
else | |
throw new TestAppException("Error: wrong input on line=" + lineCounter); | |
} | |
return rectList; | |
} | |
private void writeOutFile(String outFile, int data) throws TestAppException { | |
try (Writer writer = new BufferedWriter(new FileWriter(outFile))) { | |
writer.write(data+""); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
throw new TestAppException("Error: cannot write output file"); | |
} | |
} | |
private Integer[] getXArray(List<MyRectangle> rects){ | |
SortedSet<Integer> X = new TreeSet<>(); | |
if(rects == null) | |
return null; | |
for(MyRectangle rect : rects){ | |
X.add(rect.getX1()); | |
X.add(rect.getX2()); | |
} | |
return X.toArray(new Integer[X.size()]); | |
} | |
private Integer[] getYArray(List<MyRectangle> rects){ | |
SortedSet<Integer> Y = new TreeSet<>(); | |
if(rects == null) | |
return null; | |
for(MyRectangle rect : rects){ | |
Y.add(rect.getY1()); | |
Y.add(rect.getY2()); | |
} | |
return Y.toArray(new Integer[Y.size()]); | |
} | |
public static void main(String [] args){ | |
if(args.length != 2){ | |
System.err.println("Error: usage (java testApp input.txt output.txt)"); | |
System.exit(0); | |
} | |
testApp test = new testApp(); | |
try { | |
test.calcRectsSquare(args[0], args[1]); | |
} catch (TestAppException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class MyRectangle{ | |
private int x1 = 0; | |
private int x2 = 0; | |
private int y1 = 0; | |
private int y2 = 0; | |
private int width = 0; | |
private int height = 0; | |
public MyRectangle(int x1, int y1, int x2, int y2) { | |
// normalize points | |
this.x1 = x1 < x2 ? x1 : x2; | |
this.x2 = x2 > x1 ? x2 : x1; | |
this.y1 = y1 < y2 ? y1 : y2; | |
this.y2 = y2 > y1 ? y2 : y1; | |
width = x2 - x1; | |
height = y2 - y1; | |
} | |
public int getSquare(){ | |
return width * height; | |
} | |
public boolean isBelong(MyRectangle rectNode){ | |
return x1 <= rectNode.x1 && rectNode.x1 <= x2 && | |
x1 <= rectNode.x2 && rectNode.x2 <= x2 && | |
y1 <= rectNode.y1 && rectNode.y1 <= y2 && | |
y1 <= rectNode.y2 && rectNode.y2 <= y2; | |
} | |
public int getX1(){ | |
return x1; | |
} | |
public int getX2(){ | |
return x2; | |
} | |
public int getY1(){ | |
return y1; | |
} | |
public int getY2(){ | |
return y2; | |
} | |
} | |
class TestAppException extends Exception { | |
TestAppException(String msg){ | |
super(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment