-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.js
134 lines (127 loc) · 3.98 KB
/
day5.js
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
const { parse } = require('path');
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream('input.txt'),
});
let current = 0;
let seeds = [];
const seedToSoil = [];
const soilToFertilizer = [];
const fertilizerToWater = [];
const waterToLight = [];
const lightToTemperature = [];
const temperatureToHumidity = [];
const humidityToLocation = [];
let minLocation = Number.MAX_SAFE_INTEGER;
lineReader.on('line', function (line) {
if (line.includes('seeds:')) {
const seedsList = line.split('seeds: ')[1].split(' ');
for (let i = 0; i < seedsList.length; i = i + 2) {
const start = parseInt(seedsList[i]);
const end = start + parseInt(seedsList[i + 1]);
seeds.push([start, end]);
}
} else if (line.includes('seed-to-soil')) {
current = 1;
} else if (line.includes('soil-to-fertilizer')) {
current = 2;
} else if (line.includes('fertilizer-to-water')) {
current = 3;
} else if (line.includes('water-to-light')) {
current = 4;
} else if (line.includes('light-to-temperature')) {
current = 5;
} else if (line.includes('temperature-to-humidity')) {
current = 6;
} else if (line.includes('humidity-to-location')) {
current = 7;
} else {
const parts = line.split(' ');
const range = parseInt(parts[2]);
const source = parseInt(parts[1]);
const destination = parseInt(parts[0]);
switch (current) {
case 1:
seedToSoil.push([source, destination, range]);
break;
case 2:
soilToFertilizer.push([source, destination, range]);
break;
case 3:
fertilizerToWater.push([source, destination, range]);
break;
case 4:
waterToLight.push([source, destination, range]);
break;
case 5:
lightToTemperature.push([source, destination, range]);
break;
case 6:
temperatureToHumidity.push([source, destination, range]);
break;
case 7:
humidityToLocation.push([source, destination, range]);
break;
}
}
});
lineReader.on('close', function () {
seeds.forEach(([start, end]) => {
for (let i = start; i < end; i++) {
const seed = i;
let soil = seed;
seedToSoil.forEach((entry) => {
// 0 - seed
// 1 - soil
// 2 - range
if (seed >= entry[0] && seed < entry[0] + entry[2]) {
const offset = seed - entry[0];
soil = entry[1] + offset;
}
});
let fertilizer = soil;
soilToFertilizer.forEach((entry) => {
if (soil >= entry[0] && soil < entry[0] + entry[2]) {
const offset = soil - entry[0];
fertilizer = entry[1] + offset;
}
});
let water = fertilizer;
fertilizerToWater.forEach((entry) => {
if (fertilizer >= entry[0] && fertilizer < entry[0] + entry[2]) {
const offset = fertilizer - entry[0];
water = entry[1] + offset;
}
});
let light = water;
waterToLight.forEach((entry) => {
if (water >= entry[0] && water < entry[0] + entry[2]) {
const offset = water - entry[0];
light = entry[1] + offset;
}
});
let temperature = light;
lightToTemperature.forEach((entry) => {
if (light >= entry[0] && light < entry[0] + entry[2]) {
const offset = light - entry[0];
temperature = entry[1] + offset;
}
});
let humidity = temperature;
temperatureToHumidity.forEach((entry) => {
if (temperature >= entry[0] && temperature < entry[0] + entry[2]) {
const offset = temperature - entry[0];
humidity = entry[1] + offset;
}
});
let location = humidity;
humidityToLocation.forEach((entry) => {
if (humidity >= entry[0] && humidity < entry[0] + entry[2]) {
const offset = humidity - entry[0];
location = entry[1] + offset;
}
});
minLocation = Math.min(minLocation, location);
}
});
console.log(minLocation);
});