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/Problem67.lua
|
||||
--Matthew Ellison
|
||||
-- Created: 03-26-19
|
||||
--Modified: 03-28-19
|
||||
--Modified: 06-19-20
|
||||
--Find the maximum total from top to bottom
|
||||
--[[
|
||||
59
|
||||
@@ -108,7 +108,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
|
||||
@@ -136,7 +136,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];
|
||||
@@ -144,17 +144,8 @@ function invert(list)
|
||||
end
|
||||
end
|
||||
|
||||
function foundInList(list, loc)
|
||||
for location=1,#list do
|
||||
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
|
||||
return true;
|
||||
end
|
||||
end
|
||||
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);
|
||||
@@ -165,13 +156,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 = 100;
|
||||
|
||||
|
||||
list = {};
|
||||
local list = {};
|
||||
list[1] = {59};
|
||||
list[2] = {73, 41};
|
||||
list[3] = {52, 40, 9};
|
||||
@@ -276,18 +267,18 @@ list[100] ={23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28,
|
||||
--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];
|
||||
@@ -301,8 +292,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
|
||||
@@ -312,7 +303,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