aoc2023/day-11/one.go
2023-12-11 18:57:25 +00:00

89 lines
1.7 KiB
Go

package main
import (
"math"
log "github.com/sirupsen/logrus"
)
var runes = [][]rune{}
type coords struct {
X int
Y int
}
type galaxy struct {
identifier int
coords
}
func newGalaxy(identifier, x, y int) galaxy {
return galaxy{identifier: identifier, coords: coords{X: x, Y: y}}
}
var galaxies = make([]galaxy, 0)
var empty = map[string][]int{
"row": {},
"col": {},
}
// shortestRoute calculates horizontal and vertical distances between points
// if empty row or columns exist then horz and vert are padded
// returns the sum of both distances
func shortestRoute(a, b coords, factor int) int {
horz := int(math.Abs(float64(b.X - a.X)))
vert := int(math.Abs(float64(b.Y - a.Y)))
for _, row := range empty["row"] {
if b.Y > a.Y {
if row >= a.Y && row < b.Y {
log.Debug("empty row, adding to vert")
vert += (factor - 1)
}
} else {
if row >= b.Y && row < a.Y {
log.Debug("empty row, adding to vert")
vert += (factor - 1)
}
}
}
for _, col := range empty["col"] {
if b.X > a.X {
if col >= a.X && col < b.X {
log.Debug("empty col, adding to horz")
horz += (factor - 1)
}
} else {
if col >= b.X && col < a.X {
log.Debug("empty col, adding to horz")
horz += (factor - 1)
}
}
}
return int(math.Abs(float64(horz + vert)))
}
// one returns the sum of all shortest distances between galaxies
func one(lines []string) int {
parseInput(lines)
compared := [][]int{}
sum := 0
for i := range galaxies {
for j := range galaxies {
if i == j || inCompared(i, j, compared) {
continue
}
dist := shortestRoute(galaxies[i].coords, galaxies[j].coords, 2)
compared = append(compared, []int{galaxies[i].identifier, galaxies[j].identifier})
sum += dist
}
}
return sum
}