-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.java
More file actions
73 lines (62 loc) · 1.71 KB
/
Day1.java
File metadata and controls
73 lines (62 loc) · 1.71 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
72
73
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Day1 {
static String fileName = "C:\\Training\\Save Data\\AoC-day1.txt";
public static void main(String[] args) {
findTotal();
}
public static void findTotal() {
Scanner scanner = null;
ArrayList<Integer> matchArray = new ArrayList<Integer>();
ArrayList<String> repeatArray = new ArrayList<String>();
int matchValue = 0;
int total = 0;
boolean foundIt = false;
try {
scanner = new Scanner(new File(fileName));
} catch (FileNotFoundException ex) {
}
while(scanner.hasNext()) {
String mess = scanner.next();
repeatArray.add(mess);
if(mess.charAt(0) == '-') {
total = total - Integer.parseInt(mess.substring(1));
matchArray.add(total);
}
if(mess.charAt(0) == '+') {
total = total + Integer.parseInt(mess.substring(1));
matchArray.add(total);
}
for(int j = 0; j < matchArray.size() - 1; j++) {
if(total == matchArray.get(j)) {
matchValue = total;
System.out.println("Match value:" + matchValue);
}
}
}
System.out.println(total);
//Part 2 starts here
while(!foundIt) {
for(int q = 0; q < repeatArray.size(); q++) {
if(repeatArray.get(q).charAt(0) == '-') {
total = total - Integer.parseInt(repeatArray.get(q).substring(1));
matchArray.add(total);
}
if(repeatArray.get(q).charAt(0) == '+') {
total = total + Integer.parseInt(repeatArray.get(q).substring(1));
matchArray.add(total);
}
for(int j = 0; j < matchArray.size() - 1; j++) {
if(total == matchArray.get(j)) {
matchValue = total;
foundIt = true;
System.out.println("Match value:" + matchValue);
System.exit(0);
}
}
}
}
}
}