solving Day02 in ts
This commit is contained in:
parent
ce25c9cc61
commit
6e1d7bc978
75
Day02/ts/solution.ts
Normal file
75
Day02/ts/solution.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
interface ColorCounts {
|
||||
red: number;
|
||||
green: number;
|
||||
blue: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a line from the game data file.
|
||||
*
|
||||
* @param {string} line - A line from the game data file.
|
||||
* @returns {ColorCounts} - An object containing the max counts of each color.
|
||||
*/
|
||||
function parseGameData(line: string): ColorCounts {
|
||||
const colorCounts: ColorCounts = { red: 0, green: 0, blue: 0 };
|
||||
|
||||
const subsets = line.split(': ')[1].split('; ');
|
||||
subsets.forEach(subset => {
|
||||
subset.split(', ').forEach(color => {
|
||||
const [count, colorName] = color.split(' ');
|
||||
colorCounts[colorName as keyof ColorCounts] = Math.max(colorCounts[colorName as keyof ColorCounts], parseInt(count));
|
||||
});
|
||||
});
|
||||
|
||||
return colorCounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the power of a cube set.
|
||||
*
|
||||
* @param {ColorCounts} colorCounts - The counts of each color.
|
||||
* @returns {number} - The power of the cube set.
|
||||
*/
|
||||
function calculatePower(colorCounts: ColorCounts): number {
|
||||
return colorCounts.red * colorCounts.green * colorCounts.blue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the game data from a file.
|
||||
*
|
||||
* @param {string} filePath - The path to the file.
|
||||
* @returns {number} - The sum of the powers of the minimum cube sets.
|
||||
*/
|
||||
function processGames(filePath: string): number {
|
||||
try {
|
||||
const lines = fs.readFileSync(filePath, 'utf-8').split('\n').filter(line => line);
|
||||
return lines.reduce((sum, line) => sum + calculatePower(parseGameData(line)), 0);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to process file: ${filePath}. Error: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Test the functionality
|
||||
function runTest(): void {
|
||||
const testFilePath = path.join(__dirname, '../test.txt');
|
||||
const testResult = processGames(testFilePath);
|
||||
console.assert(testResult === 2286, `Test failed: Expected 2286, got ${testResult}`);
|
||||
console.log(`Test Passed: Sum of powers for the minimum cube sets is ${testResult}`);
|
||||
}
|
||||
|
||||
// Main execution
|
||||
function main(): void {
|
||||
try {
|
||||
runTest();
|
||||
const inputFilePath = path.join(__dirname, '../input.txt');
|
||||
const result = processGames(inputFilePath);
|
||||
console.log(`From input.txt: Sum of powers for the minimum cube sets is ${result}`);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
13
README.md
13
README.md
@ -40,6 +40,8 @@ I aim to complete each day's puzzle on the same day, but as with any challenge,
|
||||
|
||||
## Running the Solutions
|
||||
Each solution is a standalone script.
|
||||
|
||||
## Python
|
||||
To run any of the solutions, navigate to the respective day's directory and run the script using a Python interpreter.
|
||||
For example:
|
||||
|
||||
@ -49,6 +51,9 @@ python3 part2.py test.txt # for testing
|
||||
python3 part2.py
|
||||
```
|
||||
|
||||
Make sure you have Python installed on your machine. The solutions are developed using Python 3.x.
|
||||
|
||||
## Rust
|
||||
For Rust you have to use the rust project generated in each Day.
|
||||
Make sure you have Rust and Cargo installed.
|
||||
You can either test or run the solution:
|
||||
@ -59,13 +64,19 @@ cargo test #running tests
|
||||
cargo run
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
Using solutions in JavaScript requires NodeJS installed on your system. Then:
|
||||
```bash
|
||||
cd Day01/js
|
||||
node solution.js
|
||||
```
|
||||
|
||||
Make sure you have Python installed on your machine. The solutions are developed using Python 3.x.
|
||||
## TypeScript
|
||||
Normally you can use compile the TS file to JS using the tsc compiler. But for ease in this simple scripts i suggest installing: `npm install -g typescript ts-node`. With this you can just run the TS file at once and get the output without compiling and running over and over.
|
||||
```bash
|
||||
cd Day01/ts
|
||||
ts-node solution.ts
|
||||
```
|
||||
|
||||
## Feedback and Collaboration
|
||||
I'm always open to feedback and suggestions for improving the solutions.
|
||||
|
Loading…
Reference in New Issue
Block a user