Public
Edited
Dec 10, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
import { SankeyChart } from "@d3/sankey-component"
Insert cell
chart = SankeyChart(
{
links: [
{ source: "seed 00-49", target: "soil 00-36", value: 37 },
{ source: "seed 00-49", target: "soil 37-38", value: 2 },
{ source: "seed 00-49", target: "soil 39-49", value: 11 },
{ source: "seed 50-51", target: "soil 98-99", value: 2 }, //50 98 2
{ source: "seed 52-99", target: "soil 50-53", value: 4 }, //52 50 48
{ source: "seed 52-99", target: "soil 54-97", value: 44 }, // => 0/50/52 | 0/50/98

{ source: "soil 00-36", target: "fertilizer 15-41", value: 27 },
{ source: "soil 00-36", target: "fertilizer 42-48", value: 7 },
{ source: "soil 00-36", target: "fertilizer 49-51", value: 3 }, //0 15 37
{ source: "soil 37-38", target: "fertilizer 52-53", value: 2 }, //37 52 2
{ source: "soil 39-49", target: "fertilizer 00-14", value: 11 }, //39 0 15
{ source: "soil 50-53", target: "fertilizer 00-14", value: 4 }, // => 0/37/39/54 | 0/15/52/54
{ source: "soil 54-97", target: "fertilizer 54-56", value: 3 },
{ source: "soil 54-97", target: "fertilizer 57-60", value: 4 },
{ source: "soil 54-97", target: "fertilizer 61-99", value: 37 },
{ source: "soil 98-99", target: "fertilizer 61-99", value: 2 },

{ source: "fertilizer 00-14", target: "water 11-53", value: 15 }, //49 53 8
{ source: "fertilizer 15-41", target: "water 11-53", value: 27 }, //0 11 42
{ source: "fertilizer 42-48", target: "water 00-06", value: 7 }, //42 0 7
{ source: "fertilizer 49-51", target: "water 53-60", value: 3 }, //57 7 4
{ source: "fertilizer 52-53", target: "water 53-60", value: 2 }, // => 0/42/49/57/61
{ source: "fertilizer 54-56", target: "water 53-60", value: 3 },
{ source: "fertilizer 57-60", target: "water 07-10", value: 4 },
{ source: "fertilizer 61-99", target: "water 61-99", value: 39 }
]
},
{
nodeGroup: (d) => d.id.split(" ")[0], // take first word for color
nodeSort: (a, b) => a.id.localeCompare(b.id),
//nodeAlign, // e.g., d3.sankeyJustify; set by input above
//linkColor, // e.g., "source" or "target"; set by input above
format: (
(f) => (d) =>
`${f(d)} TWh`
)(d3.format(",.1~f")),
width,
height: 600
}
)
Insert cell
Object.keys(almanac)
.filter((key) => key != "seeds")
.map((key, idx, arr) =>
Array.from(
new Set(
almanac[key]
.map((mapping) => mapping.dest)
.concat(0)
.concat(almanac[key].map((mapping) => mapping.dest + mapping.count))
.filter((d) => d != 100)
.concat(
(idx > 0 ? almanac[arr[idx - 1]] : [])
.map((mapping) => mapping.source)
.concat(0)
.concat(
(idx > 0 ? almanac[arr[idx - 1]] : []).map(
(mapping) => mapping.source + mapping.count
)
)
.filter((d) => d != 100)
)
)
).sort((a, b) => a - b)
)
Insert cell
Object.keys(almanac)
.filter((key) => key != "seeds")
.reduce((arr, key) => {
let sum = 0;
almanac[key].forEach((d, idx) => {
arr.push({
source: key.split("-to-")[0] + " " + idx,
target: key.split("-to-")[1] + " " + idx,
value: d.count
});
sum += d.count;
});
arr.push({
source: key.split("-to-")[0] + " 0",
target: key.split("-to-")[1] + " 0",
value: 100 - sum
});
return arr;
}, [])
Insert cell
Object.keys(allMappings).reduce((arr, key) => {
allMappings[key].forEach((v, k) => {
arr.push({
source: key.split("-to-")[0] + " " + k,
target: key.split("-to-")[1] + " " + v,
value: 1
});
});
return arr;
}, [])
Insert cell
Insert cell
Insert cell
Insert cell
mappings = almanac.seeds.map((seed) => {
let id = seed;
Object.keys(almanac)
.filter((key) => key != "seeds")
.forEach((key) => {
let mapping = almanac[key].find(
(map) => map.source <= id && map.source + map.count >= id
);
if (mapping) {
id = id - mapping.source + mapping.dest;
}
});
return id;
})
Insert cell
result1 = Math.min(...mappings)
Insert cell
Insert cell
/*mappings2 = almanac.seeds.map((seed, idx) => {
let min = Number.MAX_SAFE_INTEGER;
if (idx % 2 == 0) {
for (let n = 0; n < almanac.seeds[idx + 1]; n++) {
let id = seed + n;
Object.keys(almanac)
.filter((key) => key != "seeds")
.forEach((key) => {
let mapping = almanac[key].find(
(map) => map.source <= id && map.source + map.count >= id
);
if (mapping) {
id = id - mapping.source + mapping.dest;
}
});
min = Math.min(min, id);
}
}
return min;
})*/
Insert cell
findMappings2 = function* (seed, idx) {
let min = Number.MAX_SAFE_INTEGER;
if (idx % 2 == 0) {
for (let n = 0; n < almanac.seeds[idx + 1]; n++) {
let id = seed + n;
Object.keys(almanac)
.filter((key) => key != "seeds")
.forEach((key) => {
let mapping = almanac[key].find(
(map) => map.source <= id && map.source + map.count >= id
);
if (mapping) {
id = id - mapping.source + mapping.dest;
}
});
min = Math.min(min, id);
}
}
yield min;
}
Insert cell
mappings2 = almanac.seeds.map((seed, idx) => findMappings2(seed, idx))
Insert cell
result2 = Math.min(...mappings2)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more