Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. package main
  2.  
  3. import(
  4. "os"
  5. "bufio"
  6. "errors"
  7. "fmt"
  8. )
  9.  
  10. func main() {
  11. wordlist, err := getWordlistFrom("wordlist.txt")
  12. if err != nil {
  13. fmt.Println(err)
  14. return
  15. }
  16.  
  17. start, end, err := getUserInput()
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22.  
  23. ladder, err := getWordLadder(start, end, wordlist)
  24. if err != nil {
  25. fmt.Println(err)
  26. return
  27. }
  28.  
  29. for _, word := range ladder {
  30. println(word)
  31. }
  32. }
  33.  
  34. func getWordLadder(start, end string, wordlist []string) ([]string, error) {
  35. ladder := []string{start}
  36.  
  37. for step := 1; step <= 4; step++ {
  38. next := ""
  39.  
  40. for _, word := range wordlist {
  41. matchesWithStart := getMatchingCharsBetweenStrings(start, word)
  42. matchesWithEnd := getMatchingCharsBetweenStrings(end, word)
  43.  
  44. if matchesWithStart == len(start) - step && matchesWithEnd == step {
  45. next = word
  46. break
  47. }
  48. }
  49.  
  50. if len(next) == 0 {
  51. return nil, errors.New("Word ladder not possible with current wordlist")
  52. }
  53.  
  54. ladder = append(ladder, next)
  55. }
  56.  
  57. ladder = append(ladder, end)
  58.  
  59. return ladder, nil
  60. }
  61.  
  62. func getUserInput() (string, string, error) {
  63. if len(os.Args) != 3 {
  64. return "", "", errors.New("Invalid number of arguments")
  65. }
  66.  
  67. return os.Args[1], os.Args[2], nil
  68. }
  69.  
  70. func getWordlistFrom(path string) ([]string, error) {
  71. file, err := os.Open(path)
  72. if err != nil {
  73. return nil, err
  74. }
  75.  
  76. defer file.Close()
  77.  
  78. var lines []string
  79. scanner := bufio.NewScanner(file)
  80. for scanner.Scan() {
  81. lines = append(lines, scanner.Text())
  82. }
  83.  
  84. return lines, scanner.Err()
  85. }
  86.  
  87. func getMatchingCharsBetweenStrings(a, b string) (matches int) {
  88. for _, charA := range a {
  89. for _, charB := range b {
  90. if charA == charB {
  91. matches++
  92. }
  93. }
  94. }
  95.  
  96. return
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement