changeset 4:c7b6dfd6eba6

day 4
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Thu, 07 Dec 2017 11:44:52 -0500
parents b5533de6ff5b
children 20d440f0793e
files 2017/day04.d
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/2017/day04.d
@@ -0,0 +1,27 @@
+import std.stdio;
+import std.algorithm: filter, map, sort;
+import std.array: array;
+import std.string: split;
+import std.conv: to;
+
+auto allUnique(string[] arr) {
+  bool[string] counter;
+  foreach(elt; arr) {
+    auto key = to!string(sort(elt.array));
+    counter[key] = true;
+  }
+  return counter.length == arr.length;
+}
+
+auto countUnique(string[][] passphrases) {
+  return passphrases.filter!(allUnique).array.length;
+}
+
+void main(string[] args) {
+  auto passphrases =
+    File(args[1])
+    .byLineCopy
+    .map!(split)
+    .array;
+  writeln(countUnique(passphrases));
+}