|
Post by pierre on Oct 28, 2022 5:14:34 GMT -5
Ok! Someone had to do it. I tried it using memory SQLite. It's fast, but doubt this is the best way to do it. It's in RunBasic simply because it's easier to write in RB than LB. But should be easy to convert to LB if anyone is interested. Good news : the Runbasic code works in LB 5 alpha. Bad news : the results are not correct. here is what they should have been: A G B 8 A Y B 7 <---- A Y S 10 P G B 6 P Y B 11 I will have to study the code. That will take time.
|
|
|
Post by tsh73 on Oct 28, 2022 6:10:35 GMT -5
After reading Bplus' code, I fixed part of my code that bothered me
Of cource I get storing items in arrays, ant it works more then 10x slower But now I can write
if sum("A")<>25 instead of
if aby+asy+abg+asg<>25
So. bruteforce/nested loops, using names.
' Use -1 to tell code that that value has not been determined yet ASY=1: names$(1) = "ASY" : values(1) = -1 ' for now ASG=2: names$(2) = "ASG" : values(2) = 0 ' stays 0 ALY=3: names$(3) = "ALY" : values(3) = -1 ' > we want answer to this ALG=4: names$(4) = "ALG" : values(4) = -1 ' for now PSY=5: names$(5) = "PSY" : values(5) = 0 ' stays = 0 no SP's PSG=6: names$(6) = "PSG" : values(6) = 0 ' stays = 0 no SP's PLY=7: names$(7) = "PLY" : values(7) = -1 ' for now PLG=8: names$(8) = "PLG" : values(8) = -1 ' for now
t0=time$("ms") asg=0 psy=0 psg=0
for pby = 0 to 42 values(PLY) =pby for pbg = 0 to 42 values(PLG)=pbg if sum("P") <>17 then [contPbg] for aby = 0 to 42 values(ALY)=aby for asy = 0 to 42 values(ASY)=asy SCAN if sum("Y") <>28 then [contAsy] abg=42-sumExcept("ALG") values(ALG)=abg 'next value does not seems necessary ' if abg<0 then [contAsy] 'negative apples does not make sense! if sum("A")<>25 then [contAsy] if sum("L")<>32 then [contAsy] '"two more" condition if sum2("G","A")<>sum2("G","P")+2 then [contAsy] 'else we got it for j = 1 to 8 line1$=line1$+names$(j)+" " line2$=line2$+using("###", values(j))+" " next print line1$ print line2$ [contAsy] next next [contPbg] next next t1=time$("ms")
print "Found in ";t1-t0; " ms"
end
function sum(letter$) for N = 1 to 8 if instr(names$(N), letter$) then sum = sum + values(N) next end function
function sumExcept(code$) for N = 1 to 8 if names$(N)<>code$ then sumExcept = sumExcept + values(N) next end function
function sum2(letter1$, letter2$) for N = 1 to 8 if instr(names$(N), letter1$)<>0 and instr(names$(N), letter2$)<>0 then sum2 = sum2 + values(N) next end function
|
|
|
Post by meerkat on Oct 28, 2022 7:22:59 GMT -5
[quote Bad news : the results are not correct.
here is what they should have been:
A G B 8 A Y B 7 <---- A Y S 10 P G B 6 P Y B 11
I will have to study the code. That will take time.[/quote]
Sorry about that. I'm on the road and did it in a hurry and never checked the results. My bad. I'll have another look at it when I get home..
*** For some reason the SELECTS WHERE colr = 'Y' or 'G' do not work in memory. I opened a db on disk and they worked. *** Haven't done any more than that, but will get back
If you get it to work b4 I do, I'll be happy..
Thanks for the info.. Dan
|
|
|
Post by pierre on Oct 29, 2022 14:19:26 GMT -5
change this:
mem$ = "CREATE TABLE box(type, siz, colr)" 'instead of (type,colr,siz)
in the section "need 32 big fruits"
need = 32 - totB 'instead of need = 32 = totB
then the results will be:
---- Totals by type siz colr ---- SELECT count(*) as t,* FROM box GROUP BY type,siz,colr A B G 8 A B Y 7 A S Y 10 P B G 6 P B Y 11
OK
|
|
|
Post by meerkat on Oct 29, 2022 14:57:50 GMT -5
thanks pierre
Some pretty stupid errors..
I took out the debug prints to get a better run time. I got 9ms. That seems too fast.. Maybe it's wrong cause I'm still having problems with SQLite in RB
What did you get??
sqliteconnect #mem, ":memory:" mem$ = "CREATE TABLE box(type,siz,colr)" #mem execute(mem$) ' create in memory db
begTime = time$("ms") ' ------------------------------------ ' seed the db with apples and make some yellow some green ' no small green apples please ' ------------------------------------ for i = 1 to 25 ' gimme 25 Apples colr$ = "Y" siz$ = "S" if (i and 1) then ' No small green apples colr$ = "G" siz$ = "B" end if mem$ = "INSERT INTO box VALUES('A','";siz$;"','";colr$;"')" #mem execute(mem$) next i ' ------------------------------------------------- ' seed the db with pears no small pears please ' ------------------------------------------------- siz$ = "B" ' All pears are big for i = 1 to 17 ' gimme 17 pears colr$ = "Y" ' some yellow some green if (i and 1) then colr$ = "G" mem$ = "INSERT INTO box VALUES('P','";siz$;"','";colr$;"')" #mem execute(mem$) next i
' ----------------------------------------- ' need 28 yellow fruits ' if we need more yellow then change some green to yellow ' ----------------------------------------- mem$ = "UPDATE box SET colr = 'Y' WHERE rowid IN ( SELECT rowid FROM box WHERE colr = 'G' LIMIT ( SELECT 28 - count(*) FROM box WHERE colr = 'Y'))" #mem execute(mem$)
' ------------------------------------------- ' need 2 more green apples than green pares ' if need more then change some green pares to yellow ' and some yellow apples to green ' ------------------------------------------- mem$ = "SELECT *,count(*) as t FROM box WHERE colr = 'G' GROUP BY type" #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() colr$ = #row colr$() t = #row t() print type$;colr$;"=";t if type$ = "P" then totGP = t if type$ = "A" then totGA = t WEND need = (totGP - totGA)/2 + 1 print "need more green apples ";need if need > 0 then ' change some green pares to yellow mem$ = "UPDATE box set colr = 'G' WHERE type = 'A' and siz = 'B' and colr = 'Y' LIMIT ";need #mem execute(mem$) mem$ = "UPDATE box set colr = 'Y' WHERE type = 'P' and colr = 'G' LIMIT ";need #mem execute(mem$) end if
' ------------------------------------------- ' need 32 big fruits ' if need change some Small to Big ' -------------------------------------------- mem$ = "UPDATE box SET siz = 'B' WHERE WHERE siz = 'S' LIMIT (SELECT 32 - count(*) FROM box WHERE siz='B' group by siz))" #mem execute(mem$)
' -------------------------------------- ' The results ' totals by type, size, color ' -------------------------------------- print "---- Totals by type siz colr ----" mem$ = "SELECT count(*) as t,* FROM box GROUP BY type,siz,colr" #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() siz$ = #row siz$() colr$ = #row colr$() t = #row t() print type$;siz$;colr$;"=";t WEND endTime = time$("ms") print "Total Time:";endTime - begTime
#mem disconnect() end
|
|
|
Post by pierre on Oct 29, 2022 15:56:43 GMT -5
You have beaten metro !
here is what I've collected on my (rather slow) laptop:
- meerkat RB & SQLITE 11 ms - metro (pen & paper) 20 ms - Bplus 85 ms - pierre (pen & paper) 166 ms - metro (loopy loop) 247 ms - meerkat LB5 & SQLITE 337 ms - tsh73 (brute force) 710 ms
|
|
|
Post by meerkat on Oct 29, 2022 18:50:47 GMT -5
The code wasn't built for speed. However using 1 INSERT command instead of 42 INSERTs I was able to knock 3ms off the time I get 6ms instead of 9ms.
mem$ = "" for i = 1 to 25 ' gimme 25 Apples colr$ = "Y" siz$ = "S" if (i and 1) then ' No small green apples colr$ = "G" siz$ = "B" end if mem$ = mem$ + cma$+"('A','";siz$;"','";colr$;"')" cma$ = "," next i
siz$ = "B" ' All pears are big for i = 1 to 17 ' gimme 17 pears colr$ = "Y" ' some yellow some green if (i and 1) then colr$ = "G" mem$ = mem$ + cma$+"('P','";siz$;"','";colr$;"')" next i
mem$ = "INSERT INTO box VALUES ";mem$ #mem execute(mem$) '<===== only 1 INSERT
|
|
|
Post by metro on Oct 29, 2022 20:29:14 GMT -5
Super Cool using SQlite Dan, I do believe AYB has not been catered for though (maybe too many late nights at the bar)
Using my code If I don't print all permutations and just the required answer I get better mileage... mind you not sure if WINE &LINUX aren't messin with the time calculation. time varies each run. i7 desktop Mint Linux
|
|