This is a read-only archive. No new posts or registrations. Privacy Page
Discussion

Monday Madness

13 min read

This thread's last reply is from . Advice, software, and links below may be out of date — treat specific steps and download links with caution.

well done raktor.. :thumbup: i'm in the middle of writing a bash script to work it out, just never really written any before(nearly 30yrs since i did any programming), so taking me a lot longer to get it working because i have no idea of the correct syntax, etc.. :pullhair: if i ever get it working i'll post it tho. :rofl: :popcorn:
well it sort of works.. :rofl:

output..
Wed Oct 20 19:23:41 BST 2010
Wed Oct 20 19:24:11 BST 2010
Wed Oct 20 19:24:43 BST 2010
Wed Oct 20 19:25:14 BST 2010
Wed Oct 20 19:25:30 BST 2010
453x6=2718 correct
Wed Oct 20 19:25:44 BST 2010
Wed Oct 20 19:26:12 BST 2010
582x3=1746 correct
Wed Oct 20 19:26:16 BST 2010
Wed Oct 20 19:26:48 BST 2010
Wed Oct 20 19:27:20 BST 2010
Wed Oct 20 19:27:55 BST 2010

now my first ever bash script
#!/bin/bash
function debit {
l=8
echo "before -" $y
y=$(($y-$l))
echo "after -"$y"—"$l
}
#===============================================================================
===================
function add {
t=$(($t+1))
}
#===============================================================================
===================
function check {
t=0;q=0;o=0;y=0;j=0;x=0
for j in $a $b $c $d $e $f $g $h; do
x[q]=$j
#echo ${x[q]} >> sumtest.txt
q=$(($q+1))
done
for q in 0 1 2 3 4 5 6 7; do
for o in 1 2 3 4 5 6 7; do
y=$(($q+$o))
if [ $y > $7 ]
then debit; fi
if [ ${x[q]} = ${x[$y]} ]
then add; fi
#echo $q $y" compare "${x[q]}" to "${x[$y]}"–loop–"$o"–count–"$t >> sumtest.txt
done;done
if [ $t = 0 ];then date >> sumtest.txt;echo $a$b$c"x"$d"="$e$f$g$h "correct" >> sumtest.txt; fi
}
#===============================================================================
==================
date >> sumtest.txt
for a in 1 2 3 4 5 6 7 8; do
for b in 1 2 3 4 5 6 7 8; do
for c in 1 2 3 4 5 6 7 8; do
for d in 1 2 3 4 5 6 7 8; do
for e in 1 2 3 4 5 6 7 8; do
for f in 1 2 3 4 5 6 7 8; do
for g in 1 2 3 4 5 6 7 8; do
for h in 1 2 3 4 5 6 7 8; do
k=$(($a$b$c*$d));s=$e$f$g$h;
if [ $s = $k ]
then check; fi
done;done;done;done;done;done;done;date >> sumtest.txt;done

now for the problem in it..
if [ $y > $7 ]
then debit; fi
no matter what i did(spent about 10 hrs trial and error on it) i just could find a way to do a simple "if number => 7 then delete 8 from it"

so got lucky anyway and it gave up the correct two solutions.. :rofl:

no where near as elegant or efficient as your coding raktor, :thumbup:
takes about 4 1/2 minutes to complete on my machine, but it got the job 9/10's done.. :rofl:

right off to bed to get some sleep now.

:popcorn:

takes about 4 1/2 minutes to complete on my machine

that's quite long, though don't know anything about bash scripts :)

