Skip to content

Instantly share code, notes, and snippets.

@p-a
Created December 25, 2022 06:52
Show Gist options
  • Save p-a/0dfd5e99f67a62c58c6304a9914620cf to your computer and use it in GitHub Desktop.
Save p-a/0dfd5e99f67a62c58c6304a9914620cf to your computer and use it in GitHub Desktop.
AoC 2022 Day 25
const pipe = (...fns) => v => fns.reduce((a, f) => f(a), v);
const MAPPING = "=-012";
const parse = input =>
input
.split("\n")
.map(line =>
line.split("").map(c => MAPPING.indexOf(c) - 2)
);
const toDecimal = list =>
list.map(nums =>
nums
.reverse()
.map((num, i) => num * Math.pow(5, i))
.reduce((sum, v) => sum + v)
);
const sum = list => list.reduce((sum, v) => sum + v);
const toSNAFU = number =>
[...number.toString(5)]
.map(Number)
.reduceRight(
([res, carry], v) => (
(v += carry),
[[v > 2 ? v - 5 : v, ...res], v > 2 ? 1 : 0]
),
[[], 0]
)[0]
.map(v => MAPPING[v + 2])
.join("");
export const part1 = pipe(parse, toDecimal, sum, toSNAFU);
@mystermiam
Copy link

Could you explain how pipe works ? I don't understand from where the functions in the pipe get their arguments from

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