-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.java
75 lines (73 loc) · 2.46 KB
/
day6.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
68
69
70
71
72
73
74
75
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class day6{
public static void main(String[] args){
System.out.println(part1());
System.out.println(part2());
}
public static int part1(){
ArrayList<Integer> data = openFile("input.txt");
for(int i = 0; i < 80; i++){
for(int j = 0; j < data.size(); j++){
int fish = data.get(j);
if(fish == 0){
data.set(j, 6);
data.add(9);
} else{
data.set(j, fish - 1);
}
}
}
return data.size();
}
public static long part2(){
// The method for part 1 causes an OutOfMemoryError, so it had to be redone
// This method also uses long instead of int because the result is hefty
ArrayList<Integer> data = openFile("input.txt");
ArrayList<Long> numFish = new ArrayList<Long>();
// Populate numFish with input data
for(int i = 0; i < 9; i++){
long numOfThisFish = 0;
for(int j = 0; j < data.size(); j++){
if(data.get(j) == i){
numOfThisFish++;
}
}
numFish.add(numOfThisFish);
}
for(int i = 0; i < 256; i++){
long zeroVal = numFish.get(0);
// Shift left
for(int j = 1; j < numFish.size(); j++){
numFish.set(j-1, numFish.get(j));
}
// Do zero stuff
numFish.set(6, numFish.get(6) + zeroVal);
numFish.set(8, zeroVal);
}
// Get total
long total = 0;
for(int i = 0; i < numFish.size(); i++){
total += numFish.get(i);
}
return total;
}
public static ArrayList<Integer> openFile(String path){
try{
Scanner s = new Scanner(new File(path));
ArrayList<Integer> lines = new ArrayList<Integer>();
while(s.hasNextLine()){
String[] split = s.nextLine().split(",");
for(int i = 0; i < split.length; i++){
lines.add(Integer.parseInt(split[i]));
}
}
s.close();
return lines;
} catch(FileNotFoundException e){
throw new RuntimeException(e);
}
}
}