Created
December 25, 2022 06:52
-
-
Save p-a/0dfd5e99f67a62c58c6304a9914620cf to your computer and use it in GitHub Desktop.
AoC 2022 Day 25
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you explain how pipe works ? I don't understand from where the functions in the pipe get their arguments from