Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "math"
- "os"
- "strconv"
- "strings"
- )
- var part2Area = 0
- func getClosestPoint(x int, y int, coordMap map[int][]int) int {
- id := 0
- sumOfDistances := 0
- minDistance := -1.0
- for key, xy := range coordMap {
- distance := math.Abs(float64(x-xy[0])) + math.Abs(float64(y-xy[1]))
- sumOfDistances += int(distance)
- if distance < minDistance || minDistance == -1 {
- minDistance = distance
- id = key
- } else if distance == minDistance { //equal distance to 2 points
- id = -1
- }
- }
- if sumOfDistances < 10000 {
- part2Area++
- }
- return id
- }
- func main() {
- coordinates := make(map[int][]int)
- fileHandle, _ := os.Open("day6.txt")
- scanner := bufio.NewScanner(fileHandle)
- minX := -1
- minY := -1
- maxX := 0
- maxY := 0
- //read coordinates into map and determine the matrix size
- i := 0
- for scanner.Scan() {
- tempSlice := strings.Split(scanner.Text(), ", ")
- x, _ := strconv.Atoi(tempSlice[0])
- y, _ := strconv.Atoi(tempSlice[1])
- if x < minX || minX == -1 {minX = x}
- if x > maxX {maxX = x}
- if y < minY || minY == -1 {minY = y}
- if y > maxY {maxY = y}
- coordinates[i] = []int{x,y}
- i++
- }
- //normalize coordinates
- //TODO this is probably redundant
- for _, xy := range coordinates {
- xy[0] -= minX
- xy[1] -= minY
- }
- //calculate values and add up area size
- counter := make([]int, len(coordinates))
- for x := 0; x < maxX - minX + 1; x++ {
- for y := 0; y < maxY - minY + 1; y++ {
- value := getClosestPoint(x, y, coordinates)
- //if not exactly in the middle two origins
- if value != -1 {
- //if area is not already disqualified
- if counter[value] > -1 {
- counter[value] ++
- }
- //area gets disqualified if it touches the edge
- if x == 0 || x == maxX - minX || y == 0 || y == maxY - minY {
- counter[value] = -1
- }
- }
- }
- }
- fmt.Println(counter)
- fmt.Println(part2Area)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement