better constrain possible problems

It seems the cleanest way to get what we want is to generate the
solution up front.
This commit is contained in:
Kartik K. Agaram 2023-11-16 09:14:51 -08:00
parent 7860ad4bf8
commit d5f5859f02
7 changed files with 16 additions and 44 deletions

View File

@ -4,8 +4,6 @@ print(chord)
new()
elseif chord == '`' then
if Draw == 'problem' then
Draw = 'char'
elseif Draw == 'char' then
Draw = 'solution'
elseif Draw == 'solution' then
Draw = 'problem'

View File

@ -1,9 +0,0 @@
-- a set of numbers that uniquely characterizes a sum-grid instance.
Char = {
row_totals={0, 0},
col_totals={0, 0},
data={
{0, 0},
{0, 0},
},
}

View File

@ -1,4 +1,3 @@
-- solution for the sum-grid problem defined by Char.
Solution = {
row_totals={0, 0},
col_totals={0, 0},

View File

@ -1,21 +0,0 @@
solve = function()
print('solve')
-- copy the slots we have
local c = Char.col_totals
local d = Char.data
Solution = {
col_totals = {c[1], c[2]},
data = {
{d[1][1], d[1][2] },
{ },
}
}
-- fill out the remaining data
Solution.data[2][1] = c[1] - d[1][1]
Solution.data[2][2] = c[2] - d[1][2]
d = Solution.data
Solution.row_totals = {
d[1][1] + d[1][2],
d[2][1] + d[2][2],
}
end

View File

@ -1 +1 @@
Draw = 'problem'
Draw = 'problem' -- or 'solution'

View File

@ -1,14 +1,20 @@
generate = function()
-- a sum grid is fully characterized by 4 numbers
Char = {
local g = {
col_totals = {'', ''},
row_totals = {'', ''},
data = {
{ math.random(1,9), math.random(1,9) },
{ '', ''},
{'', ''},
{'', ''},
},
row_totals = { '', '' },
}
Char.col_totals = {
math.random(Char.data[1][1]+1,10),
math.random(Char.data[1][2]+1,10)
}
g.data[1][1] = math.random(1,9)
local d11 = g.data[1][1]
g.row_totals[1] = math.random(d11+1,10)
g.col_totals[1] = math.random(d11+1,10)
g.data[1][2] = g.row_totals[1] - d11
g.data[2][1] = g.col_totals[1]-d11
g.row_totals[2] = math.random(g.data[1][2]+1,10)
g.col_totals[2] = math.random(g.data[2][1]+1,10)
g.data[2][2] = g.col_totals[2]-g.data[2][1]
Solution = g
end

View File

@ -1,5 +1,4 @@
new = function()
generate()
solve()
create_problem()
end