diff options
| author | Abigail <abigail@abigail.be> | 2021-01-09 02:29:29 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-09 19:17:03 +0100 |
| commit | baead4f7da7e805466f6961bf24531f0190e7fbe (patch) | |
| tree | 83e5fa99ec771c111c9031036ea5a124404c050c /challenge-094 | |
| parent | a2cce72927966dfa716005651cb051ac91667075 (diff) | |
| download | perlweeklychallenge-club-baead4f7da7e805466f6961bf24531f0190e7fbe.tar.gz perlweeklychallenge-club-baead4f7da7e805466f6961bf24531f0190e7fbe.tar.bz2 perlweeklychallenge-club-baead4f7da7e805466f6961bf24531f0190e7fbe.zip | |
Node solution for week 94/part 1.
Diffstat (limited to 'challenge-094')
| -rw-r--r-- | challenge-094/abigail/node/ch-1.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-094/abigail/node/ch-1.js b/challenge-094/abigail/node/ch-1.js new file mode 100644 index 0000000000..d790d52c0a --- /dev/null +++ b/challenge-094/abigail/node/ch-1.js @@ -0,0 +1,44 @@ +// +// Call as "node ch-1.js < ../t/input-1-X", for suitable X. +// + require ("fs") +. readFileSync (0) // Read all. +. toString () // Turn it into a string. +. split ("\n") // Split on newlines. +. filter (_ => _ . length) // Filter out empty lines. + // + // Split each line on ', ', and remove the double quotes. + // We now have a list of words. + // +. map (_ => _ . split (/,\s*/) + . map (_ => _ . replace (/"/g, '')) + ) + // + // Iterate over the list of words, find the canonical representation + // of each word (characters sorted), and store the words in a hash, + // keyed by their canonical representation. + // +. map (_ => _ . reduce ((hash, word) => { + let key = word . split ("") + . sort () + . join (""); + hash [key] = hash [key] || []; + hash [key] . push (word); + return (hash); + }, {}) + ) + // + // Print the results: sorted by key, the words in the order + // of the input. Each word will be surrounded by double quotes + // and separated by commas. + // +. map (hash => Object . keys (hash) + . sort () + . forEach (_ => { + console . log ( + hash [_] . map (_ => '"' + _ + '"') + . join (", ") + ) + }) + ) +; |
