view 2017/day04/app.d @ 35:1d99d733cf13 default tip @

day08: replace static foreach with workaround
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 16 Jan 2018 11:28:55 -0500
parents bc652fa0a645
children
line wrap: on
line source

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));
}