-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.java
110 lines (94 loc) · 2.87 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.kasteelharry.aoc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Day6 extends Day {
private static final String FILENAME = "\\input\\day6.txt";
private static final String FILENAME_TEST = "\\input\\day6Test.txt";
private static final int DAYS = 80;
private static final int DAYS_PART_TWO = 256;
private List<String> inputList;
private List<Integer> fish;
private long[] totalFish = new long[9];
private long count = 0;
public Day6(boolean partTwo) {
super(FILENAME, partTwo);
this.inputList = new ArrayList<>();
this.fish = new ArrayList<>();
}
public Day6(int exp, boolean partTwo) {
super(FILENAME_TEST, partTwo, exp);
this.inputList = new ArrayList<>();
this.fish = new ArrayList<>();
}
private void parseInput() {
String[] arr = inputList.get(0).trim().split(",");
for (String s : arr) {
this.fish.add(Integer.parseInt(s));
}
}
public void duplicate() {
List<Integer> fishState = new ArrayList<>(this.fish);
for (int i = 0; i < fishState.size(); i++) {
int cycle = this.fish.get(i);
cycle--;
if (cycle < 0) {
this.fish.set(i, 6);
this.fish.add(8);
} else {
this.fish.set(i, cycle);
}
}
}
private void dayCycle() {
for (int i = 0; i < DAYS; i++) {
try {
duplicate();
} catch (OutOfMemoryError ignore){
System.out.println("Failed on day " + i);
return;
}
}
}
private void dayCycle2() {
for (Integer integer : this.fish) {
totalFish[integer]++;
}
for (int i = 0; i < DAYS_PART_TWO; i++) {
try {
long val = totalFish[0];
System.arraycopy(totalFish, 1, totalFish, 0, totalFish.length - 1);
totalFish[8] = val;
totalFish[6] += val;
} catch (OutOfMemoryError ignore){
System.out.println("Failed on day " + i);
return;
}
}
}
@Override
public void start() {
this.inputList = getLines();
parseInput();
dayCycle();
System.out.println(fish.size());
}
@Override
public void startPart2() {
this.inputList = getLines();
parseInput();
dayCycle2();
System.out.println(Arrays.stream(totalFish).sum());
}
public static void main(String[] args) {
Day6 dayTest = new Day6(5934, false);
dayTest.run();
Day6 day6 = new Day6(false);
day6.run();
dayTest = new Day6(0, true);
dayTest.run();
day6 = new Day6(true);
day6.run();
}
}