diff --git a/README.rst b/README.rst index 3c7f968e..7f6038ff 100644 --- a/README.rst +++ b/README.rst @@ -46,7 +46,7 @@ Olivia's Project Euler Solutions +------------+--------------------------+--------+-------------------+ | Language | Version | Solved | Status | +============+==========================+========+===================+ -| C | C99+ in: |clang|, |br| | 21 | |Ci| |br| | +| C | C99+ in: |clang|, |br| | 20 | |Ci| |br| | | | |gcc|, |pcc|, |tcc| |br| | | |C-Cov| |br| | | | C11+ in: |msvc| [1]_ | | |CodeQL| | +------------+--------------------------+--------+-------------------+ @@ -58,7 +58,7 @@ Olivia's Project Euler Solutions | | | | |Cs-Cov| |br| | | | | | |CodeQL| | +------------+--------------------------+--------+-------------------+ -| JavaScript | Node 12+ |br| | 13 | |JavaScript| |br| | +| JavaScript | Node 12+ |br| | 14 | |JavaScript| |br| | | | Bun 1.0+ |br| | | |Js-Cov| |br| | | | Firefox [2]_ |br| | | |CodeQL| |br| | | | Chrome [2]_ | | |ESLint| | diff --git a/docs/index.rst b/docs/index.rst index 01cd421d..f4bf92d2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -169,7 +169,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`21` | | | | |:py-d:`0021`| | +-----------+------------+------------+------------+------------+------------+------------+ -|:prob:`22` |:c-i:`0022` | | | |:py-d:`0022`|:rs-d:`0022`| +|:prob:`22` |:c-i:`0022` | | |:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`| +-----------+------------+------------+------------+------------+------------+------------+ |:prob:`23` | | | | |:py-d:`0023`| | +-----------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/javascript/p0022.rst b/docs/javascript/p0022.rst new file mode 100644 index 00000000..83592222 --- /dev/null +++ b/docs/javascript/p0022.rst @@ -0,0 +1,18 @@ +JavaScript Implementation of Problem 22 +======================================= + +View source code :source:`javascript/src/p0022.js` + +Includes +-------- + +- `utils <./utils.html>`_ + +Problem Solution +---------------- + +.. js:autofunction:: p0022 + +.. literalinclude:: ../../javascript/src/p0022.js + :language: javascript + :linenos: diff --git a/javascript/euler.test.js b/javascript/euler.test.js index d48ab8b7..847cc4eb 100644 --- a/javascript/euler.test.js +++ b/javascript/euler.test.js @@ -29,6 +29,7 @@ const answers = { 14: [require('./src/p0014.js'), false, 837799], 15: [require('./src/p0015.js'), false, 137846528820], 17: [require('./src/p0017.js'), false, 21124], + 22: [require('./src/p0022.js'), false, 871198282], 34: [require('./src/p0034.js'), false, 40730], 76: [require('./src/p0076.js'), true, 190569291], 836: [require('./src/p0836.js'), false, 'aprilfoolsjoke'], diff --git a/javascript/src/p0022.js b/javascript/src/p0022.js new file mode 100644 index 00000000..645ac3a4 --- /dev/null +++ b/javascript/src/p0022.js @@ -0,0 +1,34 @@ +/** + * Project Euler Problem 22 + * + * + * Problem: + * + * Using names.txt (right click and 'Save Link/Target As...'), a 46K text file + * containing over five-thousand first names, begin by sorting it into + * alphabetical order. Then working out the alphabetical value for each name, + * multiply this value by its alphabetical position in the list to obtain a name + * score. + * + * For example, when the list is sorted into alphabetical order, COLIN, which is + * worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would + * obtain a score of 938 × 53 = 49714. + * + * What is the total of all the name scores in the file? + * + * @return {number} + */ +exports.p0022 = function() { + const contents = require('./lib/utils.js').get_data_file('p0022_names.txt'); + let names = contents.replaceAll('"', '').split(','); + names.sort(); + let sum = 0; + for (let i = 0; i < names.length; i += 1) { + let quantity = 0; + for (let j = 0; j < names[i].length; j += 1) { + quantity += names[i].charCodeAt(j) & 0x3F; + } + sum += quantity * (i + 1); + } + return sum; +};