@@ -12,6 +12,7 @@ public class HouseDecoratingContest {
1212
1313 private final List <String > input ;
1414 private final boolean [][] houseDecoration = new boolean [1000 ][1000 ];
15+ private final int [][] brightness = new int [1000 ][1000 ];
1516
1617 public HouseDecoratingContest (String fileName ) {
1718 input = readData (fileName );
@@ -49,7 +50,8 @@ public int solvePart1() {
4950 }
5051
5152 public int solvePart2 () {
52- return 0 ;
53+ solvePart1 ();
54+ return countBrightness ();
5355 }
5456
5557 private int countNumberOfLitLights () {
@@ -66,6 +68,18 @@ private int countNumberOfLitLights() {
6668 return numberOfLightsLit ;
6769 }
6870
71+ private int countBrightness () {
72+ int currentBrightness = 0 ;
73+
74+ for (int x = 0 ; x < 1000 ; x ++) {
75+ for (int y = 0 ; y < 1000 ; y ++) {
76+ currentBrightness += brightness [x ][y ];
77+ }
78+ }
79+
80+ return currentBrightness ;
81+ }
82+
6983 private int [] parseInstruction (String instruction , int offset ) {
7084 int [] values = new int [4 ];
7185 String data = instruction .substring (offset );
@@ -81,24 +95,16 @@ private int[] parseInstruction(String instruction, int offset) {
8195 return values ;
8296 }
8397
84- private void turnOnLight (int x , int y ) {
85- setLight (x , y , true );
86- }
87-
8898 private void turnOnLight (int fromX , int toX , int fromY , int toY ) {
89- setLight (fromX , toX , fromY , toY , true );
90- }
91-
92- private void turnOffLight (int x , int y ) {
93- setLight (x , y , false );
99+ setLight (fromX , toX , fromY , toY , true , 1 );
94100 }
95101
96102 private void turnOffLight (int fromX , int toX , int fromY , int toY ) {
97- setLight (fromX , toX , fromY , toY , false );
103+ setLight (fromX , toX , fromY , toY , false , - 1 );
98104 }
99105
100106 private void toggleLight (int x , int y ) {
101- setLight (x , y , !houseDecoration [x ][y ]);
107+ setLight (x , y , !houseDecoration [x ][y ], 2 );
102108 }
103109
104110 private void toggleLight (int fromX , int toX , int fromY , int toY ) {
@@ -109,14 +115,19 @@ private void toggleLight(int fromX, int toX, int fromY, int toY) {
109115 }
110116 }
111117
112- private void setLight (int x , int y , boolean value ) {
118+ private void setLight (int x , int y , boolean value , int brightnessChange ) {
113119 houseDecoration [x ][y ] = value ;
120+ brightness [x ][y ] += brightnessChange ;
121+
122+ if (brightness [x ][y ] < 0 ) {
123+ brightness [x ][y ] = 0 ;
124+ }
114125 }
115126
116- private void setLight (int fromX , int toX , int fromY , int toY , boolean value ) {
127+ private void setLight (int fromX , int toX , int fromY , int toY , boolean value , int brightnessChange ) {
117128 for (int x = fromX ; x <= toX ; x ++) {
118129 for (int y = fromY ; y <= toY ; y ++) {
119- setLight (x , y , value );
130+ setLight (x , y , value , brightnessChange );
120131 }
121132 }
122133 }
0 commit comments