Skip to content

Commit

Permalink
Add initial lua solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 5, 2024
1 parent 6dd7fb6 commit 9c636c4
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 175 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/lua.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Lua

on:
push:
paths:
- 'lua/**'
- '!lua/README.rst'
schedule:
- cron: "0 0 1 * *"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
test:
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest
- macos-13
version:
- 5.4
- 5.3
- 5.2
- 5.1
- 4.0
- 3.2
- 3.1
- 3.0
- 2.5
- 2.4
- 2.2
- 2.1
- 1.1
- 1.0

runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- uses: leafo/gh-actions-lua@v10
with:
luaVersion: ${{ matrix.version }}

- run: make lu_test
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ help: LICENSE
@echo "The following jobs are available under the js_ prefix"
@$(MAKE) js_help $(MFLAGS) --no-print-directory
@echo
@echo "The following jobs are available under the lu_ prefix"
@$(MAKE) lu_help $(MFLAGS) --no-print-directory
@echo
@echo "The following jobs are available under the py_ prefix"
@$(MAKE) py_help $(MFLAGS) --no-print-directory
@echo
Expand Down Expand Up @@ -69,6 +72,10 @@ ja_%: LICENSE
js_%: LICENSE
@$(MAKE) -C javascript $* $(MFLAGS) --no-print-directory

.PHONY: lu_%
lu_%: LICENSE
@$(MAKE) -C lua $* $(MFLAGS) --no-print-directory

.PHONY: py_%
py_%: LICENSE
@$(MAKE) -C python $* $(MFLAGS) --no-print-directory
Expand All @@ -78,4 +85,4 @@ rs_%: LICENSE
@$(MAKE) -C rust $* $(MFLAGS) --no-print-directory

%:
@$(MAKE) c_$* cp_$* cs_$* ja_$* js_$* py_$* rs_$* $(MFLAGS) --no-print-directory
@$(MAKE) c_$* cp_$* cs_$* ja_$* js_$* lu_$* py_$* rs_$* $(MFLAGS) --no-print-directory
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Olivia's Project Euler Solutions
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
+------------+----------------------------+--------+-------------------+
| Lua | (In Progress) | 2 | (In Progress) |
+------------+----------------------------+--------+-------------------+
| Python | CPython 3.6+ |br| | 80 | |Python| |br| |
| | Pypy 3.6+ |br| | | |Py-Cov| |br| |
| | GraalPy 23.1+ |br| | | |CodeQL| |br| |
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
# 'sphinx-computron',
# 'sphinx-compendia',
# 'sphinx-navtree',
'sphinxcontrib-luadomain',
]

try:
Expand Down Expand Up @@ -121,7 +122,7 @@
# ('ju', 'julia'),
# ('kt', 'kotlin'),
# ('ls', 'lisp'), # sphinxcontrib-cldomain
# ('lu', 'lua'), # sphinx-lua or sphinxcontrib-luadomain
('lu', 'lua'), # sphinx-lua or sphinxcontrib-luadomain
# ('ma', 'matlab'), # sphinxcontrib-matlabdomain
# ('mk', 'makefile'), # hacked together with sphinxcontrib-domaintools
# ('oc', 'ocaml'), # sphinxcontrib-ocaml
Expand Down
346 changes: 173 additions & 173 deletions docs/index.rst

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ sphinx-notfound-page
javasphinx-workaround
sphinx-tags
sphinx-mathjax-offline
sphinxcontrib-luadomain
24 changes: 24 additions & 0 deletions docs/src/lua.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Euler Lua Implementation
========================

.. include:: ../../lua/README.rst
:start-line: 2
:end-before: Problems Solved

