d05.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import "encoding/hex"
  3. import "crypto/md5"
  4. import "strconv"
  5. import "strings"
  6. import "fmt"
  7. import "os"
  8. func string2Md5(input string, incr int) string {
  9. inputBytes := []byte(input + strconv.Itoa(incr))
  10. hashed := make([]byte, 16)
  11. for i, j := range(md5.Sum(inputBytes)) {
  12. hashed[i] = j
  13. }
  14. result := hex.EncodeToString(hashed)
  15. return result
  16. }
  17. func ex1(doorId string) string {
  18. var result string
  19. for i := 0 ; len(result) < 8; i++ {
  20. hashed := (string2Md5(doorId, i))
  21. if (strings.HasPrefix(hashed, "00000")) {
  22. result += string(hashed[5])
  23. fmt.Printf(result)
  24. for j := len(result); j < 8; j++ {
  25. fmt.Printf("_")
  26. }
  27. fmt.Printf("\n")
  28. }
  29. }
  30. return result
  31. }
  32. func isDone(in []rune) bool {
  33. res := len(in)
  34. for i := 0; i < len(in); i++ {
  35. if in[i] != 0 {
  36. res--;
  37. }
  38. }
  39. return res == 0
  40. }
  41. func ex2(doorId string) string {
  42. result := make([]rune, 8)
  43. for i := 0 ; true ; i++ {
  44. hashed := (string2Md5(doorId, i))
  45. if (strings.HasPrefix(hashed, "00000")) {
  46. if (hashed[5] <= '7') {
  47. if (result[hashed[5] - '0'] == 0) {
  48. result[hashed[5] - '0'] = rune(hashed[6])
  49. for j := 0; j < 8; j++ {
  50. if result[j] == 0 {
  51. fmt.Printf("_")
  52. } else {
  53. fmt.Printf("%c", result[j])
  54. }
  55. }
  56. fmt.Printf("\n")
  57. if isDone(result) {
  58. return string(result)
  59. }
  60. }
  61. }
  62. }
  63. }
  64. return ""
  65. }
  66. func main() {
  67. if len(os.Args) != 2 {
  68. fmt.Fprintln(os.Stderr, "Usage: PROG doorId")
  69. } else {
  70. doorId := os.Args[1]
  71. ex1(doorId)
  72. }
  73. }