Skip to content

Instantly share code, notes, and snippets.

@zsteva
Last active December 16, 2024 18:58
Show Gist options
  • Save zsteva/2688142e80e3d51fd52ae823434544b1 to your computer and use it in GitHub Desktop.
Save zsteva/2688142e80e3d51fd52ae823434544b1 to your computer and use it in GitHub Desktop.
// part 1
// example:
// input = [...document.getElementsByTagName('code')].map((e) => e.innerText)[5];
input = getInput();
rules = new Set(input.split("\n\n")[0].splitNL());
list1 = input.split("\n\n")[1].splitNL().splitCOL(",");
list1.filter((row) => {
return row.comb2().map(([a, b]) => rules.has(b + "|" + a) ? 1 : 0).sum() == 0;
}).map((row) => row[Math.floor(row.length / 2)]).nums().sum();
// result 6242
// part 2
list1.filter((row) => {
return row.comb2().map(([a, b]) => rules.has(b + "|" + a) ? 1 : 0).sum() > 0;
}).map((row) => {
return row.sort((a, b) => rules.has(b + "|" + a) ? 1 : -1);
}).map((row) => row[Math.floor(row.length / 2)]).nums().sum();
// result 5169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment