-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartOne.java
More file actions
189 lines (158 loc) · 5.8 KB
/
PartOne.java
File metadata and controls
189 lines (158 loc) · 5.8 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* --- Day 5: If You Give A Seed A Fertilizer ---
*
*
* https://adventofcode.com/2023/day/5
**/
// 462648396
public class PartOne {
record Destination(long start, long increment) {
}
private static ArrayList<Long> seeds = new ArrayList<>();
private static Map<Long, Destination> seedToSoilMap = new HashMap<>();
private static Map<Long, Destination> seedToFertilizerMap = new HashMap<>();
private static Map<Long, Destination> seedToWaterMap = new HashMap<>();
private static Map<Long, Destination> seedToLightMap = new HashMap<>();
private static Map<Long, Destination> seedToTemperatureMap = new HashMap<>();
private static Map<Long, Destination> seedToHumidityMap = new HashMap<>();
private static Map<Long, Destination> seedToLocationMap = new HashMap<>();
public static void main(String[] args) throws Exception {
Map<Long, String> inputMap = new HashMap<>();
long index = 0;
try {
Scanner scanner = new Scanner(new File("./src/input.txt"));
while (scanner.hasNextLine()) {
String input = scanner.nextLine();
if (input.startsWith("seeds: ")) {
addSeeds(input);
} else if ((index == 0 && input.isEmpty()) || input.contains("map")) {
continue;
} else {
inputMap.put(index, input);
index++;
}
}
scanner.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
convertSeedToMap(inputMap);
System.out.println(findMinLocation());
}
/**
* Find the minimum location for the seeds
* @return the minimum location for the seeds
*/
private static long findMinLocation() {
long minMax = Long.MAX_VALUE;
for (long seed : seeds) {
long soil = getMapValue(seedToSoilMap, seed);
long fertilizer = getMapValue(seedToFertilizerMap, soil);
long water = getMapValue(seedToWaterMap, fertilizer);
long light = getMapValue(seedToLightMap, water);
long temperature = getMapValue(seedToTemperatureMap, light);
long humidity = getMapValue(seedToHumidityMap, temperature);
long location = getMapValue(seedToLocationMap, humidity);
minMax = Math.min(minMax, location);
}
return minMax;
}
/**
* Get the value from the map
* @param map the map to get the value from
* @param seed the seed to get the value for
* @return the value from the map
*/
private static long getMapValue(Map<Long, PartOne.Destination> map, long seed) {
long result = seed;
Map.Entry<Long, Destination> entry = findClosestEntry(map, seed);
if (entry == null) {
return result;
}
long seedKey = entry.getKey();
Destination destination = entry.getValue();
if (seed == seedKey) {
result = destination.start;
} else if (seed <= seedKey + destination.increment) {
long difference = seed - seedKey;
result = destination.start + difference;
}
return result;
}
/**
* Find the closest entry in the map
* @param map the map to search
* @param key the key to search for
* @return the closest entry in the map
*/
private static Map.Entry<Long, Destination> findClosestEntry(Map<Long, Destination> map, long key) {
Map.Entry<Long, Destination> closestEntry = null;
for (Map.Entry<Long, Destination> entry : map.entrySet()) {
if (entry.getKey() <= key && (closestEntry == null || entry.getKey() > closestEntry.getKey())) {
closestEntry = entry;
}
}
return closestEntry;
}
/**
* Convert the seed to map
* @param inputList the input list to convert
*/
private static void convertSeedToMap(Map<Long, String> inputList) {
List<Map<Long, Destination>> maps = List.of(
seedToSoilMap,
seedToFertilizerMap,
seedToWaterMap,
seedToLightMap,
seedToTemperatureMap,
seedToHumidityMap,
seedToLocationMap);
long index = 0;
for (Map<Long, Destination> specificMap : maps) {
String line = inputList.get(index);
while (!line.isEmpty()) {
addRangeToMap(line, specificMap);
index++;
if (index >= inputList.size()) {
break;
}
line = inputList.get(index);
}
index++;
}
}
/**
* Add the range to the map
* @param line the line to add
* @param map the map to add to
*/
private static void addRangeToMap(String line, Map<Long, PartOne.Destination> map) {
String[] split = line.split(" ");
long destinationRangeStart = Long.parseLong(split[0]);
long sourceRangeStart = Long.parseLong(split[1]);
long rangeLength = Long.parseLong(split[2]);
Destination destination = new Destination(destinationRangeStart, rangeLength);
map.put(sourceRangeStart, destination);
}
/**
* Add the seeds to the list
* @param input the input to add
*/
private static void addSeeds(String input) {
input = input.replace("seeds:", "");
String[] split = input.split(" ");
for (String s : split) {
if (s.equals("")) {
continue;
}
seeds.add(Long.parseLong(s));
}
}
}