diff --git a/Day15/ts/solution.ts b/Day15/ts/solution.ts new file mode 100644 index 0000000..1defff9 --- /dev/null +++ b/Day15/ts/solution.ts @@ -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 = 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(); diff --git a/README.md b/README.md index a0d79d3..84b7a90 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@
![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) |