Skip to content

Instantly share code, notes, and snippets.

@FlafyDev
Created June 22, 2022 15:35
Show Gist options
  • Save FlafyDev/97d30f96dbfeac77331c6d82b4d07016 to your computer and use it in GitHub Desktop.
Save FlafyDev/97d30f96dbfeac77331c6d82b4d07016 to your computer and use it in GitHub Desktop.
Solution to my Codingame challenge in TypeScript
interface Change {
line: number,
column: number,
str: string,
}
class InsertManager {
changes: Change[] = []
constructor(public str: string) { }
insert(line: number, column: number, str: string) {
this.changes.push({
line, column, str
});
}
getString() {
this.changes = this.changes.sort((firstChange, secondChange) => {
return secondChange.line - firstChange.line || secondChange.column - firstChange.column;
})
for (const change of this.changes) {
this.str = InsertManager.insertString(this.str, change.line, change.column, change.str);
}
return this.str;
}
static insertString(str: string, line: number, column: number, subtext: string) {
const lines = str.split("\n");
lines[line] = lines[line].slice(0, column) + subtext + lines[line].slice(column);
return lines.join("\n");
}
}
const s: string = readline();
const changeCount: number = parseInt(readline());
const rawChanges: string[] = []
for (let i = 0; i < changeCount; i++) {
const rawChange: string = readline();
rawChanges.push(rawChange)
}
const insertManager = new InsertManager(s.replace(/\\n/g, '\n'));
rawChanges.forEach(rawChange => {
const splitChange = rawChange.split("|");
insertManager.insert(parseInt(splitChange[0]), parseInt(splitChange[1]), splitChange[2].replace(/\\n/g, '\n'));
})
console.log(insertManager.getString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment