Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 5, 2023 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluepichu/364d6c4ff7a218095f960de62002d8a0 to your computer and use it in GitHub Desktop.
Save bluepichu/364d6c4ff7a218095f960de62002d8a0 to your computer and use it in GitHub Desktop.
import { Advent, f, fm, chr, ord, int } from "advent";
import { Set, Map } from "immutable";
const { compute, computeCheck } = await Advent({ day: 5 });
compute(2, async (input) => {
const seeds = input.all().split("\n")[0].split(": ")[1].split(" ").map(int);
console.log(seeds);
const maps = input.all().split("\n\n").slice(1).map((m) => {
const parts = m.split("\n").slice(1);
return parts.map((p) => {
const [dst, src, len] = p.split(" ").map(int);
return { dst, src, len };
});
})
let ans = 9999999999999999;
for (let i = 0; i < seeds.length; i += 2) {
console.log(i);
let x = seeds[i];
let start = x;
let next = seeds[i + 1];
let end = x + next;
while (start < end) {
// console.log("", end - start);
x = start;
for (let map of maps) {
for (let { dst, src, len } of map) {
if (x >= src && x < src + len) {
next = Math.min(next, len - (x - src));
x = dst + (x - src);
break;
}
}
// console.log(map, x);
}
ans = Math.min(ans, x);
start += next;
}
}
return ans;
});
@GreatGameGal
Copy link

let ans = Infinity;
Might've been a better way to initialize ans for any value to be less than it.

@bluepichu
Copy link
Author

I always forget that Infinity is a valid numeric value in JS/TS...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment