Re: Rosetta Code
Posted: 02 Feb 2014, 18:29
"Twelve Statements" done. Will upload it to Rosetta Code later.
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
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 )
}
}
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