annotate 2017/day04.d @ 13:13f69301183f tip @

day 12
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 12 Dec 2017 17:21:08 -0500
parents c7b6dfd6eba6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import std.stdio;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import std.algorithm: filter, map, sort;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.array: array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 import std.string: split;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 import std.conv: to;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 auto allUnique(string[] arr) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 bool[string] counter;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 foreach(elt; arr) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 auto key = to!string(sort(elt.array));
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 counter[key] = true;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 return counter.length == arr.length;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 auto countUnique(string[][] passphrases) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 return passphrases.filter!(allUnique).array.length;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 void main(string[] args) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 auto passphrases =
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 File(args[1])
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 .byLineCopy
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 .map!(split)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 .array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 writeln(countUnique(passphrases));
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 }