|
| 1 | +import java.io.File; |
| 2 | +import java.io.FileNotFoundException; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | + |
| 7 | +public class TimeDiff { |
| 8 | + static String dayslist[] = new String[]{"Mon","Tues","Wed","Thurs","Fri","Sat","Sun"}; |
| 9 | + |
| 10 | + public static void main(String[] args) throws FileNotFoundException { |
| 11 | + parseData(args[0]); |
| 12 | + } |
| 13 | + |
| 14 | + public static void parseData(String filename) throws FileNotFoundException { |
| 15 | + Scanner sc = new Scanner(new File(filename)); |
| 16 | + ArrayList<String> data = new ArrayList<String>(); |
| 17 | + while(sc.hasNext()) |
| 18 | + data.add(sc.nextLine()); |
| 19 | + sc.close(); |
| 20 | + |
| 21 | + boolean diffArr[][][] = new boolean[4][7][24]; |
| 22 | + int i = 0; |
| 23 | + for(String times:data) { |
| 24 | + //System.out.println(times); |
| 25 | + String[] days = times.split(", "); |
| 26 | + //System.out.println(i); |
| 27 | + for(String day_str:days) { |
| 28 | + String[] day_info = day_str.split("[.] "); |
| 29 | + int day = getDayIndex(day_info[0]); |
| 30 | + //System.out.println(day_info[0]+"--"+day); |
| 31 | + String range[] = day_info[1].split(" - "); |
| 32 | + int hour1 = getHourIndexes(day_info[1])[0], hour2 = getHourIndexes(day_info[1])[1]; |
| 33 | + //System.out.println(range[0]+"-"+range[1]+" == "+hour1+"-"+hour2); |
| 34 | + for(int j = hour1; j <= hour2; j++) |
| 35 | + diffArr[i][day][j-1] = true; |
| 36 | + } |
| 37 | + //System.out.println("\n"); |
| 38 | + i++; |
| 39 | + } |
| 40 | + |
| 41 | + boolean andedArr[][] = new boolean[7][24]; |
| 42 | + for(int r = 0; r < andedArr.length; r++) |
| 43 | + for(int c = 0; c < andedArr[r].length; c++) |
| 44 | + andedArr[r][c] = true; |
| 45 | + |
| 46 | + for(i = 0; i < diffArr.length; i++) { |
| 47 | + //printTimes(diffArr[i]); |
| 48 | + //System.out.println("\n"); |
| 49 | + for(int r = 0; r < diffArr[i].length; r++) |
| 50 | + for(int c = 0; c < diffArr[i][r].length; c++) |
| 51 | + andedArr[r][c] &= diffArr[i][r][c]; |
| 52 | + } |
| 53 | + |
| 54 | + System.out.println("Intersection of hours available:\n(first column is 1 AM, last is midnight)"); |
| 55 | + printTimes(andedArr); |
| 56 | + } |
| 57 | + |
| 58 | + public static void printTimes(boolean[][] matrix) { |
| 59 | + System.out.print("\t"); |
| 60 | + for(int i = 0; i < matrix[0].length; i++) |
| 61 | + System.out.print((i+1)%10); |
| 62 | + System.out.println(); |
| 63 | + for(int r = 0; r < matrix.length; r++) { |
| 64 | + System.out.print(dayslist[r]+":\t"); |
| 65 | + for(int c = 0; c < matrix[0].length; c++) { |
| 66 | + int num = (matrix[r][c]) ? 1 : 0; |
| 67 | + System.out.print(num); |
| 68 | + } |
| 69 | + System.out.println(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static int[] getHourIndexes(String range) { |
| 74 | + String[] tokens = range.split(" "); |
| 75 | + String hours[] = range.split(" - "); |
| 76 | + int index1 = getHourIndex(hours[0]), |
| 77 | + index2 = getHourIndex(hours[1]); |
| 78 | + if (tokens.length < 5) { |
| 79 | + String suffix = tokens[3]; |
| 80 | + if (suffix.equals("PM")) |
| 81 | + index1+=12; |
| 82 | + else if (suffix.equals("AM") && index1 == 12) |
| 83 | + index1 = 24; |
| 84 | + } |
| 85 | + return new int[]{index1, index2}; |
| 86 | + } |
| 87 | + |
| 88 | + public static int getHourIndex(String hour) { |
| 89 | + String tokens[] = hour.split(" "); |
| 90 | + int num = 0; |
| 91 | + if (hour.split(":").length == 1) |
| 92 | + num = Integer.parseInt(tokens[0]); |
| 93 | + else |
| 94 | + num = Integer.parseInt(hour.split(":")[0]); |
| 95 | + if (tokens.length > 1) { |
| 96 | + String suffix = tokens[1]; |
| 97 | + if (suffix.equals("PM")) |
| 98 | + num+=12; |
| 99 | + else if (suffix.equals("AM") && num == 12) |
| 100 | + num = 24; |
| 101 | + } |
| 102 | + return num; |
| 103 | + } |
| 104 | + |
| 105 | + public static int getDayIndex(String day) { |
| 106 | + for(int i = 0; i < dayslist.length; i++) |
| 107 | + if (day.equals(dayslist[i])) |
| 108 | + return i; |
| 109 | + return -1; |
| 110 | + } |
| 111 | +} |