code.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type point struct {
  9. x int
  10. y int
  11. }
  12. func readInput(file *os.File) [][2]point {
  13. scanner := bufio.NewScanner(file)
  14. var points [][2]point
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. continue
  19. }
  20. var current [2]point
  21. n, err := fmt.Sscanf(line, "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", &current[0].x, &current[0].y, &current[1].x, &current[1].y)
  22. if n != 4 || err != nil {
  23. log.Fatal("Can't parse", line, err)
  24. }
  25. points = append(points, current)
  26. }
  27. return points
  28. }
  29. func abs(x int) int {
  30. if x < 0 {
  31. return 0 - x
  32. }
  33. return x
  34. }
  35. func getDistance(sensor point, beacon point) int {
  36. return abs(sensor.x-beacon.x) + abs(sensor.y-beacon.y)
  37. }
  38. func markOccupied(sensor point, distance int, occupied map[int]map[int]byte) {
  39. end := sensor.y - distance
  40. width := distance
  41. for i := sensor.y; i >= end; i-- {
  42. if occupied[i] == nil {
  43. occupied[i] = make(map[int]byte)
  44. }
  45. for j := sensor.x - width; j <= sensor.x+width; j++ {
  46. occupied[i][j] = '#'
  47. }
  48. width--
  49. }
  50. width = distance - 1
  51. end = sensor.y + distance
  52. for i := sensor.y + 1; i <= end; i++ {
  53. if occupied[i] == nil {
  54. occupied[i] = make(map[int]byte)
  55. }
  56. for j := sensor.x - width; j <= sensor.x+width; j++ {
  57. occupied[i][j] = '#'
  58. }
  59. width--
  60. }
  61. }
  62. func drawLines(points [][2]point) map[int]map[int]byte {
  63. occupied := make(map[int]map[int]byte)
  64. for i := range points {
  65. if occupied[points[i][0].y] == nil {
  66. occupied[points[i][0].y] = make(map[int]byte)
  67. }
  68. occupied[points[i][0].y][points[i][0].x] = 's'
  69. if occupied[points[i][1].y] == nil {
  70. occupied[points[i][1].y] = make(map[int]byte)
  71. }
  72. occupied[points[i][1].y][points[i][1].x] = 'b'
  73. distance := getDistance(points[i][0], points[i][1])
  74. markOccupied(points[i][0], distance, occupied)
  75. }
  76. return occupied
  77. }
  78. func main() {
  79. if len(os.Args) < 2 {
  80. log.Fatal("You need to specify a file!")
  81. }
  82. filePath := os.Args[1]
  83. file, err := os.Open(filePath)
  84. if err != nil {
  85. log.Fatalf("Failed to open %s!\n", filePath)
  86. }
  87. points := readInput(file)
  88. fmt.Println(drawLines(points))
  89. }