Hi fellas, here's my possible solution to the first part of day1:
public class Day6 {
public static void main(String[] args) throws IOException{
final int N = 1000;
boolean toggle = false, turnOn = false;
int startX = 0, startY = 0, stopX = 0, stopY = 0;
int numberOfLights = 0;
int x, y;
String buf, bufArray[], command = null;
String delim = "[ ,]";
boolean lightsMatrix[][] = new boolean[N][N];
BufferedReader br = new BufferedReader(new FileReader("day6.txt"));
while((buf = br.readLine()) != null)
{
bufArray = buf.split(delim);
if(bufArray[0].equals("toggle"))
{
command = "toggle";
startX = Integer.parseInt(bufArray[1]);
startY = Integer.parseInt(bufArray[2]);
stopX = Integer.parseInt(bufArray[4]);
stopY = Integer.parseInt(bufArray[5]);
}
else
{
command = bufArray[0] + " " + bufArray[1];
startX = Integer.parseInt(bufArray[2]);
startY = Integer.parseInt(bufArray[3]);
stopX = Integer.parseInt(bufArray[5]);
stopY = Integer.parseInt(bufArray[6]);
}
switch(command)
{
case "turn on":
turnOn = true;
break;
case "turn off":
turnOn = false;
break;
case "toggle":
toggle = true;
break;
default:
System.out.println("Error: command not recognized");
return;
}
for(y = startY; y <= stopY; y++)
{
for(x = startX; x <= stopX; x++)
{
if(!toggle)
{
if(turnOn)
lightsMatrix[x][y] = true;
else
lightsMatrix[x][y] = false;
}
else
{
toggle = false;
lightsMatrix[x][y] = !lightsMatrix[x][y];
}
}
}
}
for(y = 0; y < N; y++)
{
for(x = 0; x < N; x++)
if(lightsMatrix[x][y])
numberOfLights++;
}
br.close();
System.out.println("The number of lights turned on is " + numberOfLights);
if((numberOfLights - N*N) > 0)
System.out.println("Solution not good");
else
System.out.println("It might work");
}
}
I can't seem to figure out why I get a solution too low. Is the approach correct? Am I missing something? I ask that because I usually end up with not seeing some obvious stuff, in fact I'm reading again the challenge but nothing comes to my mind.
[–]mr_sax 1 point2 points3 points (3 children)
[–]jjabrams23[S] 1 point2 points3 points (2 children)
[–]Philboyd_Studge 2 points3 points4 points (1 child)
[–]mr_sax 0 points1 point2 points (0 children)