Skip to content

Commit

Permalink
🤞🪛 Updated with Glitch: day 5, part 2 is too slow
Browse files Browse the repository at this point in the history
  • Loading branch information
Glitch (branch-three-oviraptor) committed Dec 5, 2023
1 parent 9403ce0 commit 6d57ac0
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,64 @@
return Math.min(...paths.map(p => p.location));
},
part2: (data) => {
return data;
const almanac = data.trim().split('\n\n');
const seedRanges = almanac[0].split(':')[1].trim().split(' ').map(Number);
const seeds = [];
for (let i = 0; i < seedRanges.length; i+= 2) {
const s = seedRanges[i];
const r = seedRanges[i + 1];
for (let j = s; j < s + r; j++) {
seeds.push(j);
}
}
const maps = almanac.slice(1).map(m => {
const lines = m.split('\n');
const matchName = /(\w+)-to-(\w+)/;
const matched = lines[0].match(matchName);
const source = matched[1];
const dest = matched[2];
const routes = lines.slice(1).map(l => {
const nums = l.split(' ').map(Number);
return {
s1: nums[1],
s2: nums[1] + nums[2],
d1: nums[0],
d2: nums[0] + nums[2],
r: nums[2]
};
});
return {
source,
dest,
routes
};
});
console.log(seeds, maps);
const paths = seeds.reduce((acc, s) => {
acc.push({ seed: s });
return acc;
}, []);
let step = 'seed';
maps.forEach(m => {
const next = m.dest;
paths.forEach(p => {
const val = p[step];
let isMapped = false;
for (let i = 0; i < m.routes.length; i++) {
if (val >= m.routes[i].s1 && val <= m.routes[i].s2) {
p[next] = (val - m.routes[i].s1) + m.routes[i].d1;
isMapped = true;
break;
}
}
if (!isMapped) {
p[next] = val;
}
});
step = next;
});
console.log(paths)
return Math.min(...paths.map(p => p.location));
}
},
day6: {
Expand Down

0 comments on commit 6d57ac0

Please sign in to comment.