-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay1.java
47 lines (38 loc) · 1.08 KB
/
Day1.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
package aoc19;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import myutils19.StaticUtils;
public class Day1 {
private List<Integer> fuelMasses;
private final double divisionFactor = 3.0;
private final int subtractionFactor = 2;
public Day1(File inputFile) {
fuelMasses = StaticUtils.fileToStringList(inputFile)
.stream()
.map(n -> Integer.parseInt(n))
.collect(Collectors.toList());
}
public int run1() {
return fuelMasses
.stream()
.map(mass -> Math.floor(mass / divisionFactor) - subtractionFactor)
.reduce(0.0, (a, b) -> a + b)
.intValue();
}
public int run2() {
int totalRequirement = 0;
for(int mass : fuelMasses) {
while(mass > 0) {
mass = ((int) Math.floor(mass / divisionFactor)) - subtractionFactor;
if(mass > 0)
totalRequirement += mass;
}
}
return totalRequirement;
}
public static void main(String[] args) {
Day1 test = new Day1(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2019\\Day 1\\InputFile.txt"));
System.out.println(test.run2());
}
}