Rosetta Code

Talk about anything
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Rosetta Code

02 Feb 2014, 18:29

"Twelve Statements" done. Will upload it to Rosetta Code later.

Code: Select all

; Note: the original puzzle provides 12 statements and starts with
; "Given the following twelve statements...", so the first statement
; should ignore the F[1] flag and always be true (see "( N == 1 )").

S := 12 ; number of statements
Output := ""
Loop, % 2**S {
    ;;If !Mod(A_Index,100) ;; optional 'if' to show the loop progress
    ;;    ToolTip, Index: %A_Index%
    SetFlags(A_Index-1), Current := "", Count := 0
    Loop, %S%
        R := TestStatement(A_Index), Current .= R, Count += (R == F[%A_Index%])
    If ( Count >= S-1 )
        Output .= "`n[" Count "] -> " Current
}
ToolTip
MsgBox, % SubStr(Output,2)
Return

;-------------------------------------------------------------------------------------

SetFlags(D) {
    Global
    Local I
    Loop, %S%
        I := S-A_Index+1 , F[%I%] := (D >> (S-A_Index)) & 1
}

;-------------------------------------------------------------------------------------

TestStatement(N) {
    Global
    Local I, C := 0
    If ( N == 1 ) ; This is a numbered list of twelve statements.
        Return ( S == 12 ) ; should always be true
    If ( N == 2 ) { ; Exactly 3 of the last 6 statements are true.
        Loop, 6
            I := S-A_Index+1 , C += F[%I%]
        Return ( C == 3 )
    }
    If ( N == 3 ) { ; Exactly 2 of the even-numbered statements are true.
        Loop, %S%
            C += ( !Mod(A_Index,2) & F[%A_Index%] )
        Return ( C == 2 )
    }
    If ( N == 4 ) ; If statement 5 is true, then statements 6 and 7 are both true.
        Return ( F[5] ? F[6] & F[7] : 1 )
    If ( N == 5 ) { ; The 3 preceding statements are all false.
        Loop, 3
            I := N-A_Index , C += F[%I%]
        Return ( C == 0 )
    }
    If ( N == 6 ) { ; Exactly 4 of the odd-numbered statements are true.
        Loop, %S%
            C += ( !!Mod(A_Index,2) & F[%A_Index%] )
        Return ( C == 4 )
    }
    If ( N == 7 ) ; Either statement 2 or 3 is true, but not both.
        Return ( F[2] ^ F[3] )
    If ( N == 8 ) ; If statement 7 is true, then 5 and 6 are both true.
        Return ( F[7] ? F[5] & F[6] : 1 )
    If ( N == 9 ) { ; Exactly 3 of the first 6 statements are true.
        Loop, 6
            C += F[%A_Index%]
        Return ( C == 3 )
    }
    If ( N == 10 ) ; The next two statements are both true.
        Return ( F[11] & F[12] )
    If ( N == 11 ) ; Exactly 1 of statements 7, 8 and 9 are true
        Return ( F[7]+F[8]+F[9] == 1 )
    If ( N == 12 ) { ; Exactly 4 of the preceding statements are true
        Loop, % N-1
            C += F[%A_Index%]
        Return ( C == 4 )
    }
}
I adapted the Python code, using bruteforce (4096 iterations) to set 12 flags and test all statements on each iteration.
If the proposed flags match the results after validating each statement, we have the solution.
I show all cases where we have at least S-1 matches (being S := 12 statements).

Bonus:
Also, the puzzle can be easily modified without much additional coding.
And it works for AHK 1.0 as well! haha :lol:
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Rosetta Code

02 Feb 2014, 20:16

@MasterFocus You beat me to it :lol:
I based mine on the Java version.

Code: Select all

global S
S := {1: 1}
Recurse(2)
ExitApp

Check2() {
	return (S[2] = (S[7]+S[8]+S[9]+S[10]+S[11]+S[12] = 3))
}

Check3() {
	return (S[3] = (S[2]+S[4]+S[6]+S[8]+S[10]+S[12] = 2))
}

Check4() {
	if (S[5])
		return (S[4] = (S[6]+S[7] = 2))
	else
		return (S[4] = 1)
}

Check5() {
	return (S[5] = (S[2]+S[3]+S[4] = 0))
}

Check6() {
	return (S[6] = (S[1]+S[3]+S[5]+S[7]+S[9]+S[11] = 4))
}

Check7() {
	return (S[7] = (S[2]+S[3] = 1))
}

Check8() {
	if (S[7])
		return (S[8] = (S[5]+S[6] = 2))
	else
		return 
}

