|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var inputFile = flag.String("inputFile", "inputs/day25.input", "Relative file path to use as input.") |
| 13 | + |
| 14 | +type Coord4 struct { |
| 15 | + X, Y, Z, T int |
| 16 | +} |
| 17 | + |
| 18 | +func (c Coord4) Adjacent(t Coord4) bool { |
| 19 | + deltaX := c.X - t.X |
| 20 | + deltaY := c.Y - t.Y |
| 21 | + deltaZ := c.Z - t.Z |
| 22 | + deltaT := c.T - t.T |
| 23 | + |
| 24 | + sum := 0 |
| 25 | + if deltaX < 0 { |
| 26 | + sum -= deltaX |
| 27 | + } else { |
| 28 | + sum += deltaX |
| 29 | + } |
| 30 | + if deltaY < 0 { |
| 31 | + sum -= deltaY |
| 32 | + } else { |
| 33 | + sum += deltaY |
| 34 | + } |
| 35 | + if deltaZ < 0 { |
| 36 | + sum -= deltaZ |
| 37 | + } else { |
| 38 | + sum += deltaZ |
| 39 | + } |
| 40 | + if deltaT < 0 { |
| 41 | + sum -= deltaT |
| 42 | + } else { |
| 43 | + sum += deltaT |
| 44 | + } |
| 45 | + |
| 46 | + return sum <= 3 |
| 47 | +} |
| 48 | + |
| 49 | +func main() { |
| 50 | + flag.Parse() |
| 51 | + f, err := os.Open(*inputFile) |
| 52 | + if err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + defer f.Close() |
| 56 | + |
| 57 | + coords := make([]Coord4, 0) |
| 58 | + r := bufio.NewReader(f) |
| 59 | + for { |
| 60 | + l, err := r.ReadString('\n') |
| 61 | + if err != nil || len(l) == 0 { |
| 62 | + break |
| 63 | + } |
| 64 | + l = l[:len(l)-1] |
| 65 | + nums := strings.Split(l, ",") |
| 66 | + x, _ := strconv.Atoi(nums[0]) |
| 67 | + y, _ := strconv.Atoi(nums[1]) |
| 68 | + z, _ := strconv.Atoi(nums[2]) |
| 69 | + t, _ := strconv.Atoi(nums[3]) |
| 70 | + coords = append(coords, Coord4{x, y, z, t}) |
| 71 | + } |
| 72 | + |
| 73 | + // Maps index of coord to clique number. |
| 74 | + cliquesByCoord := make(map[int]int) |
| 75 | + coordsByClique := make(map[int][]int) |
| 76 | + |
| 77 | + // Seed the first element. |
| 78 | + highestClique := 1 |
| 79 | + |
| 80 | + changed := true |
| 81 | + for changed { |
| 82 | + changed = false |
| 83 | + for c := range coords { |
| 84 | + cClique := cliquesByCoord[c] |
| 85 | + if cClique == 0 { |
| 86 | + cliquesByCoord[c] = highestClique |
| 87 | + coordsByClique[highestClique] = []int{c} |
| 88 | + cClique = highestClique |
| 89 | + highestClique++ |
| 90 | + changed = true |
| 91 | + } |
| 92 | + for t := range coords { |
| 93 | + tClique := cliquesByCoord[t] |
| 94 | + if tClique == cClique { |
| 95 | + continue |
| 96 | + } |
| 97 | + if coords[c].Adjacent(coords[t]) { |
| 98 | + if tClique == 0 { |
| 99 | + cliquesByCoord[t] = cClique |
| 100 | + coordsByClique[cClique] = append(coordsByClique[cClique], t) |
| 101 | + changed = true |
| 102 | + continue |
| 103 | + } |
| 104 | + // Merge the t Clique and the c Clique. |
| 105 | + changed = true |
| 106 | + for _, v := range coordsByClique[tClique] { |
| 107 | + cliquesByCoord[v] = cClique |
| 108 | + } |
| 109 | + coordsByClique[cClique] = append(coordsByClique[cClique], coordsByClique[tClique]...) |
| 110 | + delete(coordsByClique, tClique) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + fmt.Println(len(coordsByClique)) |
| 116 | +} |
0 commit comments