Skip to content

Commit

Permalink
solve p22 in js
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 4, 2024
1 parent 59ba724 commit 66d9b4d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
+------------+--------------------------+--------+-------------------+
Expand All @@ -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| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down
18 changes: 18 additions & 0 deletions docs/javascript/p0022.rst
Original file line number Diff line number Diff line change
@@ -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:
1 change: 1 addition & 0 deletions javascript/euler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
34 changes: 34 additions & 0 deletions javascript/src/p0022.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 66d9b4d

Please sign in to comment.