Check9() {
	return (S[9] = (S[1]+S[2]+S[3]+S[4]+S[5]+S[6] = 3))
}

Check10() {
	return (S[10] = (S[11]+S[12] = 2))
}

Check11() {
	return (S[11] = (S[7]+S[8]+S[9] = 1))
}

Check12(t=0) {
	Loop 11
		if (S[A_Index])
			t++
	return (S[12] = (t = 4))
}

Check() {
	if (Check2() && Check3() && Check4() && Check5() && Check6() Check7() && Check8() && Check9() && Check10() && Check11() && Check12()) {
		for k, v in S
			if (v)
				Out .= k " "
		MsgBox, % "True statements: " Out
	}
}

Recurse(k) {
	if (k = 13)
		Check()
	else {
		S[k] := 0
		Recurse(k+1)
		S[k] := 1
		Recurse(k+1)
	}
}
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Rosetta Code

02 Feb 2014, 20:58

I was trying to fix Anonymous Recursion, but that's the best I could do:

Code: Select all

/*
Fib(x) {
    t := -1
    If ( x >= 0 )
        GoSub, Fib_Label
    Else
        MsgBox, Oops!
    Return t
    Fib_Label:
        i := 0, j := 1
        Loop, % x-1
            t := i+j, i := j, j := t
    Return
}

MsgBox, % "Calling Fib(5) ..."
MsgBox, % "Result: " Fib(5)
MsgBox, % "Calling Fib(-3) ..."
MsgBox, % "Result: " Fib(-3)
ListVars
MsgBox, % "ListVars shows the variables are not global."
;GoSub, Fib_Label ; <--- this yields an error
*/
This currently works, but doesn't fulfill the task, as the calculation itself should be done recursively.
I'm not used to AHK Objects, so maybe someone can figure this out.

On a related note (fixing stuff), can this task actually be completed by AutoHotkey, obeying all four items?
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
User avatar
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: Rosetta Code

02 Feb 2014, 23:20

Anyone look at 100 Doors yet?
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Rosetta Code

03 Feb 2014, 00:07

That should be relatively easy.
I'm putting here 4 versions I came up with. I suppose it's pretty self-explanatory.
Spoiler
Once again, here's everything I'll upload later (hopefully until wednesday):
[list][*]Kaprekar Numbers
[*]Twelve Statements
[*]100 Doors (was actually already solved)[/list]
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Rosetta Code

03 Feb 2014, 00:48

Nice! :D
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Rosetta Code

03 Feb 2014, 02:32

fischgeek wrote:Anyone look at 100 Doors yet?
any perfect sq. like 1,4,9,...100 has odd no. of factors.
rest have even no. of factors.

;)
John ... you working ?
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Rosetta Code

03 Feb 2014, 22:34

@fischgeek

Code: Select all

m := 1, k := 1
loop, 100{
	gosub, label
	MsgBox  % !mod((sqrt(k)), 1 ) ? k " Open" . "`nFactors-" . list . "`nOdd number of factors" : k " Closed " . "`nFactors-" . list . "`nEven number of factors"
	list := "", 	m := 1, k := k + 1
}  
 exitapp
 esc::exitapp
  label:
 loop, % k/2{
  list .= !mod(k, m) ? m "," : ""
   m++
}
list := list . k
return
   
May Be.
John ... you working ?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Rosetta Code

03 Feb 2014, 23:50

Fibonacci word/fractal

100 doors is already solved. :?
Looks like @sinkfaze solved it and @tinku99 submitted it. Link
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Rosetta Code

04 Feb 2014, 22:49

Added: Draw a cuboid
Does anyone know how to upload an image file like the other examples on that page have done?

I made this animated 3d version just for fun. The dimensions can be changed with hotkeys (Up/Down/Left/Right/PgUp/PgDn/Home/End).
I think this version is more interesting, but it doesn't fit the requirements of the Rosetta Code task.

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

C1 := 0xff0000aa, C2 := 0xff00aa00, C3 := 0xffaa0000

; Uncomment if Gdip.ahk is not in your standard library
;#Include, Gdip.ahk

If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}
OnExit, Exit

Width := A_ScreenWidth, WC := Width//2 , Height := A_ScreenHeight, HC := Height//2
	, TopLeftX := (A_ScreenWidth - Width)//2, TopLeftY := (A_ScreenHeight - Height)//2

Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, 1: Show, NA

hwnd1 := WinExist(), hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC()
	, obm := SelectObject(hdc, hbm), G := Gdip_GraphicsFromHDC(hdc), Gdip_SetSmoothingMode(G, 4)
	, speed := 1, s := 120, d := 20, h := 120, 2h := h + h

