adventofcode/2018/day25/day25.go

76 lines
1.2 KiB
Go
Raw Normal View History

2019-05-29 20:19:18 +00:00
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
2019-11-08 21:22:03 +00:00
inp := StdinToStringSlice()
fmt.Println(HotChocolatePortal(inp))
2019-05-29 20:19:18 +00:00
}
2019-11-08 21:22:03 +00:00
func HotChocolatePortal(input []string) int {
var constellations []Constellation
2019-05-29 20:19:18 +00:00
2019-11-08 21:22:03 +00:00
for _, line := range input {
var c Coordinate
2019-05-29 20:19:18 +00:00
2019-11-08 21:22:03 +00:00
if _, err := fmt.Sscanf(line, "%d,%d,%d,%d", &c[0], &c[1], &c[2], &c[3]); err != nil {
panic(err)
}
2019-05-29 20:19:18 +00:00
2019-11-08 21:22:03 +00:00
var found bool
for _, constellation := range constellations {
if constellation.Distance(c) <= 3 {
constellation.Add(c)
found = true
break
2019-05-29 20:19:18 +00:00
}
}
2019-11-08 21:22:03 +00:00
if !found {
constellations = append(constellations, NewConstellation(c))
}
2019-05-29 20:19:18 +00:00
}
2019-11-08 21:22:03 +00:00
var nc []Constellation
for i, a := range constellations {
merged := false
2019-05-29 20:19:18 +00:00
2019-11-08 21:22:03 +00:00
for j, b := range constellations {
if i == j {
continue
}
2019-05-29 20:19:18 +00:00
2019-11-08 21:22:03 +00:00
if b.CelestialDistance(a) <= 3 {
b.Merge(a)
merged = true
break
}
}
2019-05-29 20:19:18 +00:00
2019-11-08 21:22:03 +00:00
if !merged {
nc = append(nc, a)
2019-05-29 20:19:18 +00:00
}
}
2019-11-08 21:22:03 +00:00
return len(nc)
2019-05-29 20:19:18 +00:00
}
2019-11-08 21:22:03 +00:00
func abs(num int) int {
if num < 0 {
return num * -1
}
return num
2019-05-29 20:19:18 +00:00
}
2019-11-08 21:22:03 +00:00
func StdinToStringSlice() []string {
2019-05-29 20:19:18 +00:00
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}