mirror of
https://bitbucket.org/Mattrixwv/projecteulerlua.git
synced 2025-12-06 17:43:57 -05:00
Updated problems to be more in line with conventions
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
--ProjectEuler/lua/Problem18.lua
|
||||
--Matthew Ellison
|
||||
-- Created: 03-12-19
|
||||
--Modified: 03-28-19
|
||||
--Modified: 06-19-20
|
||||
--Find the maximum total from top to bottom
|
||||
--[[
|
||||
75
|
||||
@@ -23,7 +23,7 @@
|
||||
--This is done using a breadth first search
|
||||
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
|
||||
--[[
|
||||
Copyright (C) 2019 Matthew Ellison
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -51,7 +51,7 @@ location = {
|
||||
};
|
||||
]]
|
||||
|
||||
function invert(list)
|
||||
local function invert(list)
|
||||
for rowCnt=1,NUM_ROWS do
|
||||
for colCnt=1,#list[rowCnt] do
|
||||
list[rowCnt][colCnt] = 100 - list[rowCnt][colCnt];
|
||||
@@ -59,7 +59,7 @@ function invert(list)
|
||||
end
|
||||
end
|
||||
|
||||
function foundInList(list, loc)
|
||||
local function foundInList(list, loc)
|
||||
for location=1,#list do
|
||||
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
|
||||
return true;
|
||||
@@ -68,8 +68,8 @@ function foundInList(list, loc)
|
||||
return false;
|
||||
end
|
||||
|
||||
function remove_if(list, loc)
|
||||
location = 1;
|
||||
local function remove_if(list, loc)
|
||||
local location = 1;
|
||||
while(location <= #list) do
|
||||
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
|
||||
table.remove(list, location);
|
||||
@@ -80,13 +80,13 @@ function remove_if(list, loc)
|
||||
end
|
||||
|
||||
--Create a timer and time the algorithm
|
||||
timer = Stopwatch:create();
|
||||
local timer = Stopwatch:create();
|
||||
timer:start();
|
||||
|
||||
NUM_ROWS = 15;
|
||||
local NUM_ROWS = 15;
|
||||
|
||||
|
||||
list = {};
|
||||
local list = {};
|
||||
list[1] = {75};
|
||||
list[2] = {95, 64};
|
||||
list[3] = {17, 47, 82};
|
||||
@@ -106,18 +106,18 @@ list[15] = {04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23};
|
||||
--Invert the list so all elements are 100 - element
|
||||
invert(list);
|
||||
|
||||
foundPoints = {}; --This is a table of locations
|
||||
local foundPoints = {}; --This is a table of locations
|
||||
foundPoints[1] = {xLocation = 1, yLocation = 1, total = list[1][1], fromRight = false};
|
||||
possiblePoints = {}; --This is a table of locations
|
||||
local possiblePoints = {}; --This is a table of locations
|
||||
--Add the second row as possible points
|
||||
possiblePoints[1] = {xLocation = 1, yLocation = 2, total = (list[1][1] + list[2][1]), fromRight = true};
|
||||
possiblePoints[2] = {xLocation = 2, yLocation = 2, total = (list[1][1] + list[2][2]), fromRight = false};
|
||||
foundBottom = false; --Used when you find a point at the bottom
|
||||
local foundBottom = false; --Used when you find a point at the bottom
|
||||
|
||||
--Loop until you find the bottom
|
||||
while(not foundBottom) do
|
||||
--Check which possible point gives us the lowest number. If more than one has the same number simply keep the first one
|
||||
minLoc = possiblePoints[1];
|
||||
local minLoc = possiblePoints[1];
|
||||
for loc=1,#possiblePoints do
|
||||
if(possiblePoints[loc].total < minLoc.total) then
|
||||
minLoc = possiblePoints[loc];
|
||||
@@ -137,8 +137,8 @@ while(not foundBottom) do
|
||||
|
||||
--Add to the list of possible points from the point we just found and
|
||||
--If you are at the bottom raise the flag to end the program
|
||||
xLoc = minLoc.xLocation;
|
||||
yLoc = minLoc.yLocation + 1; --Add one because you will always be moving to the next row
|
||||
local xLoc = minLoc.xLocation;
|
||||
local yLoc = minLoc.yLocation + 1; --Add one because you will always be moving to the next row
|
||||
if(yLoc > NUM_ROWS) then
|
||||
foundBottom = true;
|
||||
else
|
||||
@@ -150,7 +150,7 @@ while(not foundBottom) do
|
||||
end
|
||||
end
|
||||
|
||||
actualTotal = ((100 * (NUM_ROWS)) - foundPoints[#foundPoints].total);
|
||||
local actualTotal = ((100 * (NUM_ROWS)) - foundPoints[#foundPoints].total);
|
||||
|
||||
--Stop the timer
|
||||
timer:stop();
|
||||
|
||||
Reference in New Issue
Block a user