2025: day6: JS

This commit is contained in:
2025-12-06 17:51:37 +00:00
parent 12a56a01f3
commit 3396a87262
6 changed files with 230 additions and 0 deletions

34
AdventOfCode2025/day6/.gitignore vendored Normal file
View File

@ -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

View File

@ -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.

View File

@ -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=="],
}
}

View File

@ -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)

View File

@ -0,0 +1,12 @@
{
"name": "day6",
"module": "index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
}
}

View File

@ -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
}
}