-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 15-1.java
67 lines (59 loc) · 2.06 KB
/
Day 15-1.java
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
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
List<Integer> starting = new ArrayList<Integer>();
starting = populateArray();
secondStar(starting);
}
public static int secondStar(List<Integer> values){
HashMap<Integer, Integer> locationvalues = new HashMap<Integer, Integer>();
for (int i = 0; i < values.size()-1; i++){
locationvalues.put(values.get(i),i+1);
}
System.out.println(locationvalues);
int currlocation = values.size();
int lastVal = values.get(values.size()-1);
//System.out.println("vals: " + lastVal);
while (currlocation != 30000000){
//System.out.println("looking for: " + lastVal);
if (locationvalues.containsKey(lastVal)){
//System.out.println("map contains: " + lastVal);
int previous = locationvalues.get(lastVal);
locationvalues.put(lastVal, currlocation);
//System.out.println("current location: " + currlocation);
//System.out.println("previous location: " + previous);
lastVal = currlocation - previous;
} else {
System.out.println(locationvalues.size());
locationvalues.put(lastVal, currlocation);
lastVal = 0;
}
//System.out.println("new value: " + lastVal);
//locationvalues.put(lastVal, currlocation);
//System.out.println(locationvalues);
currlocation++;
}
//System.out.println(locationvalues);
System.out.println("last value: " + lastVal);
return 0;
}
public static List<Integer> populateArray(){
List<Integer> ints = new ArrayList<Integer>();
try{
File myObj = new File("puzzle.txt");
Scanner myReader = new Scanner(myObj);
String data = myReader.nextLine();
int index = 0;
String[] array = data.split(",");
for (int i = 0; i < array.length; i++){
ints.add(Integer.parseInt(array[i]));
}
}catch (Exception e){
}
return ints;
}
}