55import java .util .List ;
66
77public class HouseDecoratingContest {
8+ private static final int FROM_X = 0 ;
9+ private static final int FROM_Y = 1 ;
10+ private static final int TO_X = 2 ;
11+ private static final int TO_Y = 3 ;
12+
813 private final List <String > input ;
14+ private final boolean [][] houseDecoration = new boolean [1000 ][1000 ];
915
1016 public HouseDecoratingContest (String fileName ) {
1117 input = readData (fileName );
@@ -22,13 +28,99 @@ public static int solvePart2(String fileName) {
2228 }
2329
2430 public int solvePart1 () {
25- return 0 ;
31+ for (String instruction : input ) {
32+ if (instruction .startsWith ("turn on" )) {
33+ int [] values = parseInstruction (instruction ,8 );
34+ turnOnLight (values [FROM_X ], values [TO_X ], values [FROM_Y ], values [TO_Y ]);
35+
36+ } else if (instruction .startsWith ("turn off" )) {
37+ int [] values = parseInstruction (instruction ,9 );
38+ turnOffLight (values [FROM_X ], values [TO_X ], values [FROM_Y ], values [TO_Y ]);
39+
40+ } else if (instruction .startsWith ("toggle" )) {
41+ int [] values = parseInstruction (instruction ,7 );
42+ toggleLight (values [FROM_X ], values [TO_X ], values [FROM_Y ], values [TO_Y ]);
43+ } else {
44+ throw new IllegalArgumentException ("no valid instruction: " + instruction );
45+ }
46+ }
47+
48+ return countNumberOfLitLights ();
2649 }
2750
2851 public int solvePart2 () {
2952 return 0 ;
3053 }
3154
55+ private int countNumberOfLitLights () {
56+ int numberOfLightsLit = 0 ;
57+
58+ for (int x = 0 ; x < 1000 ; x ++) {
59+ for (int y = 0 ; y < 1000 ; y ++) {
60+ if (houseDecoration [x ][y ]) {
61+ numberOfLightsLit ++;
62+ }
63+ }
64+ }
65+
66+ return numberOfLightsLit ;
67+ }
68+
69+ private int [] parseInstruction (String instruction , int offset ) {
70+ int [] values = new int [4 ];
71+ String data = instruction .substring (offset );
72+ String [] words = data .split (" " );
73+ String [] fromWords = words [0 ].split ("," );
74+ String [] toWords = words [2 ].split ("," );
75+
76+ values [FROM_X ] = Integer .parseInt (fromWords [0 ]);
77+ values [FROM_Y ] = Integer .parseInt (fromWords [1 ]);
78+ values [TO_X ] = Integer .parseInt (toWords [0 ]);
79+ values [TO_Y ] = Integer .parseInt (toWords [1 ]);
80+
81+ return values ;
82+ }
83+
84+ private void turnOnLight (int x , int y ) {
85+ setLight (x , y , true );
86+ }
87+
88+ 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 );
94+ }
95+
96+ private void turnOffLight (int fromX , int toX , int fromY , int toY ) {
97+ setLight (fromX , toX , fromY , toY , false );
98+ }
99+
100+ private void toggleLight (int x , int y ) {
101+ setLight (x , y , !houseDecoration [x ][y ]);
102+ }
103+
104+ private void toggleLight (int fromX , int toX , int fromY , int toY ) {
105+ for (int x = fromX ; x <= toX ; x ++) {
106+ for (int y = fromY ; y <= toY ; y ++) {
107+ toggleLight (x , y );
108+ }
109+ }
110+ }
111+
112+ private void setLight (int x , int y , boolean value ) {
113+ houseDecoration [x ][y ] = value ;
114+ }
115+
116+ private void setLight (int fromX , int toX , int fromY , int toY , boolean value ) {
117+ for (int x = fromX ; x <= toX ; x ++) {
118+ for (int y = fromY ; y <= toY ; y ++) {
119+ setLight (x , y , value );
120+ }
121+ }
122+ }
123+
32124 private List <String > readData (String fileName ) {
33125 return DataReader .readData (fileName , MainClass .class );
34126 }
0 commit comments