Created
December 25, 2022 05:27
-
-
Save t3dotgg/b88db1d1c54ad5365e2dfc8b818de5c4 to your computer and use it in GitHub Desktop.
typescript aoc day 25 solution
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 input = await Deno.readTextFile("./input.txt"); | |
const lines = input.split("\n"); | |
const dv = { | |
"2": 2, | |
"1": 1, | |
"0": 0, | |
"-": -1, // === -1 | |
"=": -2, // === -2 | |
}; | |
const convertB5ToNum = (number: string): number => { | |
let result = 0; | |
for (let i = 0; i < number.length; i++) { | |
const digit = number[i]; | |
const digitValue = dv[digit as keyof typeof dv]; | |
result += digitValue * Math.pow(5, number.length - i - 1); | |
} | |
return result; | |
}; | |
function convertNumToB5(n: number): string { | |
if (n === 0) return ""; | |
if (n % 5 === 0) return convertNumToB5(Math.floor(n / 5)) + "0"; | |
if (n % 5 === 1) return convertNumToB5(Math.floor(n / 5)) + "1"; | |
if (n % 5 === 2) return convertNumToB5(Math.floor(n / 5)) + "2"; | |
if (n % 5 === 3) return convertNumToB5((n + 2) / 5) + "="; | |
if (n % 5 === 4) return convertNumToB5((n + 1) / 5) + "-"; | |
throw new Error("should never hit here"); | |
} | |
const nums = lines.map(convertB5ToNum); | |
const sum = nums.reduce((a, b) => a + b, 0); | |
console.log(sum); | |
const ans = convertNumToB5(sum); | |
console.log(ans); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment