From cd2acfd143f9fae9c62b11446df1a16d25c4d5ac Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 4 Dec 2017 10:20:06 +0100 Subject: [PATCH] Implement day 4 part 2. --- 2017/README.md | 5 ++-- 2017/day-04/{solution.awk => solution1.awk} | 0 2017/day-04/solution2.awk | 33 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) rename 2017/day-04/{solution.awk => solution1.awk} (100%) create mode 100644 2017/day-04/solution2.awk diff --git a/2017/README.md b/2017/README.md index e113253..4288cfd 100644 --- a/2017/README.md +++ b/2017/README.md @@ -6,11 +6,12 @@ and using 25 different programming languages. The current plan, in no particular order: -- [ ] AWK (note, not included in shell script) +- [x] AWK - [Day 04](./day-04) (two separate scripts) - [x] Bash/shell script - [Day 02](./day-02/solution.sh) - [ ] C - [x] C++ - [Day 01](./day-01/solution.cpp) - [ ] C# +- [ ] Clojure - [Day 03](./day-03/solution.clj) (need to finish part 2) - [ ] Coffeescript - [ ] Haskell - [ ] Java @@ -27,4 +28,4 @@ The current plan, in no particular order: - [ ] Scheme - [ ] SQL -… and then I will need 5 more. But that will come in time. +… and then I will need some more. But that will come in time. diff --git a/2017/day-04/solution.awk b/2017/day-04/solution1.awk similarity index 100% rename from 2017/day-04/solution.awk rename to 2017/day-04/solution1.awk diff --git a/2017/day-04/solution2.awk b/2017/day-04/solution2.awk new file mode 100644 index 0000000..aad962a --- /dev/null +++ b/2017/day-04/solution2.awk @@ -0,0 +1,33 @@ +function word2key(word, a, i, n, result) +{ + n = split(word, a, "") + asort(a) + + for (i = 1; i <= n; i++) + result = result a[i] + + return result +} + +BEGIN { + valid=0 +} +{ + duplicates=0 + for (i=1;i<=NF;i++) { + a[word2key($i)]++ + } + for (x in a) + { + if(a[x]>1) + duplicates++ + } + for (x in a) + delete a[x] + + if(duplicates==0) + valid++ +} +END { + print valid +}