From 4d9b3828acef1ebba82010dbecb0cd76e8582e26 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Tue, 8 Oct 2024 00:36:46 -0500 Subject: [PATCH] Solve p22 in lua --- README.rst | 2 +- docs/index.rst | 2 +- docs/src/lua/p0022.rst | 23 +++++++++++++++++++++++ lua/README.rst | 1 + lua/src/p0022.lua | 33 +++++++++++++++++++++++++++++++++ lua/test.lua | 1 + 6 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 docs/src/lua/p0022.rst create mode 100644 lua/src/p0022.lua diff --git a/README.rst b/README.rst index c331cc98..27ec1ae2 100644 --- a/README.rst +++ b/README.rst @@ -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| | +------------+----------------------------+--------+-------------------+ diff --git a/docs/index.rst b/docs/index.rst index 6dfcd501..d204378d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/lua/p0022.rst b/docs/src/lua/p0022.rst new file mode 100644 index 00000000..fbf18ea7 --- /dev/null +++ b/docs/src/lua/p0022.rst @@ -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 diff --git a/lua/README.rst b/lua/README.rst index 5eaedcf8..1dee450b 100644 --- a/lua/README.rst +++ b/lua/README.rst @@ -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>`__ diff --git a/lua/src/p0022.lua b/lua/src/p0022.lua new file mode 100644 index 00000000..6240e60a --- /dev/null +++ b/lua/src/p0022.lua @@ -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 +} diff --git a/lua/test.lua b/lua/test.lua index 5d799953..4105cfb9 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -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},