Rosetta Code Challenges
#7
(10-31-2022, 07:24 PM)Spriggsy Wrote: I used to peruse those challenges to see what I could do. Fellippe tried getting everyone on board with that a while back. Just so many things on that site.

Yes, I can't seem to get enough challenges that make me think but are doable. Here is great place to copy the masters!

Twelve Statements (Boolean Logic + Binary Scenarios Puzzle)

I did figure out Twelve Statements but I had to add Else clauses in Statement 4 and 8 handlers that make the statement True if the first condition is not. (Plus Statement 12 was missing a TF(7) in the total.)
Statement 4:  If statement 5 is true, then statements 6 and 7 are both true.
                    So where are we if 5 is NOT true?
                    Well I guess if I want to match Rosetta single unique solution I have to say:
                    ELSE statement(4) = 1

the same happens in
Statement 8: If statement 7 is true, then 5 and 6 are both true.
                    So where are we if 7 is NOT true?
                    Well I guess if I want to match Rosetta single unique solution I have to say:
                    ELSE statement(8) = 1

BTW allot of these statements have symmetric complements:
The first 6, the last 6
The evens, the odds
Statement 4 and statement 8

Here using 1 not -1, so be careful with IF's ANDs...

2 ^ 12 is the number of permutations of True and False (1 and 0) so convert all numbers 0 to 2^12 - 1 to Binary and you run all the possible ways T and F, 1 and 0, can be arranged in 12 digits slots. So run the numbers and see which one(s) produce a consistent match with the 12 statements. As usual with Rosetta Code problems the problem is setup to produce one or unique set of solutions. 
Code: (Select All)
Option _Explicit
_Title "12 Statements - Rosetta Code" ' b+  found 2022-10-30
'ref:     https://rosettacode.org/wiki/Twelve_statements

'     12 Statements
' 1.  This is a numbered list of twelve statements.
' 2.  Exactly 3 of the last 6 statements are true.
' 3.  Exactly 2 of the even-numbered statements are true.
' 4.  If statement 5 is true, then statements 6 and 7 are both true.
' 5.  The 3 preceding statements are all false.
' 6.  Exactly 4 of the odd-numbered statements are true.
' 7.  Either statement 2 or 3 is true, but not both.
' 8.  If statement 7 is true, then 5 and 6 are both true.
' 9.  Exactly 3 of the first 6 statements are true.
'10.  The next two statements are both true.
'11.  Exactly 1 of statements 7, 8 and 9 are true.
'12.  Exactly 4 of the preceding statements are true.

' Solution at Rosetta Code is with statements 1 3 4 6 7 11 true.

'VVVVVVVV mass comment method
'$If  Then
ReDim As Long n, TF(1 To 12), wrong, i
For n = 0 To 4095
    Dec2BinArr12 n, TF() ' get a TF scenario and compare to 12 statements
    ReDim st(1 To 12) As Long
    ' 1.  This is a numbered list of twelve statements.
    st(1) = 1

    ' 2.  Exactly 3 of the last 6 statements are true.
    If ((TF(7) + TF(8) + TF(9) + TF(10) + TF(11) + TF(12)) = 3) Then st(2) = 1

    ' 3.  Exactly 2 of the even-numbered statements are true.
    If ((TF(2) + TF(4) + TF(6) + TF(8) + TF(10) + TF(12)) = 2) Then st(3) = 1

    ' 4.  If statement 5 is true, then statements 6 and 7 are both true.
    If TF(5) Then
        If ((TF(6) + TF(7)) = 2) Then st(4) = 1
    Else ' we need next to say true??
        st(4) = 1 ' ?  not working the other way every other TF will be 0 anyway
    End If

    ' 5.  The 3 preceding statements are all false.
    If ((TF(4) + TF(3) + TF(2)) = 0) Then st(5) = 1

    ' 6.  Exactly 4 of the odd-numbered statements are true.
    If ((TF(1) + TF(3) + TF(5) + TF(7) + TF(9) + TF(11)) = 4) Then st(6) = 1

    ' 7.  Either statement 2 or 3 is true, but not both.
    If ((TF(2) + TF(3)) = 1) Then st(7) = 1

    ' 8.  If statement 7 is true, then 5 and 6 are both true.
    If TF(7) Then
        If ((TF(5) + TF(6)) = 2) Then st(8) = 1
    Else '
        st(8) = 1 ' ??? this is like 4  which fixed things when I added else like here
    End If

    ' 9.  Exactly 3 of the first 6 statements are true.
    If ((TF(1) + TF(2) + TF(3) + TF(4) + TF(5) + TF(6)) = 3) Then st(9) = 1

    '10.  The next two statements are both true.
    If ((TF(11) + TF(12)) = 2) Then st(10) = 1

    '11.  Exactly 1 of statements 7, 8 and 9 are true.
    If ((TF(7) + TF(8) + TF(9)) = 1) Then st(11) = 1

    '12.  Exactly 4 of the preceding statements are true.
    If ((TF(1) + TF(2) + TF(3) + TF(4) + TF(5) + TF(6) + TF(7) + TF(8) + TF(9) + TF(10) + TF(11)) = 4) Then st(12) = 1

    wrong = 0
    For i = 1 To 12
        If st(i) <> TF(i) Then wrong = wrong + 1
    Next
    If wrong = 0 Then
        For i = 1 To 12
            Print i; TF(i); st(i)
        Next
        Print "This matches the Binary of"; n
        Print
        Print "zzz... press any to continue"
        Sleep
        Cls
    End If
Next
Print "End of Run."
'$End If

'' test sub Dec2BinArr12
'VVVVVVVV mass comment method
$If  Then
        ReDim out12(0) As Long, k As Long, j As Long
        'For k = 4095 - 20 To 4095 ' << check tail end
        For k = 0 To 20 ' << check from end
        Dec2BinArr12 k, out12()
        Print k,
        For j = 1 To 12
        Print out12(j);
        Next
        Print
        Next
$End If

Sub Dec2BinArr12 (dec As Long, out12() As Long)
    ReDim out12(1 To 12) As Long, j As Long
    For j = 0 To 11
        If (dec And 2 ^ j) > 0 Then out12(j + 1) = 1
    Next
End Sub

BTW the solution is number 1133 converted to Binary.

PS This is first time I used $IF for mass commenting out code, very handy. Thanks @Pete
b = b + ...
Reply


Messages In This Thread
Rosetta Code Challenges - by bplus - 04-26-2022, 09:17 PM
RE: Rosetta Code Challenges - by SierraKen - 04-27-2022, 04:36 AM
RE: Rosetta Code Challenges - by bplus - 04-27-2022, 01:38 PM
RE: Rosetta Code Challenges - by bplus - 10-30-2022, 04:41 AM
RE: Rosetta Code Challenges - by bplus - 10-31-2022, 05:40 PM
RE: Rosetta Code Challenges - by SpriggsySpriggs - 10-31-2022, 07:24 PM
RE: Rosetta Code Challenges - by bplus - 10-31-2022, 09:09 PM
RE: Rosetta Code Challenges - by MasterGy - 10-31-2022, 09:33 PM
RE: Rosetta Code Challenges - by bplus - 10-31-2022, 09:43 PM
RE: Rosetta Code Challenges - by bplus - 11-01-2022, 02:07 AM
RE: Rosetta Code Challenges - by bplus - 11-01-2022, 12:19 PM
RE: Rosetta Code Challenges - by bplus - 11-05-2022, 04:57 AM
RE: Rosetta Code Challenges - by CharlieJV - 11-05-2022, 03:10 PM
RE: Rosetta Code Challenges - by bplus - 11-05-2022, 04:02 PM



Users browsing this thread: 4 Guest(s)