code.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "math"
  7. "os"
  8. )
  9. type box struct {
  10. x, y, z int
  11. id, best int
  12. inCircuit bool
  13. }
  14. func (p box) DistanceTo(other box) float64 {
  15. dx := other.x - p.x
  16. dy := other.y - p.y
  17. dz := other.z - p.z
  18. return math.Sqrt(float64(dx*dx + dy*dy + dz*dz))
  19. }
  20. func readInput(file *os.File) []box {
  21. scanner := bufio.NewScanner(file)
  22. var boxes []box
  23. var id int
  24. for scanner.Scan() {
  25. line := scanner.Text()
  26. if line == "" {
  27. continue
  28. }
  29. var x, y, z int
  30. n, err := fmt.Sscanf(line, "%d,%d,%d", &x, &y, &z)
  31. if n != 3 || err != nil {
  32. log.Fatalf("Bad input: %s", line)
  33. }
  34. boxes = append(boxes, box{x: x, y: y, z: z, id: id})
  35. id++
  36. }
  37. return boxes
  38. }
  39. type circuit struct {
  40. first, second int
  41. boxes []box
  42. }
  43. type distance struct {
  44. first, second int
  45. value float64
  46. }
  47. func part1(boxes []box) int {
  48. var result int
  49. var distances []distance
  50. for i := range boxes {
  51. for j := i + 1; j < len(boxes); j++ {
  52. howFar := boxes[j].DistanceTo(boxes[i])
  53. distances = append(distances, distance{first: boxes[i].id, second: boxes[j].id, value: howFar})
  54. }
  55. }
  56. fmt.Println(distances)
  57. return result
  58. }
  59. func main() {
  60. if len(os.Args) < 2 {
  61. log.Fatal("You need to specify a file!")
  62. }
  63. filePath := os.Args[1]
  64. file, err := os.Open(filePath)
  65. if err != nil {
  66. log.Fatalf("Failed to open %s!\n", filePath)
  67. }
  68. boxes := readInput(file)
  69. part1(boxes)
  70. }