aboutsummaryrefslogtreecommitdiff
path: root/challenge-135/abigail/go/ch-2.go
blob: bb8e636c00618d5259ff40c1b2844829c0ff3daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

//
// See ../README.md
//

//
// Run as: go run ch-2.go
//

import (
    "fmt"
    "bufio"
    "os"
    "strings"
    "regexp"
)

func main () {
    var reader = bufio . NewReader (os. Stdin)
    var w      = [] rune {1, 3, 1, 7, 3, 9, 1}
    for {
        var text, err = reader . ReadString ('\n')
        if (err != nil) {
            break
        }

        //
        // Remove trailing newline
        //
        text = strings . TrimRight (text, "\n")

        //
        // Syntax check
        //
        match, _ := regexp . MatchString (
            "^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$", text)

        if !match {
            fmt . Print ("0\n")
            continue
        }

        //
        // Check sum
        //
        var check rune = 0
        for i, rune := range text {
            if rune <= '9' {
                rune -= '0'
            } else {
                rune -= 'A'
            }
            check += w [i] * rune
        }
        if check % 10 == 0 {
            fmt . Print ("1\n")
        } else {
            fmt . Print ("0\n")
        }
    }
}