Created
December 5, 2023 05:30
-
-
Save bluepichu/364d6c4ff7a218095f960de62002d8a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); |
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
let ans = Infinity;
Might've been a better way to initialize ans for any value to be less than it.