Skip to content

Instantly share code, notes, and snippets.

@OoDeLally
Last active April 5, 2020 06:46
Show Gist options
  • Save OoDeLally/97bd0094c9c059295ce040d727ee720e to your computer and use it in GitHub Desktop.
Save OoDeLally/97bd0094c9c059295ce040d727ee720e to your computer and use it in GitHub Desktop.
const containsDuplicate = (array) => {
const set = new Set();
for (const item of array) {
if (set.has(item)) {
return true;
}
set.add(item);
}
return false;
};
const invertMatrix = (matrix) => {
const colCount = matrix[0].length;
const invertedMatrix = [];
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex++) {
const row = [];
for (let colIndex = 0; colIndex < colCount; colIndex++) {
row.push(matrix[colIndex][rowIndex]);
}
invertedMatrix.push(row);
}
return invertedMatrix;
}
const runTest = ({ headerLine, size, lines }) => {
const matrix = lines.map(line => line.split(' ').map(str => +str));
const trace = matrix.reduce((sum, row, rowIndex) => sum + row[rowIndex], 0);
const countRepeatingRows = m => m.reduce(
(sum, row, rowIndex) =>
sum + (containsDuplicate(row) ? 1 : 0),
0
);
const repeatingRows = countRepeatingRows(matrix);
const invertedMatrix = invertMatrix(matrix);
const repeatingColumns = countRepeatingRows(invertedMatrix);
return `${trace} ${repeatingRows} ${repeatingColumns}`;
const runTest = ({ lines }) => {
const list = lines[0].split('').map(str => +str);
// console.log('list', list)
let depth = 0;
let out = '';
const goUp = (step) => {
if (step <= 0) { return }
depth -= step;
for (let i = 0; i < step; i++) {
out += ')';
}
};
const goDown = (step) => {
if (step <= 0) { return }
depth += step;
for (let i = 0; i < step; i++) {
out += '(';
}
};
for (let i = 0; i < list.length; i++) {
const nextNumber = list[i];
goDown(nextNumber - depth);
goUp(depth - nextNumber);
out += String(nextNumber);
}
goUp(depth);
return out;
};
const runTest = ({ headerLine, size, lines }) => {
const tasks = lines.map((line, lineIndex) => {
const [ start, end ] = line.split(' ').map(str => +str);
return { start, end, taskIndex: lineIndex };
});
// console.log('tasks', tasks)
const workers = [
{ name: 'C', availableTime: 0 },
{ name: 'J', availableTime: 0 }
];
let schedule = [];
tasks
.sort((t1, t2) => t1.start - t2.start)
.forEach(({ start, end, taskIndex }) => {
// console.log('Task', start, end)
for (const worker of workers) {
if (worker.availableTime <= start) {
worker.availableTime = end;
schedule[taskIndex] = worker.name;
return;
}
}
throw new ImpossibleError();
});
return schedule.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment