Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

punched cards javascript solution uploaded - Apprentice 2022d #418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added solutions/punched-cards/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions solutions/punched-cards/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
3 4
2 2
2 3
47 changes: 47 additions & 0 deletions solutions/punched-cards/punched_cards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//Requirements to read the input file
const fs = require('fs');
const inputFile = fs.readFileSync(0, 'utf8').trim().split('\n');

// Function definition
const punchedCards = (input) => {
// Declaring variables
let testCases = input[0],
cols = 0,
rows = 0,
caseNum = 1;
// Loop for each case
while (caseNum <= testCases) {
// Saving rows and columns numbers and create the matrix
rows = input[caseNum].split(' ')[0] * 2;
cols = input[caseNum].split(' ')[1] * 2;
let card = [];
// Loop to circle each row
for (let i = 0; i <= rows; i++) {
card.push(['']);
// Second loop to circle each column
if (i % 2 == 0) {
for (let j = 0; j <= cols; j++) {
card[i][j] = ((j % 2 == 0)? '+' : '-');
}
}
else {
for (let j = 0; j <= cols; j++) {
card[i][j] = ((j % 2 == 0)? '|' : '.');
}
}
}
// Replacing the top-left cell
card[0][0] = '.';
card[0][1] = '.';
card[1][0] = '.';
card[1][1] = '.';
// Printing the result
console.log(`Case #${caseNum}:`);
for (let i = 0; i <= rows; i++) {
console.log(`${card[i].join('')}\n`);
}
caseNum++;
}
};
// Function call
punchedCards(inputFile);