-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
38 lines (31 loc) · 1.13 KB
/
io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// I/O functionality needed to read and write to input and outfile respectively
const fs = require("fs");
const processInput = (filePath, hands, decks) => {
let rawInput;
try {
rawInput = fs.readFileSync(filePath, "utf8");
} catch (error) {
console.log("Error during reading input");
}
// Split the raw input into separate lines trimmed at the end
let inputLines = rawInput.split("\n").map(value => value.trimEnd());
// Initialize hands array by taking the first 5 elements (14 characters = 5*card symbol + 4*spaces between)
inputLines.map(value => hands.push(value.slice(0, 14).split(" ")));
// Initialize decks array by taking the second 5 elements (14 characters = 5*card symbol + 4*spaces between)
inputLines.map(value => decks.push(value.slice(15, 29).split(" ")));
};
const processOutput = (filePath, hands, decks, bestHands) => {
fs.truncateSync(filePath);
for (let i = 0; i < hands.length; i++) {
fs.appendFileSync(
filePath,
`Hand: ${hands[i].join(" ")} Deck: ${decks[i].join(" ")} Best hand: ${
bestHands[i]
}\n`
);
}
};
module.exports = {
processInput,
processOutput
};