code.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "sort"
  8. )
  9. type Object struct {
  10. xMin, yMin, zMin int
  11. xMax, yMax, zMax int
  12. zRealMin, zRealMax int
  13. supports []*Object
  14. standsOn []*Object
  15. }
  16. func readInput(file *os.File) []Object {
  17. scanner := bufio.NewScanner(file)
  18. var objects []Object
  19. for scanner.Scan() {
  20. line := scanner.Text()
  21. if line == "" {
  22. break
  23. }
  24. var object Object
  25. n, err := fmt.Sscanf(line, "%d,%d,%d~%d,%d,%d", &object.xMin, &object.yMin, &object.zMin, &object.xMax, &object.yMax, &object.zMax)
  26. if n != 6 || err != nil {
  27. log.Fatalf("Bad input: %s\n%s", line, err)
  28. }
  29. objects = append(objects, object)
  30. }
  31. return objects
  32. }
  33. func intersect(a, b Object) bool {
  34. return a.xMin <= b.xMax && a.xMax >= b.xMin && a.yMin <= b.yMax && a.yMax >= b.yMin
  35. }
  36. func part1(objects []Object) int {
  37. var result int
  38. sort.Slice(objects, func(i, j int) bool { return objects[i].zMin < objects[j].zMin })
  39. fmt.Println(objects)
  40. fmt.Println(objects[1], objects[2], intersect(objects[1], objects[2]))
  41. return result
  42. }
  43. func main() {
  44. if len(os.Args) < 2 {
  45. log.Fatal("You need to specify a file!")
  46. }
  47. filePath := os.Args[1]
  48. file, err := os.Open(filePath)
  49. if err != nil {
  50. log.Fatalf("Failed to open %s!\n", filePath)
  51. }
  52. objects := readInput(file)
  53. fmt.Println("Part1:", part1(objects))
  54. }