Advertisement
Guest User

day 6

a guest
Dec 15th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.90 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "math"
  7.     "os"
  8.     "strconv"
  9.     "strings"
  10. )
  11.  
  12. var part2Area = 0
  13.  
  14. func getClosestPoint(x int, y int, coordMap map[int][]int) int {
  15.     id := 0
  16.     sumOfDistances := 0
  17.     minDistance := -1.0
  18.     for key, xy := range coordMap {
  19.         distance := math.Abs(float64(x-xy[0])) + math.Abs(float64(y-xy[1]))
  20.         sumOfDistances += int(distance)
  21.         if distance < minDistance || minDistance == -1 {
  22.             minDistance = distance
  23.             id = key
  24.         } else if distance == minDistance { //equal distance to 2 points
  25.             id = -1
  26.         }
  27.     }
  28.     if sumOfDistances < 10000 {
  29.         part2Area++
  30.     }
  31.     return id
  32. }
  33.  
  34. func main() {
  35.     coordinates := make(map[int][]int)
  36.  
  37.     fileHandle, _ := os.Open("day6.txt")
  38.     scanner := bufio.NewScanner(fileHandle)
  39.  
  40.     minX := -1
  41.     minY := -1
  42.     maxX := 0
  43.     maxY := 0
  44.  
  45.     //read coordinates into map and determine the matrix size
  46.     i := 0
  47.     for scanner.Scan() {
  48.         tempSlice := strings.Split(scanner.Text(), ", ")
  49.         x, _ := strconv.Atoi(tempSlice[0])
  50.         y, _ := strconv.Atoi(tempSlice[1])
  51.         if x < minX || minX == -1 {minX = x}
  52.         if x > maxX {maxX = x}
  53.         if y < minY || minY == -1 {minY = y}
  54.         if y > maxY {maxY = y}
  55.         coordinates[i] = []int{x,y}
  56.         i++
  57.     }
  58.  
  59.     //normalize coordinates
  60.     //TODO this is probably redundant
  61.     for _, xy := range coordinates {
  62.         xy[0] -= minX
  63.         xy[1] -= minY
  64.     }
  65.  
  66.     //calculate values and add up area size
  67.     counter := make([]int, len(coordinates))
  68.     for x := 0; x < maxX - minX + 1; x++ {
  69.         for y := 0; y < maxY - minY + 1; y++ {
  70.             value := getClosestPoint(x, y, coordinates)
  71.             //if not exactly in the middle two origins
  72.             if value != -1 {
  73.                 //if area is not already disqualified
  74.                 if counter[value] > -1 {
  75.                     counter[value] ++
  76.                 }
  77.                 //area gets disqualified if it touches the edge
  78.                 if x == 0 || x == maxX - minX || y == 0 || y == maxY - minY {
  79.                     counter[value] = -1
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     fmt.Println(counter)
  86.     fmt.Println(part2Area)
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement