Skip to content

Instantly share code, notes, and snippets.

@FlafyDev
Created June 22, 2022 14:33
Show Gist options
  • Save FlafyDev/45ebe0f58e19f7c33b77311cfee7255b to your computer and use it in GitHub Desktop.
Save FlafyDev/45ebe0f58e19f7c33b77311cfee7255b to your computer and use it in GitHub Desktop.
Solution to my Codingame challenge in Dart
import 'dart:io';
String readLineSync() {
String? s = stdin.readLineSync();
return s == null ? '' : s;
}
class Change with Comparable<Change> {
int line;
int column;
String addition;
Change({
required this.line,
required this.column,
required String addition,
}) : addition = addition.replaceAll("\\n", "\n");
@override
int compareTo(Change other) {
return line == other.line ? column - other.column : line - other.line;
}
String applyToString(String string) {
List<String> lines = string.split("\n");
lines[line] = lines[line].substring(0, column) + addition + lines[line].substring(column);
return lines.join("\n");
}
@override
String toString() {
return "{line: $line, column: $column, addition: $addition}";
}
}
void main() {
String string = readLineSync().replaceAll("\\n", "\n");;
int changeCount = int.parse(readLineSync());
List<Change> changes = [];
for (int i = 0; i < changeCount; i++) {
String rawChange = readLineSync();
final split = rawChange.split("|");
changes.add(Change(
line: int.parse(split[0]),
column: int.parse(split[1]),
addition: split[2],
));
}
changes.sort((a, b) => a.compareTo(b));
changes = changes.reversed.toList();
changes.forEach((change) {
string = change.applyToString(string);
});
print(string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment