Created
August 6, 2013 12:36
-
-
Save VijayKrishna/6164074 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
public class FootTrafficAnalysis { | |
HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> dataStore = | |
new HashMap<Integer, HashMap<Integer, ArrayList<Integer>>>(); | |
public static void main(String[] args) { | |
new FootTrafficAnalysis().channelLogFile(new File("simple.in")); | |
new FootTrafficAnalysis().channelLogFile(new File("simple2.in")); | |
} | |
public void channelLogFile(File file) { | |
Scanner scanner = null; | |
try { | |
scanner = new Scanner(file); | |
} catch (FileNotFoundException ex) { | |
System.err.println(ex); | |
} | |
int N = Integer.parseInt(scanner.nextLine()); | |
while(scanner.hasNextLine()) { | |
String log = scanner.nextLine(); | |
processLog(log); | |
} | |
for(Integer room : dataStore.keySet()) { | |
HashMap<Integer, ArrayList<Integer>> visitors = dataStore.get(room); | |
int total = 0; | |
int totalVisitors = 0; | |
for(Integer visitor : visitors.keySet()) { | |
totalVisitors += 1; | |
ArrayList<Integer> times = visitors.get(visitor); | |
Collections.sort(times); | |
int len = times.size(); | |
if(times.size() % 2 != 0) { | |
throw new RuntimeException("visitor does not leave the room"); | |
} | |
for(int i = 0; i < len; i += 2) { | |
total += times.get(i + 1) - times.get(i); | |
} | |
} | |
int average = total / totalVisitors; | |
System.out.println( | |
"Room " | |
+ room | |
+ ", " | |
+ average | |
+ " minute average visit, " | |
+ totalVisitors | |
+ " visitor total"); | |
} | |
} | |
private void processLog(String log) { | |
TrafficLog trafficLog = TrafficLog.getInstance(log); | |
int room = trafficLog.getRoomId(); | |
int visitor = trafficLog.getVisitorId(); | |
int time = trafficLog.getTimeStamp(); | |
HashMap<Integer, ArrayList<Integer>> roomData = null; | |
if(dataStore.containsKey(room)) { | |
roomData = dataStore.get(room); | |
} else { | |
roomData = new HashMap<Integer, ArrayList<Integer>>(); | |
dataStore.put(room, roomData); | |
} | |
ArrayList<Integer> times = null; | |
if(roomData.containsKey(visitor)) { | |
times = roomData.get(visitor); | |
} else { | |
times = new ArrayList<Integer>(); | |
roomData.put(visitor, times); | |
} | |
times.add(time); | |
} | |
} | |
class TrafficLog { | |
public static char EnterEvent = 'I'; | |
public static char ExitEvent = 'O'; | |
private int visitorId; | |
private int roomId; | |
private char eventType; | |
private int timeStamp; | |
public static TrafficLog getInstance(String log) { | |
String[] logArray = log.split(" "); | |
if(logArray.length < 4) throw new RuntimeException("malformed log."); | |
return new TrafficLog( | |
Integer.parseInt(logArray[0]), | |
Integer.parseInt(logArray[1]), | |
logArray[2].charAt(0), | |
Integer.parseInt(logArray[3]) | |
); | |
} | |
private TrafficLog(int visitorId, int roomId, char eventType, int time) { | |
if(eventType != EnterEvent && eventType != ExitEvent) { | |
throw new RuntimeException("unknown event type."); | |
} | |
this.visitorId = visitorId; | |
this.roomId = roomId; | |
this.eventType = eventType; | |
this.timeStamp = time; | |
} | |
public int getRoomId() { | |
return this.roomId; | |
} | |
public int getVisitorId() { | |
return this.visitorId; | |
} | |
public char getEventType() { | |
return this.eventType; | |
} | |
public int getTimeStamp() { | |
return this.timeStamp; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 | |
0 0 I 540 | |
1 0 I 540 | |
0 0 O 560 | |
1 0 O 560 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Room 0, 20 minute average visit, 2 visitor total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 | |
0 11 I 347 | |
1 13 I 307 | |
2 15 I 334 | |
3 6 I 334 | |
4 9 I 334 | |
5 2 I 334 | |
6 2 I 334 | |
7 11 I 334 | |
8 1 I 334 | |
0 11 O 376 | |
1 13 O 321 | |
2 15 O 389 | |
3 6 O 412 | |
4 9 O 418 | |
5 2 O 414 | |
6 2 O 349 | |
7 11 O 418 | |
8 1 O 418 | |
0 12 I 437 | |
1 28 I 343 | |
2 32 I 408 | |
3 15 I 458 | |
4 18 I 424 | |
5 26 I 442 | |
6 7 I 435 | |
7 19 I 456 | |
8 19 I 450 | |
0 12 O 455 | |
1 28 O 374 | |
2 32 O 495 | |
3 15 O 462 | |
4 18 O 500 | |
5 26 O 479 | |
6 7 O 493 | |
7 19 O 471 | |
8 19 O 458 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Room 1, 85 minute average visit, 1 visitor total | |
Room 11, 57 minute average visit, 2 visitors total | |
Room 12, 19 minute average visit, 1 visitor total | |
Room 13, 15 minute average visit, 1 visitor total | |
Room 15, 30 minute average visit, 2 visitors total | |
Room 18, 77 minute average visit, 1 visitor total | |
Room 19, 12 minute average visit, 2 visitors total | |
Room 2, 48 minute average visit, 2 visitors total | |
Room 26, 38 minute average visit, 1 visitor total | |
Room 28, 32 minute average visit, 1 visitor total | |
Room 32, 88 minute average visit, 1 visitor total | |
Room 6, 79 minute average visit, 1 visitor total | |
Room 7, 59 minute average visit, 1 visitor total | |
Room 9, 85 minute average visit, 1 visitor total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment