solve Day15 in TS

This commit is contained in:
WieErWill 2023-12-15 20:14:58 +01:00
parent 4822cc6d4c
commit a1fefdcd14
2 changed files with 85 additions and 5 deletions

80
Day15/ts/solution.ts Normal file
View File

@ -0,0 +1,80 @@
import * as fs from 'fs';
import * as path from 'path';
function hashLabel(label: string): number {
// Compute hash value for a given label
return [...label].reduce((value, char) => (value + char.charCodeAt(0)) * 17 % 256, 0);
}
function processSteps(steps: string[]): number {
const boxes: string[][] = Array.from({ length: 256 }, () => []);
const focalLengths: Map<string, number> = new Map();
steps.forEach(step => {
const match = step.match(/([a-z]+)([=-])(\d)?/);
if (!match) return;
const [, label, operation, focalLength] = match;
const hashed = hashLabel(label);
const destination = boxes[hashed];
console.log(`Processing step: ${label}${operation}${focalLength}, Box: ${hashed}`);
if (operation === "=") {
if (!destination.includes(label)) {
destination.push(label);
}
focalLengths.set(label, parseInt(focalLength));
} else {
const index = destination.indexOf(label);
if (index !== -1) {
destination.splice(index, 1);
}
focalLengths.delete(label);
}
});
return boxes.reduce((totalPower, box, boxNumber) =>
totalPower + box.reduce((boxPower, label, i) =>
boxPower + (boxNumber + 1) * (i + 1) * (focalLengths.get(label) || 0), 0), 0);
}
function readFileContent(filePath: string): string {
try {
return fs.readFileSync(filePath, 'utf-8');
} catch (error) {
console.error(`Error reading file at ${filePath}:`, (error as Error).message);
process.exit(1);
}
}
function testAlgorithm(): void {
const testContent = readFileContent(path.join(__dirname, '../test.txt'));
const testSteps = testContent.replace(/\n/g, '').split(',');
const testResult = processSteps(testSteps);
console.log(`Test Result: ${testResult}`);
if (testResult !== 145) {
console.error(`Test failed. Expected result is 145, got ${testResult}.`);
process.exit(1);
} else {
console.log('Test passed.');
}
}
function main(): void {
try {
testAlgorithm();
const inputContent = readFileContent(path.join(__dirname, '../input.txt'));
const inputSteps = inputContent.replace(/\n/g, '').split(',');
const finalResult = processSteps(inputSteps);
console.log(`Final Result: ${finalResult}`);
} catch (error) {
console.error(`An error occurred in main: ${(error as Error).message}`);
process.exit(1);
}
}
main();

View File

@ -3,8 +3,8 @@
<div><img src="title_image.png" style="margin: 0 auto;" height="300" width="300" ></div>
![Build Status](https://github.com/wieerwill/advent_of_code_2023/actions/workflows/lint.yml/badge.svg)
![Completed](https://img.shields.io/badge/days%20completed-12-red)
![Stars](https://img.shields.io/badge/stars%20⭐-24-yellow)
![Completed](https://img.shields.io/badge/days%20completed-15-red)
![Stars](https://img.shields.io/badge/stars%20⭐-27-yellow)
![License](https://img.shields.io/github/license/wieerwill/advent_of_code_2023.svg)
Welcome to my repository where I share my solutions for the [Advent of Code 2023](https://adventofcode.com/2023). Advent of Code is an annual online event where participants solve a series of programming puzzles released daily from December 1st until December 25th. Each puzzle is a fun and unique challenge designed to test problem-solving skills.
@ -39,7 +39,7 @@ I aim to complete each day's puzzle on the same day, but as with any challenge,
| 03 | ✅ | ✅ | [Day03 README](/Day03/README.md) |
| 04 | ✅ | ✅ | [Day04 README](/Day04/README.md) |
| 05 | ✅ | ✅ | [Day05 README](/Day05/README.md) |
| 06 | ✅ | ✅ | [Day06 README](/Day06/README.md) |
| 06 | ✅ | ✅ | [Day06 README](/Day06/README.md) |
| 07 | ✅ | ✅ | [Day07 README](/Day07/README.md) |
| 08 | ✅ | ✅ | [Day08 README](/Day08/README.md) |
| 09 | ✅ | ✅ | [Day09 README](/Day09/README.md) |
@ -47,8 +47,8 @@ I aim to complete each day's puzzle on the same day, but as with any challenge,
| 11 | ✅ | ✅ | [Day11 README](/Day11/README.md) |
| 12 | ✅ | ✅ | [Day12 README](/Day12/README.md) |
| 13 | ❓ | ❓ | [Day13 README](/Day13/README.md) |
| 14 | | ❓ | [Day14 README](/Day14/README.md) |
| 15 | ❓ | ❓ | [Day15 README](/Day15/README.md) |
| 14 | | ❓ | [Day14 README](/Day14/README.md) |
| 15 | ✅ | ✅ | [Day15 README](/Day15/README.md) |
| 16 | ❓ | ❓ | [Day16 README](/Day16/README.md) |
| 17 | ❓ | ❓ | [Day17 README](/Day17/README.md) |
| 18 | ❓ | ❓ | [Day18 README](/Day18/README.md) |