From 3396a87262dc1d318d6113a61abfed98901e98a4 Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 6 Dec 2025 17:51:37 +0000 Subject: [PATCH] 2025: day6: JS --- AdventOfCode2025/day6/.gitignore | 34 ++++++++ AdventOfCode2025/day6/README.md | 15 ++++ AdventOfCode2025/day6/bun.lock | 25 ++++++ AdventOfCode2025/day6/index.ts | 116 ++++++++++++++++++++++++++++ AdventOfCode2025/day6/package.json | 12 +++ AdventOfCode2025/day6/tsconfig.json | 28 +++++++ 6 files changed, 230 insertions(+) create mode 100644 AdventOfCode2025/day6/.gitignore create mode 100644 AdventOfCode2025/day6/README.md create mode 100644 AdventOfCode2025/day6/bun.lock create mode 100644 AdventOfCode2025/day6/index.ts create mode 100644 AdventOfCode2025/day6/package.json create mode 100644 AdventOfCode2025/day6/tsconfig.json diff --git a/AdventOfCode2025/day6/.gitignore b/AdventOfCode2025/day6/.gitignore new file mode 100644 index 0000000..a14702c --- /dev/null +++ b/AdventOfCode2025/day6/.gitignore @@ -0,0 +1,34 @@ +# dependencies (bun install) +node_modules + +# output +out +dist +*.tgz + +# code coverage +coverage +*.lcov + +# logs +logs +_.log +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# caches +.eslintcache +.cache +*.tsbuildinfo + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/AdventOfCode2025/day6/README.md b/AdventOfCode2025/day6/README.md new file mode 100644 index 0000000..7e15d45 --- /dev/null +++ b/AdventOfCode2025/day6/README.md @@ -0,0 +1,15 @@ +# day6 + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run index.ts +``` + +This project was created using `bun init` in bun v1.2.8. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/AdventOfCode2025/day6/bun.lock b/AdventOfCode2025/day6/bun.lock new file mode 100644 index 0000000..898d34b --- /dev/null +++ b/AdventOfCode2025/day6/bun.lock @@ -0,0 +1,25 @@ +{ + "lockfileVersion": 1, + "workspaces": { + "": { + "name": "day6", + "devDependencies": { + "@types/bun": "latest", + }, + "peerDependencies": { + "typescript": "^5", + }, + }, + }, + "packages": { + "@types/bun": ["@types/bun@1.3.3", "", { "dependencies": { "bun-types": "1.3.3" } }, "sha512-ogrKbJ2X5N0kWLLFKeytG0eHDleBYtngtlbu9cyBKFtNL3cnpDZkNdQj8flVf6WTZUX5ulI9AY1oa7ljhSrp+g=="], + + "@types/node": ["@types/node@24.10.1", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="], + + "bun-types": ["bun-types@1.3.3", "", { "dependencies": { "@types/node": "*" } }, "sha512-z3Xwlg7j2l9JY27x5Qn3Wlyos8YAp0kKRlrePAOjgjMGS5IG6E7Jnlx736vH9UVI4wUICwwhC9anYL++XeOgTQ=="], + + "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="], + + "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="], + } +} diff --git a/AdventOfCode2025/day6/index.ts b/AdventOfCode2025/day6/index.ts new file mode 100644 index 0000000..abba951 --- /dev/null +++ b/AdventOfCode2025/day6/index.ts @@ -0,0 +1,116 @@ +const file = Bun.file("./input.txt"); +const content = await file.text().then((c) => c.trim()); + +const filterNonEmpty = (i: string) => i.length > 0 + +type Problem = { + operation: string + numbers: number[] +} + +const problems: Problem[] = []; + +const splitContent = content.split("\n") +const operationsRow = splitContent.pop()!.split(" ").filter(filterNonEmpty) + +for (const line of splitContent) { + const numbers = line.split(" ").filter(filterNonEmpty); + if (problems.length === 0) { + for (let i = 0; i < numbers.length; i++) { + problems.push({ + operation: '', + numbers: [], + }) + } + } + + for (let i = 0; i < numbers.length; i++) { + problems[i]!.numbers.push(parseInt(numbers[i]!)) + } +} + +for (const [index, problem] of problems.entries()) { + problem.operation = operationsRow[index]! +} + +const getReducer = (op: string): (nums: number[]) => number => { + switch (op) { + case '*': + return (nums) => nums.reduce((n, acc) => acc * n, 1) + case '+': + return (nums) => nums.reduce((n, acc) => acc + n, 0) + default: + throw new Error("operation not supported") + } +} + +const solve = (problems: Problem[]): number => { + let part1 = 0; + + for (const problem of problems) { + const reducer = getReducer(problem.operation) + part1 += reducer(problem.numbers) + } + + return part1 +} + +const transposeProblems = (str: string): Problem[] => { + const charArr = str.split("\n").map(c => c.split("")) + + const newCharArr: string[][] = []; + + for (let i = 0; i < charArr.length; i++) { + for (let j = 0; j < charArr[0]!.length; j++) { + if (!newCharArr[j]) { + newCharArr[j] = [] + } + newCharArr[j]![i]! = charArr[i]![j]! ?? ' ' + } + } + + const problems: Problem[] = []; + let workingProblem: Problem = { + numbers: [], + operation: "" + } + let index = 0; + + for (const line of newCharArr) { + if (line.every(c => c === " ")) { + problems.push(workingProblem) + workingProblem = { + numbers: [], + operation: '' + } + index = 0 + continue + } + + if (index === 0) { + const op = line.pop() + if (op !== " ") { + workingProblem.operation = op! + } + } + + const numbers = line.filter(c => c !== " ") + if (numbers.length === 0) { + continue + } + + const number = parseInt(numbers.join("")) + workingProblem.numbers.push(number) + } + + problems.push(workingProblem) + + return problems +} + +const part1 = solve(problems) +console.log("Part 1: ", part1) + +const part2Problems = transposeProblems(content) +const part2 = solve(part2Problems) +console.log("Part 2: ", part2) diff --git a/AdventOfCode2025/day6/package.json b/AdventOfCode2025/day6/package.json new file mode 100644 index 0000000..f020d56 --- /dev/null +++ b/AdventOfCode2025/day6/package.json @@ -0,0 +1,12 @@ +{ + "name": "day6", + "module": "index.ts", + "type": "module", + "private": true, + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5" + } +} diff --git a/AdventOfCode2025/day6/tsconfig.json b/AdventOfCode2025/day6/tsconfig.json new file mode 100644 index 0000000..9c62f74 --- /dev/null +++ b/AdventOfCode2025/day6/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + // Environment setup & latest features + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}