solve Day04 in JS
This commit is contained in:
parent
441ffbcf86
commit
ff595e0438
@ -0,0 +1,69 @@
|
||||
const fs = require('fs');
|
||||
|
||||
// Parses a line of card data and returns the card number, winning numbers, and own numbers
|
||||
function parseCardData(line) {
|
||||
const [cardInfo, numberParts] = line.split(':');
|
||||
const [winningPart, ownPart] = numberParts.split('|');
|
||||
const cardNumber = parseInt(cardInfo.match(/\d+/)[0], 10);
|
||||
const winningNumbers = new Set(winningPart.trim().split(/\s+/).map(Number));
|
||||
const ownNumbers = ownPart.trim().split(/\s+/).map(Number);
|
||||
return { cardNumber, winningNumbers, ownNumbers };
|
||||
}
|
||||
|
||||
// Calculates the number of matches for a card
|
||||
function calculateMatches(winningNumbers, ownNumbers) {
|
||||
return ownNumbers.reduce((count, number) => count + winningNumbers.has(number), 0);
|
||||
}
|
||||
|
||||
// Processes the cards and returns the total number of cards including copies
|
||||
function processCards(cards) {
|
||||
let totalCards = cards.length;
|
||||
const queue = cards.slice(); // Clone the original array to avoid modifying it
|
||||
|
||||
while (queue.length > 0) {
|
||||
const currentCard = queue.shift();
|
||||
const matches = calculateMatches(currentCard.winningNumbers, currentCard.ownNumbers);
|
||||
for (let i = 1; i <= matches; i++) {
|
||||
if (currentCard.cardNumber + i < cards.length) {
|
||||
totalCards++;
|
||||
queue.push(cards[currentCard.cardNumber + i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return totalCards;
|
||||
}
|
||||
|
||||
// Reads the file and processes the cards
|
||||
function processFile(filePath) {
|
||||
try {
|
||||
const data = fs.readFileSync(filePath, 'utf-8');
|
||||
const lines = data.trim().split('\n');
|
||||
const cards = lines.map(parseCardData);
|
||||
return processCards(cards);
|
||||
} catch (error) {
|
||||
console.error(`Error processing file ${filePath}: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Test function
|
||||
function test() {
|
||||
const expectedResult = 30;
|
||||
const result = processFile('../test.txt');
|
||||
console.assert(result === expectedResult, `Test failed: Expected ${expectedResult}, got ${result}`);
|
||||
console.log(`Test passed: ${result} cards`);
|
||||
}
|
||||
|
||||
// Main function
|
||||
function main() {
|
||||
try {
|
||||
test();
|
||||
const totalCards = processFile('../input.txt');
|
||||
console.log(`Total cards from input.txt: ${totalCards}`);
|
||||
} catch (error) {
|
||||
console.error(`An error occurred: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
44
README.md
44
README.md
@ -30,28 +30,28 @@ I aim to complete each day's puzzle on the same day, but as with any challenge,
|
||||
|-----|----------|----------|-------------|
|
||||
| 01 | ✅ | ✅ | [Day01 README](/Day01/README.md) |
|
||||
| 02 | ✅ | ✅ | [Day02 README](/Day02/README.md) |
|
||||
| 03 | ✅ | ✅ | [Day25 README](/Day03/README.md) |
|
||||
| 04 | ❓ | ❓ | [Day25 README](/Day04/README.md) |
|
||||
| 05 | ❓ | ❓ | [Day25 README](/Day05/README.md) |
|
||||
| 06 | ❓ | ❓ | [Day25 README](/Day06/README.md) |
|
||||
| 07 | ❓ | ❓ | [Day25 README](/Day07/README.md) |
|
||||
| 08 | ❓ | ❓ | [Day25 README](/Day08/README.md) |
|
||||
| 09 | ❓ | ❓ | [Day25 README](/Day09/README.md) |
|
||||
| 10 | ❓ | ❓ | [Day25 README](/Day10/README.md) |
|
||||
| 11 | ❓ | ❓ | [Day25 README](/Day11/README.md) |
|
||||
| 12 | ❓ | ❓ | [Day25 README](/Day12/README.md) |
|
||||
| 13 | ❓ | ❓ | [Day25 README](/Day13/README.md) |
|
||||
| 14 | ❓ | ❓ | [Day25 README](/Day14/README.md) |
|
||||
| 15 | ❓ | ❓ | [Day25 README](/Day15/README.md) |
|
||||
| 16 | ❓ | ❓ | [Day25 README](/Day16/README.md) |
|
||||
| 17 | ❓ | ❓ | [Day25 README](/Day17/README.md) |
|
||||
| 18 | ❓ | ❓ | [Day25 README](/Day18/README.md) |
|
||||
| 19 | ❓ | ❓ | [Day25 README](/Day19/README.md) |
|
||||
| 20 | ❓ | ❓ | [Day25 README](/Day20/README.md) |
|
||||
| 21 | ❓ | ❓ | [Day25 README](/Day21/README.md) |
|
||||
| 22 | ❓ | ❓ | [Day25 README](/Day22/README.md) |
|
||||
| 23 | ❓ | ❓ | [Day25 README](/Day23/README.md) |
|
||||
| 24 | ❓ | ❓ | [Day25 README](/Day24/README.md) |
|
||||
| 03 | ✅ | ✅ | [Day03 README](/Day03/README.md) |
|
||||
| 04 | ✅ | ✅ | [Day04 README](/Day04/README.md) |
|
||||
| 05 | ❓ | ❓ | [Day05 README](/Day05/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) |
|
||||
| 10 | ❓ | ❓ | [Day10 README](/Day10/README.md) |
|
||||
| 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) |
|
||||
| 16 | ❓ | ❓ | [Day16 README](/Day16/README.md) |
|
||||
| 17 | ❓ | ❓ | [Day17 README](/Day17/README.md) |
|
||||
| 18 | ❓ | ❓ | [Day18 README](/Day18/README.md) |
|
||||
| 19 | ❓ | ❓ | [Day19 README](/Day19/README.md) |
|
||||
| 20 | ❓ | ❓ | [Day20 README](/Day20/README.md) |
|
||||
| 21 | ❓ | ❓ | [Day21 README](/Day21/README.md) |
|
||||
| 22 | ❓ | ❓ | [Day22 README](/Day22/README.md) |
|
||||
| 23 | ❓ | ❓ | [Day23 README](/Day23/README.md) |
|
||||
| 24 | ❓ | ❓ | [Day24 README](/Day24/README.md) |
|
||||
| 25 | ❓ | ❓ | [Day25 README](/Day25/README.md) |
|
||||
|
||||
- ✅ Completed
|
||||
|
Loading…
Reference in New Issue
Block a user