-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.java
100 lines (80 loc) · 3.03 KB
/
Day12.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
package aoc2018.day12;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.IntStream;
public class Day12 {
static char[] readInput(String inputFile) {
char[] chars = null;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(inputFile));
String line = reader.readLine();
chars = line.toCharArray();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return chars;
}
static Map<String, String> read_input_rules(String inputRulesFile) {
Map<String, String> ruleMap = new HashMap<>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(inputRulesFile));
String line = reader.readLine();
while (line != null) {
String[] rule = line.split(" => ");
ruleMap.put(rule[0], rule[1]);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return ruleMap;
}
public static long findPotsWithPlantsAfterGenerations(String inputFile, String inputFileRules, long targetGens) {
final int limitForPerformance = 300;
char[] input = readInput(inputFile);
Map<String, String> rules = read_input_rules(inputFileRules);
PlantArray plants = new PlantArray(input);
PlantArray newGen = null;
int totShifts = plants.getShift();
int generations = 0;
long loopLimit = (targetGens < limitForPerformance ? targetGens : limitForPerformance);
while (generations < loopLimit) {
newGen = new PlantArray(plants);
for (int i = 2; i < plants.size() - 2; i++) {
String pattern = plants.getNeighborsOfPlant(i);
if (rules.containsKey(pattern)) {
newGen.setPlantAt(i, rules.get(pattern).charAt(0));
}
}
// newGen.printAsAString();
plants = new PlantArray(newGen.getArray());
totShifts += plants.getShift();
generations += 1;
}
long currSum = calcSumOfGen(Objects.requireNonNull(newGen), totShifts);
long penSum = calcSumOfGen(newGen, totShifts+1);
if (loopLimit == targetGens) {
return currSum;
}
return currSum + (targetGens - limitForPerformance) * (currSum - penSum);
}
public static long calcSumOfGen(PlantArray gen, int shifts) {
List<Character> result = new ArrayList<>();
for (long i = shifts; i < gen.size(); i++) {
result.add(gen.getChar((int) i));
}
long sum = IntStream.range(0, result.size()).filter(i -> result.get(i) == '#').sum();
for (long i = shifts; i > 0; i--) {
if (gen.getArray()[(int) i] == '#') {
sum -= shifts - i;
}
}
return sum;
}
}