-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD7.java
103 lines (96 loc) · 3.68 KB
/
D7.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
import java.io.*;
import java.util.ArrayList;
public class D7 {
private static BufferedReader dataReader() {
File file = new File("/Users/tingyi/code/AoC/2021/d7-data.txt");
try {
BufferedReader data = new BufferedReader(new FileReader(file));
return data;
} catch (FileNotFoundException e) {
System.out.println("The file does not exist.");
return null;
}
}
private static void part1() throws IOException {
BufferedReader inputData = dataReader();
ArrayList<Integer> crabSubmarines = new ArrayList<>();
int largestX = 0;
int smallestX = 0;
int idx = 0;
for (String crabLocation : inputData.readLine().split(",")) {
int convertCrabLocationToNum = Integer.parseInt(crabLocation);
crabSubmarines.add(convertCrabLocationToNum);
largestX = convertCrabLocationToNum > largestX ? convertCrabLocationToNum : largestX;
if (idx == 0) {
smallestX = convertCrabLocationToNum;
idx += 1;
} else {
smallestX = convertCrabLocationToNum < smallestX ? convertCrabLocationToNum : smallestX;
}
}
int cheapestFuelCost = 0;
while (smallestX <= largestX) {
int tempCost = 0;
for (Integer crabLocation : crabSubmarines) {
tempCost += Math.abs(crabLocation - smallestX);
}
if (idx == 1) {
cheapestFuelCost = tempCost;
idx += 1;
} else {
cheapestFuelCost = cheapestFuelCost < tempCost ? cheapestFuelCost : tempCost;
}
smallestX += 1;
}
System.out.println(String.format("part 1: %s", cheapestFuelCost));
}
private static void part2() throws IOException {
BufferedReader inputData = dataReader();
ArrayList<Integer> crabSubmarines = new ArrayList<>();
int largestX = 0;
int smallestX = 0;
int idx = 0;
for (String crabLocation : inputData.readLine().split(",")) {
int convertCrabLocationToNum = Integer.parseInt(crabLocation);
crabSubmarines.add(convertCrabLocationToNum);
largestX = convertCrabLocationToNum > largestX ? convertCrabLocationToNum : largestX;
if (idx == 0) {
smallestX = convertCrabLocationToNum;
idx += 1;
} else {
smallestX = convertCrabLocationToNum < smallestX ? convertCrabLocationToNum : smallestX;
}
}
int cheapestFuelCost = 0;
while (smallestX <= largestX) {
int tempCost = 0;
for (Integer crabLocation : crabSubmarines) {
tempCost += fuelCalculation(smallestX, crabLocation);
}
if (idx == 1) {
cheapestFuelCost = tempCost;
idx += 1;
} else {
cheapestFuelCost = cheapestFuelCost < tempCost ? cheapestFuelCost : tempCost;
}
smallestX += 1;
}
System.out.println(String.format("part 2: %s", cheapestFuelCost));
}
private static int fuelCalculation(Integer aimX, Integer crabLocation) {
int sum = 0;
int addUp = 1;
int largerX = crabLocation > aimX ? crabLocation : aimX;
int smallerX = largerX == crabLocation ? aimX : crabLocation;
while (largerX > smallerX) {
smallerX += 1;
sum += addUp;
addUp += 1;
}
return sum;
}
public static void main(String[] args) throws Exception {
part1();
part2();
}
}