1
+ /*
2
+ Sanjit Bhat
3
+ ACSL Lights Out
4
+ Contest #3 2016-17
5
+ Acton-Boxborough Regional High School
6
+ Senior Division
7
+ */
8
+
9
+ import java .io .*;
10
+ import java .math .BigInteger ;
11
+ import java .util .*;
12
+
13
+ public class lights_sanjit_bhat {
14
+ static String your_name = "Sanjit Bhat" ;
15
+ static String prob_name = "lights" ;
16
+
17
+ public static void main (String [] args ) throws IOException {
18
+ Scanner sc = new Scanner (new File (prob_name + ".in" ));
19
+ PrintWriter out = new PrintWriter (new FileWriter (prob_name + ".out" ), true );
20
+ out .println (your_name );
21
+
22
+ // read in all the boards, condense each into one string, and convert it to binary
23
+ String [] boards_bin = new String [6 ];
24
+ for (int i = 0 ; i < 6 ; i ++) {
25
+ StringBuilder board_hex = new StringBuilder ();
26
+ for (int j = 0 ; j < 4 ; j ++) {
27
+ board_hex .append (sc .next ());
28
+ }
29
+
30
+ // convert board to binary
31
+ String postfix = hexToBin (board_hex .toString ());
32
+
33
+ // pad each binary string to make it fill entire board
34
+ StringBuilder zero_prefix = new StringBuilder ();
35
+ for (int j = 0 ; j < 64 - postfix .length (); j ++) {
36
+ zero_prefix .append ("0" );
37
+ }
38
+ boards_bin [i ] = zero_prefix .toString () + postfix ;
39
+ }
40
+
41
+ for (int test_case = 0 ; test_case < 5 ; test_case ++) {
42
+ try {
43
+ String prev_board = boards_bin [test_case ];
44
+ String future_board = boards_bin [test_case +1 ];
45
+
46
+ // calculate array of differences between strings
47
+ // this shows whether it was in change "radius"
48
+ boolean [] diffs = new boolean [64 ];
49
+ for (int char_pos = 0 ; char_pos < 64 ; char_pos ++) {
50
+ if (prev_board .charAt (char_pos ) != future_board .charAt (char_pos )) {
51
+ diffs [char_pos ] = true ;
52
+ }
53
+ }
54
+
55
+ int press_loc = -1 ;
56
+ // find a diff that is the only one in its row - corresponds to top or bottom of star
57
+ // there might only be one (x in bottom-most or top-most part of screen) or two (change in middle of screen)
58
+ for (int i = 0 ; i < 8 ; i ++) {
59
+ int num_diffs = 0 ;
60
+ int diff_loc = -1 ;
61
+ for (int j = i *8 ; j < (i +1 )*8 ; j ++) {
62
+ if (diffs [j ]) {
63
+ num_diffs ++;
64
+ diff_loc = j ;
65
+ }
66
+ }
67
+
68
+ // look at locations 2 rows above and 2 rows below for center of press
69
+ if (num_diffs == 1 ) {
70
+ if (diff_loc -16 >= 0 && diffs [diff_loc -16 ]) press_loc = diff_loc -16 ;
71
+ if (diff_loc +16 < 64 && diffs [diff_loc +16 ]) press_loc = diff_loc +16 ;
72
+ }
73
+ }
74
+
75
+ // convert from index notation to row, col notation
76
+ int row = (press_loc / 8 ) + 1 ;
77
+ int col = (press_loc % 8 ) + 1 ;
78
+ out .println (row + "" + col );
79
+ } catch (Exception e ) {
80
+ out .println ("Something's wrong with this test case" );
81
+ e .printStackTrace ();
82
+ }
83
+ }
84
+ sc .close ();
85
+ out .close ();
86
+ }
87
+
88
+ static String hexToBin (String s ) {
89
+ return new BigInteger (s , 16 ).toString (2 );
90
+ }
91
+ }
0 commit comments