Task: Twelve statements




Go: package main

import "fmt"


var solution = make(chan int)
var nearMiss = make(chan int)
var done = make(chan bool)

func main() {
    
    for i := 0; i < 4096; i++ {
        go checkPerm(i)
    }
    
    var ms []int
    for i := 0; i < 4096; {
        select {
        case <-done:
            i++
        case s := <-solution:
            print12("solution", s)
        case m := <-nearMiss:
            ms = append(ms, m)
        }
    }
    for _, m := range ms {
        print12("near miss", m)
    }
}

func print12(label string, bits int) {
    fmt.Print(label, ":")
    for i := 1; i <= 12; i++ {
        if bits&1 == 1 {
            fmt.Print(" ", i)
        }
        bits >>= 1
    }
    fmt.Println()
}

func checkPerm(tz int) {
    
    
    ts := func(n uint) bool {
        return tz>>(n-1)&1 == 1
    }
    
    
    ntrue := func(xs ...uint) int {
        nt := 0
        for _, x := range xs {
            if ts(x) {
                nt++
            }
        }
        return nt
    }
    
    
    
    
    var con bool
    
    test := func(statement uint, b bool) {
        switch {
        case ts(statement) == b:
        case con:
            panic("bail")
        default:
            con = true
        }
    }
    
    defer func() {
        if x := recover(); x != nil {
            if msg, ok := x.(string); !ok && msg != "bail" {
                panic(x)
            }
        }
        done <- true
    }()

    
    test(1, true)

    
    test(2, ntrue(7, 8, 9, 10, 11, 12) == 3)

    
    test(3, ntrue(2, 4, 6, 8, 10, 12) == 2)

    
    test(4, !ts(5) || ts(6) && ts(7))

    
    test(5, !ts(4) && !ts(3) && !ts(2))

    
    test(6, ntrue(1, 3, 5, 7, 9, 11) == 4)

    
    test(7, ts(2) != ts(3))

    
    test(8, !ts(7) || ts(5) && ts(6))

    
    test(9, ntrue(1, 2, 3, 4, 5, 6) == 3)
    
    
    test(10, ts(11) && ts(12))
    
    
    test(11, ntrue(7, 8, 9) == 1)
    
    
    test(12, ntrue(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) == 4)

    
    if con {
        nearMiss <- tz
    } else {
        solution <- tz
    }
}





VB: Public s As String    
Public t As Integer   
 
Function s1()
    s1 = Len(s) = 12
End Function
Function s2()
    t = 0
    For i = 7 To 12
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s2 = t = 3
End Function
Function s3()
    t = 0
    For i = 2 To 12 Step 2
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s3 = t = 2
End Function
Function s4()
    s4 = Mid(s, 5, 1) = "0" Or ((Mid(s, 6, 1) = "1" And Mid(s, 7, 1) = "1"))
End Function
Function s5()
    s5 = Mid(s, 2, 1) = "0" And Mid(s, 3, 1) = "0" And Mid(s, 4, 1) = "0"
End Function
Function s6()
    t = 0
    For i = 1 To 12 Step 2
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s6 = t = 4
End Function
Function s7()
    s7 = Mid(s, 2, 1) <> Mid(s, 3, 1)
End Function
Function s8()
    s8 = Mid(s, 7, 1) = "0" Or (Mid(s, 5, 1) = "1" And Mid(s, 6, 1) = "1")
End Function
Function s9()
    t = 0
    For i = 1 To 6
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s9 = t = 3
End Function
Function s10()
    s10 = Mid(s, 11, 1) = "1" And Mid(s, 12, 1) = "1"
End Function
Function s11()
    t = 0
    For i = 7 To 9
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s11 = t = 1
End Function
Function s12()
    t = 0
    For i = 1 To 11
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s12 = t = 4
End Function
 
Public Sub twelve_statements()
    For i = 0 To 2 ^ 12 - 1
        s = Right(CStr(WorksheetFunction.Dec2Bin(64 + i \ 128)), 5) _
            & Right(CStr(WorksheetFunction.Dec2Bin(256 + i Mod 128)), 7)
        For b = 1 To 12
            Select Case b
                Case 1: If s1 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 2: If s2 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 3: If s3 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 4: If s4 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 5: If s5 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 6: If s6 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 7: If s7 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 8: If s8 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 9: If s9 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 10: If s10 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 11: If s11 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 12: If s12 <> (Mid(s, b, 1) = "1") Then Exit For
            End Select
            If b = 12 Then Debug.Print s
        Next
    Next
End Sub