-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay9.java
More file actions
80 lines (77 loc) · 2.1 KB
/
Day9.java
File metadata and controls
80 lines (77 loc) · 2.1 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
74
75
76
77
78
79
80
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;
public class Day9 {
ArrayList<String> lines = new ArrayList<>();
String input9 = "input9";
ArrayList<List<String>> preamble = new ArrayList<>();
static ArrayList<Long> list = new ArrayList<>();
ArrayList<Long> prefixSum = new ArrayList<>();
public void readFile(){
try{
File file = new File(input9);
Scanner input = new Scanner(file);
BufferedReader br = new BufferedReader(new FileReader(input9));
String line = br.readLine();
long sum = 0;
while(input.hasNext()) {
long l = Long.parseLong(line);
prefixSum.add(sum);
list.add(l);
sum += l;
line = input.nextLine();
}
System.out.println(lines);
} catch (Exception e) {
e.printStackTrace();
}
}
public long ContiguousSet() {
for(int i = 0; i < prefixSum.size(); i++){
for(int j = 1; j < prefixSum.size(); j++){
long start = prefixSum.get(i);
long finish = prefixSum.get(j);
if(finish - start == 25){
long min = Long.MAX_VALUE;
long max = Long.MIN_VALUE;
for(int k = i; k < j; k++){
if(list.get(k) < min){
min = list.get(k);
}
else if(list.get(k) > max){
max = list.get(k);
}
}
return min + max;
}
}
}
return -1;
}
public long partOne(List<Long> list){
for(int i = 25; i < list.size(); i++){
if(!checkValidity(list, i)){
return list.get(i);
}
}
return -1;
}
public boolean checkValidity(List<Long> list, int index){
//uses hashing for an O(N)
Set<Long> values = new HashSet<>();
for(int i = index - 25; i < index; i++){
if(values.contains(list.get(index) - list.get(i))){
return true;
}
values.add(list.get(i));
}
return false;
}
public static void main(String[] args) {
Day9 day9 = new Day9();
day9.readFile();
day9.ContiguousSet();
day9.partOne(list);
}
}