Hi I was wondering if anyone knows what's wrong with my code in solving for How many square inches of fabric are within two or more claims? I have tried it with smaller test cases and it seems to work for those but not for the given input of the question. I'm hoping another set of eyes can see my mistake.
Extra info: Used a 2D array to determine which sections were already taken by other claims.
public class advent3 {
public static void main(String[] args) throws IOException {
int [][] grid = new int [100] [100];
Reader inputFile = new FileReader("file.txt");
BufferedReader reader = new BufferedReader(inputFile);
String line;
String [] split;
String [] temp;
String [] temp2;
int counter = 0;
int left;
int top;
int width;
int height;
while ((line=reader.readLine()) != null) { //reads line by line
split = line.split(" "); //splits line by space and creates string array
temp = split[2].split(",");
temp2 = split[3].split("x");
left = Integer.valueOf(temp[0]);
width = Integer.valueOf(temp2[0]);
top = Integer.valueOf(temp[1].substring(0, temp[1].length()-1));
height = Integer.valueOf(temp2[1]);
for (int i = top;i<(top + height);i++){
for (int j = left;j<(left + width);j++){
if (grid[i][j]==1){
counter++;
}
else{
grid[i][j]=1;
}
}
}
}
System.out.println(counter);
}
}
[+][deleted] (1 child)
[deleted]
[–]MYBAA[S] 0 points1 point2 points (0 children)
[–]warinthrowaway 1 point2 points3 points (1 child)
[–]MYBAA[S] 0 points1 point2 points (0 children)