-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoCDay4.java
More file actions
71 lines (62 loc) · 2.01 KB
/
Copy pathAoCDay4.java
File metadata and controls
71 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Objects Practice - Advent of Code Day 4 Adaptation
* AP CS 2018
*/
import java.util.*;
import java.io.*;
public class AoCDay4{
public static ArrayList<Guard> guards = new ArrayList<Guard>();
public static Guard getGuard(int num){
for(Guard curguard : guards) {
if(curguard.getNumber() == num) {
return curguard;
}
}
return null;
}
public static void addGuard(int num, int start, int end){
Shift s = new Shift(start, end);
guards.add(new Guard(num,s));
// creates a Guard object with given numb and shift object
// adds the new Guard to the guards ArrayList
}
public static void main(String[] args){
try{
Scanner in = new Scanner(new File("Input.txt"));
int [] ary = new int[3];
Guard one = null;
while(in.hasNext()) {
//3 lines at a time
//create array with guard number at index 0, start at 1, end at 2
ary[0] = Integer.parseInt(in.nextLine().substring(1, 3));
ary[1] = Integer.parseInt(in.nextLine().substring(0, 2));
ary[2] = Integer.parseInt(in.nextLine().substring(0, 2));
if(getGuard(ary[0]) == null) {
//adds new guard if there isnt one with current number
addGuard(ary[0],ary[1],ary[2]);
}else {
//if there is one, add just the new shift to the instance data
getGuard(ary[0]).addShift(new Shift(ary[1],ary[2]));
one = getGuard(ary[0]);
}
}
Guard two = one;
two.addShift(new Shift(10,20));
System.out.println(two.totalHoursAsleep());
System.out.println(one.totalHoursAsleep());
int max = 0;
int guardnum = 0;
for(Guard g : guards) {
if(g.totalHoursAsleep() > max) {
//records current max number of minutes and the guard who slept those minutes
max = g.totalHoursAsleep();
guardnum = g.getNumber();
}
}
//output
System.out.println("Guard #" + guardnum + " slept the most with " + max + " minutes.");
}
catch(Exception e){
System.out.println(e);
}
}
}