From: Neil Smith Date: Fri, 14 Jul 2017 11:37:07 +0000 (+0100) Subject: Day 3 in Dart X-Git-Url: https://git.njae.me.uk/?p=ou-summer-of-code-2017.git;a=commitdiff_plain;h=80f2bbb7a595e8f29f5bfc11006616996efba549 Day 3 in Dart --- diff --git a/03-door-codes/day3.dart b/03-door-codes/day3.dart new file mode 100644 index 0000000..607958d --- /dev/null +++ b/03-door-codes/day3.dart @@ -0,0 +1,59 @@ +int o(String char) { + return char.codeUnitAt(0) - 'a'.codeUnitAt(0) + 1; +} + +int n(int rune) { + return rune - 'a'.codeUnitAt(0) + 1; +} + + +String c(int num) { + return new String.fromCharCode((num - 1) % 26 + 'a'.codeUnitAt(0)); +} + +var letters = new RegExp(r'[a-z]'); + +String code1(String expanded_passphrase) { + String passphrase = letters.allMatches(expanded_passphrase).map((Match m) => m[0]).join(); + String c0 = passphrase[0]; + String c1 = passphrase[1]; + passphrase.substring(2).runes.forEach((int rune) { + c0 = c(o(c0) + o(c1)); + c1 = c(o(c1) + n(rune)); + }); + return c0 + c1; +} + +String code2(String expanded_passphrase) { + String passphrase = letters.allMatches(expanded_passphrase).map((Match m) => m[0]).join(); + int alpha = 5; + int beta = 11; + String c0 = "r"; + String c1 = "i"; + passphrase.runes.forEach((int rune) { + c0 = c(o(c0) + o(c1) * alpha); + c1 = c(o(c1) + n(rune) * beta); + }); + return c0 + c1; +} + +void main() { +// "abcdefghijklmnopqrstuvwxyz".runes.forEach((int rune) { +// print('$rune : ${new String.fromCharCode(rune)} :: ${n(rune)} ${c(n(rune) + 28)}'); +// }); + +// for (var match in letters.allMatches('The quick ! broWn')) { +// print(match.group(0)); // 15, then 20 +// }; + +// print(letters.allMatches('The quick ! broWn').map((Match m) => m[0]).join()); + + String phr = "the traveller in the grey riding-coat, who called himself mr. melville, was " + "contemplating the malice of which the gods are capable."; + +// print(code1('the cat.')); + print(code1(phr)); + +// print(code2('the cat.')); + print(code2(phr)); +}