.. Library Code
.. ------------
.. .. toctree::
.. :glob:
.. :maxdepth: 1
.. lua/lib/*
Problems Solved
---------------

.. toctree::
:glob:
:maxdepth: 1

lua/p[0-9][0-9][0-9][0-9]
17 changes: 17 additions & 0 deletions docs/src/lua/p0001.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Lua Implementation of Problem 1
===============================

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

Solution
--------

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

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

.. tags:: divisibility
17 changes: 17 additions & 0 deletions docs/src/lua/p0002.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Lua Implementation of Problem 2
===============================

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

Solution
--------

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

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

.. tags:: divisibility, fibonacci-number
18 changes: 18 additions & 0 deletions lua/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BLUE=\033[0;34m
NC=\033[0m # No Color

.PHONY: help
help:
@echo " $(BLUE)test$(NC) Run through all Lua tests in sequence."
@echo " $(BLUE)test_auto$(NC) Run through all Lua tests (parallel execution not implemented)."
@echo " $(BLUE)clean$(NC) Clean up any stray files."

.PHONY: test
test: ../LICENSE
@lua test_euler.lua

test_%: test

.PHONY: clean
clean:
@echo "No files to clean."
59 changes: 59 additions & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Lua Section
============

.. .. |Lua Check| image:: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/rust.yml/badge.svg
.. :target: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/rust.yml
.. |Lua Check|
Organization
------------

All files share a common prefix with their problem number. All shared
functions are moved to files in the include subfolder

Makefile
--------

There are three main recipes in this Makefile

test
~~~~

Runs the test script

test\_\*
~~~~~~~~

In most subdirectories this would run tests in parallel, however this is not implemented for lua, and it will be redirected to ``test``

clean
~~~~~

Cleans up any stray files from runtime

Tests
-----

Generic Problems
~~~~~~~~~~~~~~~~

For each problem it will check the answer against a known dictionary. If
the problem is not in the "known slow" category (meaning that I generate
the correct answer with a poor solution), it will run it as many times
as the benchmark plugin wants. Otherwise it is run exactly once.

A test fails if it gets the wrong answer or if it takes more than 1
minute.

Dependencies
------------

I try to keep the dependencies of this project as small as possible,
except for test plugins. At the moment there are no dependencies for this section.

Problems Solved
---------------

- ☒ `1 <./src/p0001.lua>`__
- ☒ `2 <./src/p0002.lua>`__
22 changes: 22 additions & 0 deletions lua/src/p0001.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
return {
solution = function()
answer = 0

for i = 3,1000,3
do
answer = answer + i
end

for i = 5,1000,5
do
answer = answer + i
end

for i = 15,1000,15
do
answer = answer - i
end

return answer
end
}
21 changes: 21 additions & 0 deletions lua/src/p0002.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
return {
solution = function()
answer = 0
a = 1
b = 2

while b < 4000000
do
answer += b

for j = 0,3,1
do
t = b
b = a + b
a = t
end
end

return answer
end
}
46 changes: 46 additions & 0 deletions lua/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- Function to load a problem solution file
local function load_problem(file_name)
local func, err = loadfile(file_name)
if not func then
error("Failed to load file " .. file_name .. ": " .. err)
end
local chunk = func()
if type(chunk) ~= "table" or not chunk.solution then
error("File " .. file_name .. " must return a table with a 'solution' function")
end
return chunk.solution
end

-- Timing and result check function
local function check_problem(problem_func, expected_answer, timeout_seconds, problem_name)
local start_time = os.clock()
local success, result = pcall(problem_func)
local elapsed_time = os.clock() - start_time

if not success then
error("Error executing " .. problem_name .. ": " .. result)
end

if result ~= expected_answer then
error("Problem " .. problem_name .. " returned " .. tostring(result) .. ", but expected " .. tostring(expected_answer))
end

if elapsed_time > timeout_seconds then
error("Problem " .. problem_name .. " took " .. elapsed_time .. " seconds, exceeding the expected time limit of " .. timeout_seconds .. " seconds.")
end

print("Problem " .. problem_name .. " passed.")
end

-- Problems configuration: filename -> {expected_answer, timeout_seconds}
local problems = {
["p0001.lua"] = {233168, 60},
["p0002.lua"] = {4613732, 60},
-- Add more problems here as needed
}

-- Main testing loop
for file_name, config in pairs(problems) do
local problem_func = load_problem(file_name)
check_problem(problem_func, config[1], config[2], file_name:match("(%d+)"))
end

0 comments on commit 9c636c4

Please sign in to comment.