code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, row int) {
  39. width := distance - abs(row-sensor.y)
  40. if occupied[row] == nil {
  41. occupied[row] = make(map[int]byte)
  42. }
  43. for j := sensor.x - width; j <= sensor.x+width; j++ {
  44. _, ok := occupied[row][j]
  45. if !ok {
  46. occupied[row][j] = '#'
  47. }
  48. }
  49. }
  50. func drawLines(points [][2]point, row int) map[int]map[int]byte {
  51. occupied := make(map[int]map[int]byte)
  52. for i := range points {
  53. if occupied[points[i][0].y] == nil {
  54. occupied[points[i][0].y] = make(map[int]byte)
  55. }
  56. occupied[points[i][0].y][points[i][0].x] = 's'
  57. if occupied[points[i][1].y] == nil {
  58. occupied[points[i][1].y] = make(map[int]byte)
  59. }
  60. occupied[points[i][1].y][points[i][1].x] = 'b'
  61. distance := getDistance(points[i][0], points[i][1])
  62. markOccupied(points[i][0], distance, occupied, row)
  63. }
  64. return occupied
  65. }
  66. func filterPoints(points [][2]point, row int) [][2]point {
  67. var filtered [][2]point
  68. for i := range points {
  69. distance := getDistance(points[i][0], points[i][1])
  70. if row >= points[i][0].y-distance && row <= points[i][0].y+distance {
  71. filtered = append(filtered, points[i])
  72. }
  73. }
  74. return filtered
  75. }
  76. func part1(points [][2]point, row int) int {
  77. filtered := filterPoints(points, row)
  78. occupied := drawLines(filtered, row)
  79. count := 0
  80. for _, value := range occupied[row] {
  81. if value == '#' {
  82. count++
  83. }
  84. }
  85. return count
  86. }
  87. func main() {
  88. if len(os.Args) < 2 {
  89. log.Fatal("You need to specify a file!")
  90. }
  91. filePath := os.Args[1]
  92. file, err := os.Open(filePath)
  93. if err != nil {
  94. log.Fatalf("Failed to open %s!\n", filePath)
  95. }
  96. points := readInput(file)
  97. fmt.Println("Part1:", part1(points, 2000000))
  98. }