1
+ /*
2
+ Sanjit Bhat
3
+ ACSL agram
4
+ Contest #1 2016-17
5
+ Acton Boxborough Regional High School
6
+ Senior Division
7
+ */
8
+
9
+ import java .io .*;
10
+ import java .util .*;
11
+
12
+ public class agram_sanjit_bhat {
13
+ static String your_name = "Sanjit Bhat" ;
14
+ static String prob_name = "agram" ; // this needs to be standardized across everyone's code
15
+ // file names should be "{prob_name}_{first name}_{last name}.java". This is for file-keeping sake as we can't have two files with the same name.
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 ); // no buffering in case program craps out. Prev test cases will be outputted
20
+ out .println (your_name ); // MUST include this
21
+
22
+ for (int l = 0 ; l < 5 ; l ++) {
23
+ try { // use try catch in case code craps out on one test case
24
+ String input = sc .nextLine ();
25
+ String [] cards = input .split (", " );
26
+
27
+ char [] suits = {'S' , 'H' , 'D' , 'C' };
28
+ HashMap <Character , Integer > suit_vals = new HashMap <>();
29
+ for (int i = 0 ; i < suits .length ; i ++) {
30
+ suit_vals .put (suits [i ], i );
31
+ }
32
+
33
+ char [] ranks = {'A' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'T' , 'J' , 'Q' , 'K' };
34
+ HashMap <Character , Integer > rank_vals = new HashMap <>();
35
+ for (int i = 0 ; i < ranks .length ; i ++) {
36
+ rank_vals .put (ranks [i ], i );
37
+ }
38
+
39
+ char opp_rank = cards [0 ].charAt (0 );
40
+ char opp_suit = cards [0 ].charAt (1 );
41
+ int opp_rank_val = rank_vals .get (opp_rank );
42
+
43
+ char higher_in_suit = 'N' ; // lowest card in same suit that's higher than opponent card
44
+ char lowest_in_suit = 'N' ; // lowest card in same suit
45
+ char lowest_overall_rank = 'N' ; // rank of lowest card overall
46
+ char lowest_overall_suit = 'N' ; // suit of lowest card overall
47
+
48
+ for (int i = 1 ; i < cards .length ; i ++) {
49
+ char curr_rank = cards [i ].charAt (0 );
50
+ char curr_suit = cards [i ].charAt (1 );
51
+ int curr_rank_val = rank_vals .get (curr_rank );
52
+ if (curr_suit == opp_suit && curr_rank_val > opp_rank_val &&
53
+ (higher_in_suit == 'N' || curr_rank_val < rank_vals .get (higher_in_suit ))) {
54
+ higher_in_suit = curr_rank ;
55
+ }
56
+ if (curr_suit == opp_suit && (lowest_in_suit == 'N' || curr_rank_val < rank_vals .get (lowest_in_suit ))) {
57
+ lowest_in_suit = curr_rank ;
58
+ }
59
+ if (lowest_overall_rank == 'N' || curr_rank_val < rank_vals .get (lowest_overall_rank ) ||
60
+ (curr_rank_val == rank_vals .get (lowest_overall_rank ) &&
61
+ suit_vals .get (curr_suit ) > suit_vals .get (lowest_overall_suit ))) {
62
+ lowest_overall_rank = curr_rank ;
63
+ lowest_overall_suit = curr_suit ;
64
+ }
65
+ }
66
+
67
+ if (higher_in_suit != 'N' ) {
68
+ out .println ("" + higher_in_suit + opp_suit ); // remember to print to .out file for all test cases
69
+ } else if (lowest_in_suit != 'N' ) {
70
+ out .println ("" + lowest_in_suit + opp_suit );
71
+ } else {
72
+ out .println ("" + lowest_overall_rank + lowest_overall_suit );
73
+ }
74
+ } catch (Exception e ) {
75
+ out .println ("Something went wrong with this test case" ); // even if code craps out on one test case, your code will move on and grader will be fine
76
+ e .printStackTrace ();
77
+ }
78
+ }
79
+ out .close ();
80
+ }
81
+ }
0 commit comments