Skip to content

Commit

Permalink
Solve p22 in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 8, 2024
1 parent 7a8715a commit 4d9b382
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Olivia's Project Euler Solutions
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
+------------+----------------------------+--------+-------------------+
| Lua | PUC-Rio Lua 5+ [#]_ | 19 | |Luai| |br| |
| Lua | PUC-Rio Lua 5+ [#]_ | 20 | |Luai| |br| |
| | | | |Lu-Cov| |br| |
| | | | |LuaCheck| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`021`| | | | | |:js-d:`0021`| |:py-d:`0021`|:rs-i:`0021`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`022`|:c-d:`0022`|:cp-d:`0022`|:cs-d:`0022`|:fr-d:`0022`|:ja-d:`0022`|:js-d:`0022`| |:py-d:`0022`|:rs-d:`0022`|
|:prob:`022`|:c-d:`0022`|:cp-d:`0022`|:cs-d:`0022`|:fr-d:`0022`|:ja-d:`0022`|:js-d:`0022`|:lu-d:`0022`|:py-d:`0022`|:rs-d:`0022`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`023`| | | | | |:js-d:`0023`| |:py-d:`0023`|:rs-d:`0023`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
23 changes: 23 additions & 0 deletions docs/src/lua/p0022.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Lua Implementation of Problem 22
================================

View source code :source:`lua/src/p0022.lua`

Includes
--------

- `utils.lua <./lib/utils.html>`__

Solution
--------

.. lua:function:: solution()
:return: The solution to problem 22
:rtype: number

.. literalinclude:: ../../../lua/src/p0022.lua
:language: Lua
:linenos:

.. tags:: word-problem, sorting, file-io
1 change: 1 addition & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Problems Solved
- ☒ `14 <./src/p0014.lua>`__
- ☒ `15 <./src/p0015.lua>`__
- ☒ `17 <./src/p0017.lua>`__
- ☒ `22 <./src/p0022.lua>`__
- ☒ `28 <./src/p0028.lua>`__
- ☒ `34 <./src/p0034.lua>`__
- ☒ `76 <./src/p0076.lua>`__
Expand Down
33 changes: 33 additions & 0 deletions lua/src/p0022.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- 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?

local get_data_file = loadlib("utils").get_data_file

return {
solution = function()
local answer = 0
local array = {get_data_file("p0022_names.txt"):gsub('"', ''):gmatch("([^,]+)")}
table.sort(array)
for idx, name in ipairs(array) do
local score = 0
for i = 1,#name do
score = score + string.byte(name, i) - string.byte('A', 1) + 1
end
answer = answer + score * idx
end
return answer
end
}
1 change: 1 addition & 0 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ end

local more_problems = {
["p0017.lua"] = {get_answer(17), false},
["p0022.lua"] = {get_answer(22), false},
["p0028.lua"] = {get_answer(28), false},
["p0034.lua"] = {get_answer(34), false},
["p0076.lua"] = {get_answer(76), true},
Expand Down

0 comments on commit 4d9b382

Please sign in to comment.