mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
74 lines
1.5 KiB
Go
74 lines
1.5 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 row >= a.Y && row < b.Y || row >= b.Y && row < a.Y {
|
|
log.Debug("empty row, adding to vert")
|
|
vert += (factor - 1)
|
|
}
|
|
}
|
|
for _, col := range empty["col"] {
|
|
if col >= a.X && col < b.X || 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
|
|
}
|
|
|
|
compared = append(compared, []int{galaxies[i].identifier, galaxies[j].identifier})
|
|
sum += shortestRoute(galaxies[i].coords, galaxies[j].coords, 2)
|
|
}
|
|
}
|
|
|
|
return sum
|
|
}
|