Day 5: If You Give A Seed A Fertilizer
Part one was the most medative thing I have done all day…
Then I got to part two. It turns out that I got a working solution very quickly, but I was unable to run it to get an answer in a reasonable time. I spent a long time trying to run the code to get an answer. This is the first time that I have had to consider computation time in programming as I'm usually just doing web stuff that doesn't require it. So that's a bit of an eye opener and has got me thinking differently, for example planning whether to store data or continue operating with it on the fly, and not walking through the problem linearly but trying to find some way to compress the amount of loops you have to make.
Please note: running part two in browser will probably crash it. I ended up running the code directly with Node.js and it took 1 hour to compute the answer, checking a total of: 2,855,550,144 seeds.
Update: I just watched the Primeagen's speed run video where he solves part two from scratch in 23 minutes, and computes the answer in like a second, with Javascript. Ohhh so you're not supposed to brute force the answer? XD
It was really cool to see how he reads the question and instantly understands what the issues will be, what to avoid etc, and has a plan to avoid them. I had no idea. Even though I got nowhere close to being able to solve the problem without brute forcing it, or even understood what the Primeagen did, I feel that I have learnt something, and gained extra awareness.
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
const inputToUse = testInput;
// ! Make a database of necessary seeds
const inputParts = inputToUse.split(/\n\n/);
console.log(inputParts);
const seedListInput = inputParts[0];
// console.log('seedListInput',seedListInput);
const seedListInputArr = seedListInput.split(':');
const seedList = seedListInputArr[1].trim().split(' ');
console.log('seedList',seedList);
const seedDB = [];
seedList.forEach(function(seedNo) { // part 1
const dbEntry = {
id: seedNo,
location: 'unknown'
}
seedDB.push(dbEntry);
});
// console.log('seedDB',seedDB);
// ! Get the maps ready
const mapInputs = [];
inputParts.forEach(function(part, i){
if( i != 0 ) {
mapInputs.push(part);
}
});
// console.log('mapInputs',mapInputs);
const maps = [];
mapInputs.forEach(function(input){
const inputArr = input.split(':');
// console.log(inputArr);
const mapName = inputArr[0].replace(' map','').trim();
const rulesStr = inputArr[1].trim();
const rulesStrs = rulesStr.split(/\n/);
// console.log('rulesStrs',rulesStrs);
const rulesObj = [];
rulesStrs.forEach(function(str){
const cleanStr = str.trim();
const rulesArr = cleanStr.split(' ');
const numRulesArr = [];
rulesArr.forEach(function(numStr){
numRulesArr.push(parseFloat(numStr));
});
rulesObj.push(numRulesArr);
});
// console.log('rulesObj',rulesObj);
const mapObj = {
name: mapName,
map: rulesObj
}
maps.push(mapObj);
});
// console.log('maps',maps);
// ! We can pass a source number and a map name to get the corresponding id for that map
function getCorrespondingID(sourceNo, mapName) {
var correspondingID = sourceNo;
// console.log('requested to use map:',mapName);
// console.log('maps',maps);
const mapToUse = Object.values(maps).find((obj) => {
return obj.name == mapName;
});
// console.log('mapToUse',mapToUse);
const map = mapToUse.map;
// console.log('map for this is:', map);
// for each rule in the map
map.forEach(function(rule){
const minSourceNo = rule[1];
// console.log('minSourceNo',minSourceNo);
const range = rule[2];
const maxSourceNo = +minSourceNo + +(range-1);
// console.log('maxSourceNo',maxSourceNo);
if( sourceNo >= minSourceNo && sourceNo<=maxSourceNo ) {
// we need to use this rule to find the corresponding ID
// console.log(`${sourceNo} lies in range between ${minSourceNo} and ${maxSourceNo}`);
const distanceFromMinNo = sourceNo - minSourceNo;
const minDestinationNo = rule[0];
correspondingID = minDestinationNo + distanceFromMinNo;
}
});
return( correspondingID );
}
// part 1
// ! Find the seed locations to update the awesome seedDB
// seedList.forEach(function(seedNo) {
// console.log(`––– Examining seed ${seedNo}`);
// const soilNo = getCorrespondingID(seedNo,'seed-to-soil');
// const fertilizerNo = getCorrespondingID(soilNo,'soil-to-fertilizer');
// const waterNo = getCorrespondingID(fertilizerNo,'fertilizer-to-water');
// const lightNo = getCorrespondingID(waterNo,'water-to-light');
// const temperatureNo = getCorrespondingID(lightNo,'light-to-temperature');
// const humidityNo = getCorrespondingID(temperatureNo,'temperature-to-humidity');
// const locationNo = getCorrespondingID(humidityNo,'humidity-to-location');
// // locationNo = 1;
// // console.log('locationNo',locationNo);
// const seedEntryToUpdate = Object.values(seedDB).find((obj) => {
// return obj.id == seedNo;
// });
// console.log('seedEntryToUpdate',seedEntryToUpdate);
// seedEntryToUpdate.location = locationNo;
// });
// ! Check the updated DB
// console.log('seedDB', seedDB);
// const allLocations = [];
// seedDB.forEach(function(entry){
// const location = entry.location;
// allLocations.push(location);
// })
// const closestLocationNo = Math.min(...allLocations);
// console.log('closestLocationNo',closestLocationNo);
// part 2
function isOdd(num) {
return num % 2 == 1;
}
const seedListByRange = [];
seedList.forEach(function(seedNo, i) {
const cleanSeedNo = parseFloat(seedNo);
const realCount = i+1;
if( isOdd(realCount) ) {
seedListByRange.push([cleanSeedNo]);
} else {
seedListByRange[seedListByRange.length-1].push(cleanSeedNo);
}
});
// console.log('seedListByRange',seedListByRange);
var totalSeedCount = 0;
var smallestLocationNo = null;
seedListByRange.forEach(function(seedRange) {
const rangeMin = seedRange[0];
const range = seedRange[1];
const rangeMax = rangeMin + (range-1);
for( var seedNo = rangeMin; seedNo <= rangeMax; seedNo++ ) {
totalSeedCount = totalSeedCount + 1;
// console.log(`checking seed ${totalSeedCount}`);
const soilNo = getCorrespondingID(seedNo,'seed-to-soil');
const fertilizerNo = getCorrespondingID(soilNo,'soil-to-fertilizer');
const waterNo = getCorrespondingID(fertilizerNo,'fertilizer-to-water');
const lightNo = getCorrespondingID(waterNo,'water-to-light');
const temperatureNo = getCorrespondingID(lightNo,'light-to-temperature');
const humidityNo = getCorrespondingID(temperatureNo,'temperature-to-humidity');
const locationNo = getCorrespondingID(humidityNo,'humidity-to-location');
// const locationNo = 0;
if( smallestLocationNo === null ) {
smallestLocationNo = locationNo;
} else if( locationNo < smallestLocationNo ) {
smallestLocationNo = locationNo;
}
}
});
console.log('totalSeedCount',totalSeedCount);
console.log('smallestLocationNo',smallestLocationNo);
const endTime = performance.now() / 1000;
console.log(`end at ${endTime} seconds`);