This repository was archived by the owner on May 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.w
More file actions
325 lines (292 loc) · 11.9 KB
/
Copy pathmain.w
File metadata and controls
325 lines (292 loc) · 11.9 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
bring cloud;
bring expect;
bring fs;
bring math;
inflight class SourceDestition {
pub destinationRangeStart: num;
pub sourceRangeStart: num;
pub rangeLength: num;
new(destinationRangeStart: num, sourceRangeStart: num, rangeLength: num) {
this.destinationRangeStart = destinationRangeStart;
this.sourceRangeStart = sourceRangeStart;
this.rangeLength = rangeLength;
}
pub inflight isInSourceRange(value: num): bool {
return value >= this.sourceRangeStart && value < this.sourceRangeStart + this.rangeLength;
}
pub inflight isInDestinationRange(value: num): bool {
return value >= this.destinationRangeStart && value < this.destinationRangeStart + this.rangeLength;
}
pub inflight convert(value: num): num {
return this.destinationRangeStart + (value - this.sourceRangeStart);
}
}
inflight class SourceDestinationCollection {
pub sourceDestinations: Array<SourceDestition>;
new(sourceDestinations: Array<SourceDestition>) {
this.sourceDestinations = sourceDestinations;
}
pub inflight convert(value: num): num {
for sourceDestition in this.sourceDestinations {
if sourceDestition.isInSourceRange(value) {
return sourceDestition.convert(value);
}
}
return value;
}
}
inflight class Almanac {
seeds: MutArray<num>;
pub isSeedRange: bool;
pub seedToSoilMap: SourceDestinationCollection;
pub soilToFertilizerMap: SourceDestinationCollection;
pub fertilizerToWaterMap: SourceDestinationCollection;
pub waterToLightMap: SourceDestinationCollection;
pub lightToTemperatureMap: SourceDestinationCollection;
pub temperatureToHumidityMap: SourceDestinationCollection;
pub humidityToLocationMap: SourceDestinationCollection;
new(seeds: MutArray<num>, isSeedRange: bool, seedToSoilMap: SourceDestinationCollection, soilToFertilizerMap: SourceDestinationCollection, fertilizerToWaterMap: SourceDestinationCollection, waterToLightMap: SourceDestinationCollection, lightToTemperatureMap: SourceDestinationCollection, temperatureToHumidityMap: SourceDestinationCollection, humidityToLocationMap: SourceDestinationCollection) {
this.seeds = seeds;
this.isSeedRange = isSeedRange;
this.seedToSoilMap = seedToSoilMap;
this.soilToFertilizerMap = soilToFertilizerMap;
this.fertilizerToWaterMap = fertilizerToWaterMap;
this.waterToLightMap = waterToLightMap;
this.lightToTemperatureMap = lightToTemperatureMap;
this.temperatureToHumidityMap = temperatureToHumidityMap;
this.humidityToLocationMap = humidityToLocationMap;
this.seedIndex = 0;
this.maxLoop = 0;
}
pub static inflight parse(input: str, seedsAsRange: bool): Almanac {
let lines = input.split("\n");
let var seeds = MutArray<num> [];
if seedsAsRange {
for seed in lines.at(0).substring(7).split(" ") {
seeds.push(num.fromStr(seed));
}
} else {
for seed in lines.at(0).substring(7).split(" ") {
seeds.push(num.fromStr(seed));
}
}
let sourceDesitionCollections = MutArray<SourceDestinationCollection> [];
let var currentLine = 3;
for i in 0..7 {
let seedToSoils = MutArray<SourceDestition> [];
while lines.at(currentLine) != "" && lines.length > currentLine {
let parts = lines.at(currentLine).split(" ");
seedToSoils.push(new SourceDestition(num.fromStr(parts.at(0)), num.fromStr(parts.at(1)), num.fromStr(parts.at(2))));
currentLine += 1;
}
sourceDesitionCollections.push(new SourceDestinationCollection(seedToSoils.copy()));
currentLine += 2;
}
return new Almanac(
seeds,
seedsAsRange,
sourceDesitionCollections.at(0),
sourceDesitionCollections.at(1),
sourceDesitionCollections.at(2),
sourceDesitionCollections.at(3),
sourceDesitionCollections.at(4),
sourceDesitionCollections.at(5),
sourceDesitionCollections.at(6),
);
}
var seedIndex: num = 0;
var maxLoop: num = 0;
pub inflight nextSeed(): num? {
if this.isSeedRange {
let start = this.seeds.at(0);
let length = this.seeds.at(1);
this.maxLoop += 1;
if this.maxLoop > 100 {
return nil;
}
if this.seedIndex < length - 1 {
let value = start + this.seedIndex;
this.seedIndex += 1;
return value;
}
if this.seedIndex == (length -1) {
this.seeds.popAt(0);
this.seeds.popAt(0);
let value = start + this.seedIndex;
this.seedIndex = 0;
return value;
}
return nil;
} else {
if this.seedIndex >= this.seeds.length {
return nil;
}
let value = this.seeds.at(this.seedIndex);
this.seedIndex += 1;
return value;
}
}
}
let seedToLocationConverter = inflight (seed: num, almanac: Almanac): num => {
let soil = almanac.seedToSoilMap.convert(seed);
let fertilizer = almanac.soilToFertilizerMap.convert(soil);
let water = almanac.fertilizerToWaterMap.convert(fertilizer);
let light = almanac.waterToLightMap.convert(water);
let temperature = almanac.lightToTemperatureMap.convert(light);
let humidity = almanac.temperatureToHumidityMap.convert(temperature);
let location = almanac.humidityToLocationMap.convert(humidity);
return location;
};
let findLowesetLocation = new cloud.Function(inflight (input: str): num => {
let var lines = input;
if lines == "" {
lines = fs.readFile("input");
}
let almanac = Almanac.parse(lines, false);
let locations = MutArray<num> [];
let var seed = almanac.nextSeed();
while seed != nil {
if let seed = seed {
locations.push(seedToLocationConverter(seed, almanac));
}
seed = almanac.nextSeed();
}
return math.min(locations.copy());
}) as "Part 1";
let findLowesetLocationWithSeedRange = new cloud.Function(inflight (input: str): num => {
let var lines = input;
if lines == "" {
lines = fs.readFile("input");
}
let almanac = Almanac.parse(lines, true);
let locations = MutArray<num> [];
let var seed = almanac.nextSeed();
while seed != nil {
if let seed = seed {
locations.push(seedToLocationConverter(seed, almanac));
}
seed = almanac.nextSeed();
}
return math.min(locations.copy());
}) as "Part 2";
test "can parse almanac" {
let almanac = Almanac.parse(
"seeds: 79 14 55 13\n\nseed-to-soil map:\n50 98 2\n52 50 48\n\nsoil-to-fertilizer map:\n0 15 37\n37 52 2\n39 0 15\n\nfertilizer-to-water map:\n49 53 8\n0 11 42\n42 0 7\n57 7 4\n\nwater-to-light map:\n88 18 7\n18 25 70\n\nlight-to-temperature map:\n45 77 23\n81 45 19\n68 64 13\n\ntemperature-to-humidity map:\n0 69 1\n1 0 69\n\nhumidity-to-location map:\n60 56 37\n56 93 4",
false,
);
let seeds = MutArray<num> [];
let var seed = almanac.nextSeed();
while seed != nil {
if let seed = seed {
seeds.push(seed);
}
seed = almanac.nextSeed();
}
expect.equal(seeds, [79, 14, 55, 13]);
expect.equal(
Json.stringify(almanac.seedToSoilMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(50, 98, 2),
new SourceDestition(52, 50, 48),
]))
);
expect.equal(
Json.stringify(almanac.soilToFertilizerMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(0, 15, 37),
new SourceDestition(37, 52, 2),
new SourceDestition(39, 0, 15),
]))
);
expect.equal(
Json.stringify(almanac.fertilizerToWaterMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(49, 53, 8),
new SourceDestition(0, 11, 42),
new SourceDestition(42, 0, 7),
new SourceDestition(57, 7, 4),
]))
);
expect.equal(
Json.stringify(almanac.waterToLightMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(88, 18, 7),
new SourceDestition(18, 25, 70),
]))
);
expect.equal(
Json.stringify(almanac.lightToTemperatureMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(45, 77, 23),
new SourceDestition(81, 45, 19),
new SourceDestition(68, 64, 13),
]))
);
expect.equal(
Json.stringify(almanac.temperatureToHumidityMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(0, 69, 1),
new SourceDestition(1, 0, 69),
]))
);
expect.equal(
Json.stringify(almanac.humidityToLocationMap),
Json.stringify(new SourceDestinationCollection([
new SourceDestition(60, 56, 37),
new SourceDestition(56, 93, 4),
]))
);
expect.equal(seedToLocationConverter(79, almanac), 82);
expect.equal(seedToLocationConverter(14, almanac), 43);
expect.equal(seedToLocationConverter(55, almanac), 86);
expect.equal(seedToLocationConverter(13, almanac), 35);
}
test "can parse almanac with seed range" {
let almanac = Almanac.parse(
"seeds: 79 14 55 13\n\nseed-to-soil map:\n50 98 2\n52 50 48\n\nsoil-to-fertilizer map:\n0 15 37\n37 52 2\n39 0 15\n\nfertilizer-to-water map:\n49 53 8\n0 11 42\n42 0 7\n57 7 4\n\nwater-to-light map:\n88 18 7\n18 25 70\n\nlight-to-temperature map:\n45 77 23\n81 45 19\n68 64 13\n\ntemperature-to-humidity map:\n0 69 1\n1 0 69\n\nhumidity-to-location map:\n60 56 37\n56 93 4",
true,
);
let seeds = MutArray<num> [];
let var seed = almanac.nextSeed();
while seed != nil {
if let seed = seed {
seeds.push(seed);
}
seed = almanac.nextSeed();
}
// write out the ranges 79-92, 55-67
expect.equal(seeds, [
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, // 79-93
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, // 55-67
]);
}
test "is in source range" {
let sourceDestition = new SourceDestition(0, 10, 5);
expect.equal(sourceDestition.isInSourceRange(10), true);
expect.equal(sourceDestition.isInSourceRange(11), true);
expect.equal(sourceDestition.isInSourceRange(12), true);
expect.equal(sourceDestition.isInSourceRange(13), true);
expect.equal(sourceDestition.isInSourceRange(14), true);
expect.equal(sourceDestition.isInSourceRange(15), false);
expect.equal(sourceDestition.isInSourceRange(9), false);
}
test "is in destination range" {
let sourceDestition = new SourceDestition(0, 10, 5);
expect.equal(sourceDestition.isInDestinationRange(0), true);
expect.equal(sourceDestition.isInDestinationRange(1), true);
expect.equal(sourceDestition.isInDestinationRange(2), true);
expect.equal(sourceDestition.isInDestinationRange(3), true);
expect.equal(sourceDestition.isInDestinationRange(4), true);
expect.equal(sourceDestition.isInDestinationRange(5), false);
expect.equal(sourceDestition.isInDestinationRange(6), false);
}
test "can convert source to destination" {
let sourceDestinationCollection = new SourceDestinationCollection([
new SourceDestition(50, 98, 2),
new SourceDestition(52, 50, 48),
]);
expect.equal(sourceDestinationCollection.convert(79), 81);
expect.equal(sourceDestinationCollection.convert(14), 14);
expect.equal(sourceDestinationCollection.convert(55), 57);
expect.equal(sourceDestinationCollection.convert(13), 13);
}