d07.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import "strconv"
  3. import "bufio"
  4. import "fmt"
  5. import "os"
  6. type Ipv7 struct {
  7. Raw string
  8. SupernetSequence []string
  9. HypernetSequence []string
  10. }
  11. func IsAbba(in string) bool {
  12. for i := 1; i < len(in) -2; i++ {
  13. if in[i +1] == in[i] && in[i +2] == in[i -1] && in[i -1] != in[i] {
  14. return true
  15. }
  16. }
  17. return false
  18. }
  19. func (this Ipv7) IsSupportingTLS() bool {
  20. for _, i := range(this.HypernetSequence) {
  21. if IsAbba(i) {
  22. return false
  23. }
  24. }
  25. for _, i := range(this.SupernetSequence) {
  26. if IsAbba(i) {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. func checkBab(BaB string, AbA []string) bool {
  33. for _, i:= range(AbA) {
  34. if BaB[0] == i[1] && BaB[1] == i[0] {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. func (this Ipv7) IsSupportingSSL() bool {
  41. var Aba []string
  42. for _, i := range(this.SupernetSequence) {
  43. for j := 1; j < len(i) -1; j++ {
  44. if (i[j -1] == i[j +1]) {
  45. Aba = append(Aba, i[j-1: j +1])
  46. }
  47. }
  48. }
  49. for _, i := range(this.HypernetSequence) {
  50. for j := 1; j < len(i) -1; j++ {
  51. if (i[j -1] == i[j +1]) {
  52. if checkBab(i[j -1:j +1], Aba) {
  53. return true
  54. }
  55. }
  56. }
  57. }
  58. return false
  59. }
  60. func NewIpv7(ip string) Ipv7 {
  61. var this Ipv7
  62. step := 0
  63. var tmp string
  64. this.Raw = ip
  65. for _, v := range(ip) {
  66. if v == '[' {
  67. if len(tmp) > 0 && step == 0 {
  68. this.SupernetSequence = append(this.SupernetSequence, tmp)
  69. tmp = ""
  70. }
  71. step = 1
  72. } else if v == ']' {
  73. if len(tmp) > 0 && step == 1 {
  74. this.HypernetSequence = append(this.HypernetSequence, tmp)
  75. tmp = ""
  76. }
  77. step = 0
  78. } else {
  79. tmp += string(v)
  80. }
  81. }
  82. if len(tmp) > 0 {
  83. if step == 0 {
  84. this.SupernetSequence = append(this.SupernetSequence, tmp)
  85. } else {
  86. this.HypernetSequence = append(this.HypernetSequence, tmp)
  87. }
  88. }
  89. return this
  90. }
  91. func main() {
  92. reader := bufio.NewReader(os.Stdin)
  93. tlsCount := 0
  94. sslCount := 0
  95. for true {
  96. line, err := reader.ReadString('\n')
  97. if len(line) == 0 {
  98. break
  99. }
  100. line = line[0:len(line)-1]
  101. ip := NewIpv7(line)
  102. if ip.IsSupportingTLS() {
  103. tlsCount++
  104. }
  105. if ip.IsSupportingSSL() {
  106. sslCount++
  107. }
  108. if err != nil {
  109. break
  110. }
  111. }
  112. fmt.Println("TLS: " +strconv.Itoa(tlsCount))
  113. fmt.Println("SSL: " +strconv.Itoa(sslCount))
  114. }