Loop {
	mcx := (mc := Cos(a := Mod(A_Index, 90)* 0.01745329252)) * s
		, mcy := mc * d
		, msx := (ms := Sin(a)) * s
		, msy := ms * d
	
	; Points
	x1 := WC - mcx,  y1 := HC - msy - h
		, x2  := WC + msx, y2 := HC - mcy - h
		, x3  := WC + mcx, y3 := HC + msy - h
		, x4  := WC - msx, y4 := HC + mcy - h
		, yb1 := y1 + 2h, yb2 := y2 + 2h
		, yb3 := y3 + 2h, yb4 := y4 + 2h
	
	; Fill Top
	P1 := x1 "," y1 "|" x2 "," y2 "|" x3 "," y3 "|" x4 "," y4
		, DrawFace(P1, C3, G)
	
	; Fill Vertical Faces
	P1 := x1 "," y1 "|" x4 "," y4 "|" x4 "," yb4 "|" x1 "," yb1
		, P2 := x4 "," y4 "|" x3 "," y3 "|" x3 "," yb3 "|" x4 "," yb4
		, P3 := x2 "," y2 "|" x2 "," yb2 "|" x3 "," yb3 "|" x3 "," y3
		, i :=Mod(A_Index, 360)
	if (i < 90 || (i < 270 && i >= 180))
		DrawFace(x1 < x4 ? P1 : P3, C1, G)
			, DrawFace(P2, C2, G)
	else {
		if (x1 < x4) 
			DrawFace(P1, C2, G)
				, DrawFace(P2, C1, G)
		else 
			DrawFace(P2, C1, G)
				, DrawFace(P3, C2, G)
	}

	UpdateLayeredWindow(hwnd1, hdc, TopLeftX, TopLeftY, Width, Height)
	if (!Mod(A_Index, speed))
		Sleep, 30
	Gdip_GraphicsClear(G)
}

DrawFace(Points, Color, G) {
	pBrush := Gdip_BrushCreateSolid(Color)
		, Gdip_FillPolygon(G, pBrush, Points, 1)
		, Gdip_DeleteBrush(pBrush)
	return
}

p::Pause

Right::
s++
Set_TT("s", s)
return

Left::
s--
Set_TT("s", s)
return

Up::
d++
Set_TT("d", d)
return

Down::
if (d > 0)
	d--
Set_TT("d", d)
return

PGUP::
h++
2h := h + h
Set_TT("h", h)
return

PGDN::
if (h > 0)
	h--
2h := h + h
Set_TT("h", h)
return

Home::
If (Speed < 3)
	Speed++
Set_TT("Speed", Speed)
return

End::
If (Speed > 1)
	Speed--
Set_TT("Speed", Speed)
return

Set_TT(s, v) {
	ToolTip, % s ": " v
	SetTimer, TT, -1500
	return
}

TT:
ToolTip
return

Esc::
Exit:
SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
	, Gdip_DeleteGraphics(G), Gdip_Shutdown(pToken)
ExitApp
Return
Image
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Rosetta Code

05 Feb 2014, 00:22

As soon as I find Bentschis OpenGL library somewhere i will rewrite that one.
Recommends AHK Studio
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Rosetta Code

05 Feb 2014, 01:18

I'd be very interested to see that.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Rosetta Code

05 Feb 2014, 15:34

kon wrote:Added: Draw a cuboid
off the charts, thanks kon
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Rosetta Code

05 Feb 2014, 15:46

AlphaBravo wrote:off the charts, thanks kon
Thanks :D
If I wasn't clear in my post above, the version I posted to Rosetta code just shows a static image. The animated version I posted above (just for fun) doesn't fit the task requirements of a 2x3x4 cuboid. It will always have a square top for the sake of simplicity. I may be able to change it eventually to allow for 2x3x4 in dimensions, but I think that will involve some fairly extensive changes to how the points are calculated.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Rosetta Code

05 Feb 2014, 16:48

With OpenGL that would be the easy part.
But I don't have access to my PMs at autohotkey.com
Bentschi PMd me something like this.
Recommends AHK Studio
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Rosetta Code

06 Feb 2014, 10:00

9 billion names of God the integer
http://rosettacode.org/wiki/9_billion_n ... AutoHotKey
Other parts are easy, shall upload them later.
At least i solved one! :ugeek:
John ... you working ?
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Rosetta Code

06 Feb 2014, 22:30

nice!
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Off-topic Discussion”

Who is online

Users browsing this forum: No registered users and 64 guests