did this in delphi, it took about 35 ms to spit out the output.
excerp (as the entire code is really bad), took 8 digits taken 4 at a time for permutaion then took those first 3 digits multiplied to the fourth digit, after that, time to remove the items with zero's, nine's and duplicates:
s2 := StringReplace(s, ',', '', [rfReplaceAll]);
	  prod := StrToInt(Copy(s2, 1, 3)) * StrToInt(Copy(s2, 4, 1));
	  s3 := Copy(s2, 4, 1);
	  s2 := Copy(s2, 1, 3) + ' * ' + Copy(s2, 4, 1) + ' = ' + IntToStr(prod);
	  s4 := Copy(s2, 1, 3) + s3 + IntToStr(prod);

	  if Length(IntToStr(prod)) = 4 then
	  begin
		  // remove 0
		  if Pos('0', Copy(s2, 1, 3) + s3 + IntToStr(prod)) = 0 then
		 begin
		 	// remove the first char
			if Pos(Copy(Copy(s2, 1, 3) + s3 + IntToStr(prod), 1, 1), Copy(Copy(s2, 1, 3) + s3 + IntToStr(prod), 2, Length(Copy(s2, 1, 3) + s3 + IntToStr(prod)))) = 0 then
			begin
				// remove the 2nd char
			   if Pos(Copy(s4, 2, 1), Copy(s4, 1, 1) + Copy(s4, 3, length(s4))) = 0 then
			   begin
			   	// remove the 3nd char
				   if Pos(Copy(s4, 3, 1), Copy(s4, 1, 2) + Copy(s4, 4, length(s4))) = 0 then
				  begin
					  // remove the 4th char
					   if Pos(Copy(s4, 4, 1), Copy(s4, 1, 3) + Copy(s4, 5, length(s4))) = 0 then
					 begin
					 	// remove the 5th char
						   if Pos(Copy(s4, 5, 1), Copy(s4, 1, 4) + Copy(s4, 6, length(s4))) = 0 then
						begin
							// remove the 6th char
							   if Pos(Copy(s4, 6, 1), Copy(s4, 1, 5) + Copy(s4, 7, length(s4))) = 0 then
						   begin
						   	// remove the 7th char
								   if Pos(Copy(s4, 7, 1), Copy(s4, 1, 6) + Copy(s4, 8, length(s4))) = 0 then
							  begin
								  // remove the 8th char
									   if Pos(Copy(s4, 8, 1), Copy(s4, 1, 7)) = 0 then
								 begin
								 	// remove number 9
												if Pos('9', s4) = 0 then
												  memo1.lines.add(s2);
								 end; // 8
							  end; // 7
						   end; // 6
						end; // 5
					 end; // 4
				  end; // 3
			   end; // 2
			end; // 1
		 end; // 0
	  end;
	end;
Each of those "codes" look like fun… if I understood what they were doing. :notworthy: However… for a silly game… I can solve it in my head faster than I can type all of that code. :wacko: (I wrote a few programs in basica back in the early 80's - figuring out how many yards of dirt would have to be moved/imported/hauled away on a site to build the grade, or I had one that I could input parameters of a wall and it would give me a printout with number and length of all the studs, trimmers, cripples, headers and plates - these were pretty simple compared to what you guys have done. I always kind of wished I had the time to figure out how to write something real)
It's not that bad. You know it won't be multiplied by one - so try two. You know that nothing multiplied by two gets you to twenty so you know that the first of the 4 in the answer must be one. Therefore the first of the three must be 5,6,7,or 8 If you try 8 in the ones place of the three, you get a 6 in the ones place of the 4 try 7 in the tens place of the three and you get a 5 in the tens place of the 4 Therefore you know this wont work because 5,6,7, and 8 have all been used. So try 7 in the ones place of the three getting a 4 in the ones of the 4 if 8 can't be in the tens because 7 has been already used try 6 and you get 3 in the tens of the 4 Now you know this won't work because there is know combo to use the 8 Try 6 in the ones of the 3 and get 2 in the ones of the 4 Therefore this is wrong because 2 has been used Can't be 5 in ones because 0 not allowed 4 would give 8 in ones Can't be 7, 6, 5 or 4 in tens because 4, and 2 have been used and 0 not allowed 3 would fit giving 6 but leaving 5 and 7 for hundreds place which obviously won't work 3 in ones would give 6 in ones therefore 8 can't be in tens try 7 for 4 in tens Neither 8 nor 5 will work in hundreds 2 is already used and you know where 1 must be so obviously 2 cannot be the multiplier. You don't have to actually do all the iterations that are possible. Your code has to because it doesn't know when to give up on each try.
mine computes every combination from 11111111 through to 88888888
any time ???x?=???? it goes off to check if there are any duplicate numbers in it, if no, then it outputs answer,
if yes, and after no, it goes off to find more correct answers until every combination is worked through..
that's the reason why it takes so long.

not the most efficient way of doing things but gets the job done.. :rofl:

just wish i could figure out how to compare a changing variable $y to the number 7 and if greater go and take 8 away from it(needed to keep it checking correct array number) to stop it spitting out errors when run.. :pullhair:

something so simple but i just couldn't figure out how do it..(yet anyway, might do some googling and fix it later)

:popcorn:

finally fixed it..
l=7
if [ $y -gt $l ]
then debit; fi
now runs no errors..
time to bin it now and see if i can do better.. :rofl:
Alright you script wizards… I couldn't help myself this evening. I had to try to code the solution.

Seeing as how I don't know any code… I went for JavaScript.

I was right… I can solve it faster in my head. It took me almost two hours to make this work.

Now remember… no points are given for being pretty. It does output the correct answers in about a 1.5 seconds +/-.






That it's not constrained with ??? x ? = ???? but can also be used with something like ??? X ? = ????? or ???? x ? = ??? or something like that. :)