-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (110 loc) · 2.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
_ "embed"
"fmt"
"log"
"math"
"strconv"
"strings"
. "github.com/beanz/advent/lib-go"
)
//go:embed input.txt
var input []byte
type Star struct {
x, y, z, t int
}
func (s Star) String() string {
return fmt.Sprintf("[%d,%d,%d,%d]", s.x, s.y, s.z, s.t)
}
func (s Star) ManhattanDistance(other Star) int {
return int(math.Abs(float64(s.x)-float64(other.x)) +
math.Abs(float64(s.y)-float64(other.y)) +
math.Abs(float64(s.z)-float64(other.z)) +
math.Abs(float64(s.t)-float64(other.t)))
}
type Constellation []Star
func (c Constellation) String() string {
r := []string{}
for _, s := range c {
r = append(r, s.String())
}
return strings.Join(r, " ")
}
func (c Constellation) InConstellation(s Star) bool {
for _, ss := range c {
if s.ManhattanDistance(ss) <= 3 {
return true
}
}
return false
}
type Game struct {
stars []Star
constellations map[int]Constellation
debug bool
}
func (g *Game) String() string {
r := ""
for i, c := range g.constellations {
r += fmt.Sprintf("%d: %s\n", i, c)
}
return r
}
func readInts(intStrings []string) ([]int, error) {
nums := make([]int, 0, len(intStrings))
for _, intString := range intStrings {
n, err := strconv.Atoi(intString)
if err != nil {
return nil, err
}
nums = append(nums, n)
}
return nums, nil
}
func readGame(input string) *Game {
lines := strings.Split(input, "\n")
stars := []Star{}
for _, line := range lines[:len(lines)-1] {
ints, err := readInts(strings.Split(line, ","))
if err != nil {
log.Fatalf("error parsing ints '%s': %s\n", line, err)
}
stars = append(stars, Star{ints[0], ints[1], ints[2], ints[3]})
}
constellations := make(map[int]Constellation)
return &Game{stars, constellations, false}
}
func (g *Game) Part1() int {
for k, s := range g.stars {
possibleKeys := []int{}
// highest first to make removal not corrupt indices
for i, c := range g.constellations {
if c.InConstellation(s) {
possibleKeys = append(possibleKeys, i)
}
}
if len(possibleKeys) == 0 {
g.constellations[k] = Constellation{s}
} else {
g.constellations[possibleKeys[0]] =
append(g.constellations[possibleKeys[0]], s)
for i := 1; i < len(possibleKeys); i++ {
//fmt.Printf("Combining constellation %d with %d\n",
// possibleKeys[0], possibleKeys[i])
g.constellations[possibleKeys[0]] =
append(g.constellations[possibleKeys[0]],
g.constellations[possibleKeys[i]]...)
delete(g.constellations, possibleKeys[i])
}
}
}
return len(g.constellations)
}
func main() {
game := readGame(InputString(input))
res := game.Part1()
if !benchmark {
fmt.Printf("Part 1: %d\n", res)
}
}
var benchmark = false