|
| 1 | +/* |
| 2 | +--- Day 11: Cosmic Expansion --- |
| 3 | +You continue following signs for "Hot Springs" and eventually come across |
| 4 | +an observatory. The Elf within turns out to be a researcher studying cosmic |
| 5 | +expansion using the giant telescope here. |
| 6 | +
|
| 7 | +He doesn't know anything about the missing machine parts; he's only visiting for |
| 8 | +this research project. However, he confirms that the hot springs are the next-closest |
| 9 | +area likely to have people; he'll even take you straight there once he's done |
| 10 | +with today's observation analysis. |
| 11 | +
|
| 12 | +Maybe you can help him with the analysis to speed things up? |
| 13 | +
|
| 14 | +The researcher has collected a bunch of data and compiled the data into a single |
| 15 | +giant image (your puzzle input). |
| 16 | +The image includes empty space (.) and galaxies (#). For example: |
| 17 | +
|
| 18 | +...#...... |
| 19 | +.......#.. |
| 20 | +#......... |
| 21 | +.......... |
| 22 | +......#... |
| 23 | +.#........ |
| 24 | +.........# |
| 25 | +.......... |
| 26 | +.......#.. |
| 27 | +#...#..... |
| 28 | +The researcher is trying to figure out the sum of the lengths of the shortest path |
| 29 | +between every pair of galaxies. However, there's a catch: the universe expanded |
| 30 | +in the time it took the light from those galaxies to reach the observatory. |
| 31 | +
|
| 32 | +Due to something involving gravitational effects, only some space expands. |
| 33 | +In fact, the result is that any rows or columns that contain no galaxies should |
| 34 | +all actually be twice as big. |
| 35 | +
|
| 36 | +In the above example, three columns and two rows contain no galaxies: |
| 37 | +
|
| 38 | + v v v |
| 39 | + ...#...... |
| 40 | + .......#.. |
| 41 | + #......... |
| 42 | +>..........< |
| 43 | + ......#... |
| 44 | + .#........ |
| 45 | + .........# |
| 46 | +>..........< |
| 47 | + .......#.. |
| 48 | + #...#..... |
| 49 | + ^ ^ ^ |
| 50 | +
|
| 51 | +These rows and columns need to be twice as big; the result of cosmic expansion |
| 52 | +therefore looks like this: |
| 53 | +
|
| 54 | +....#........ |
| 55 | +.........#... |
| 56 | +#............ |
| 57 | +............. |
| 58 | +............. |
| 59 | +........#.... |
| 60 | +.#........... |
| 61 | +............# |
| 62 | +............. |
| 63 | +............. |
| 64 | +.........#... |
| 65 | +#....#....... |
| 66 | +
|
| 67 | +Equipped with this expanded universe, the shortest path between every pair of |
| 68 | +galaxies can be found. It can help to assign every galaxy a unique number: |
| 69 | +
|
| 70 | +....1........ |
| 71 | +.........2... |
| 72 | +3............ |
| 73 | +............. |
| 74 | +............. |
| 75 | +........4.... |
| 76 | +.5........... |
| 77 | +............6 |
| 78 | +............. |
| 79 | +............. |
| 80 | +.........7... |
| 81 | +8....9....... |
| 82 | +
|
| 83 | +In these 9 galaxies, there are 36 pairs. Only count each pair once; |
| 84 | +order within the pair doesn't matter. For each pair, find any shortest |
| 85 | +path between the two galaxies using only steps that move up, down, left, |
| 86 | +or right exactly one . or # at a time. (The shortest path between |
| 87 | +two galaxies is allowed to pass through another galaxy.) |
| 88 | +
|
| 89 | +For example, here is one of the shortest paths between galaxies 5 and 9: |
| 90 | +
|
| 91 | +....1........ |
| 92 | +.........2... |
| 93 | +3............ |
| 94 | +............. |
| 95 | +............. |
| 96 | +........4.... |
| 97 | +.5........... |
| 98 | +.##.........6 |
| 99 | +..##......... |
| 100 | +...##........ |
| 101 | +....##...7... |
| 102 | +8....9....... |
| 103 | +This path has length 9 because it takes a minimum of nine steps to get from galaxy |
| 104 | +5 to galaxy 9 (the eight locations marked # plus the step onto galaxy 9 itself). |
| 105 | +Here are some other example shortest path lengths: |
| 106 | +
|
| 107 | +Between galaxy 1 and galaxy 7: 15 |
| 108 | +Between galaxy 3 and galaxy 6: 17 |
| 109 | +Between galaxy 8 and galaxy 9: 5 |
| 110 | +
|
| 111 | +In this example, after expanding the universe, the sum of the shortest |
| 112 | +path between all 36 pairs of galaxies is 374. |
| 113 | +
|
| 114 | +Expand the universe, then find the length of the shortest path between |
| 115 | +every pair of galaxies. What is the sum of these lengths? |
| 116 | +
|
| 117 | +--- Part Two --- |
| 118 | +The galaxies are much older (and thus much farther apart) |
| 119 | +than the researcher initially estimated. |
| 120 | +
|
| 121 | +Now, instead of the expansion you did before, make each empty row or column one million times larger. |
| 122 | +That is, each empty row should be replaced with 1000000 empty rows, |
| 123 | +and each empty column should be replaced with 1000000 empty columns. |
| 124 | +
|
| 125 | +(In the example above, if each empty row or column were merely 10 times larger, |
| 126 | +the sum of the shortest paths between every pair of galaxies would be 1030. |
| 127 | +If each empty row or column were merely 100 times larger, the sum of the shortest |
| 128 | +paths between every pair of galaxies would be 8410. However, your universe will |
| 129 | +need to expand far beyond these values.) |
| 130 | +
|
| 131 | +Starting with the same initial image, expand the universe according to these new rules, |
| 132 | +then find the length of the shortest path between every pair of galaxies. |
| 133 | +What is the sum of these lengths? |
| 134 | +*/ |
| 135 | + |
| 136 | +package main |
| 137 | + |
| 138 | +import ( |
| 139 | + "bufio" |
| 140 | + "fmt" |
| 141 | + "log" |
| 142 | + "math" |
| 143 | + "os" |
| 144 | +) |
| 145 | + |
| 146 | +func Day11() [2]int { |
| 147 | + return [2]int{ |
| 148 | + d11p1(), |
| 149 | + d11p2(), |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func main() { |
| 154 | + fmt.Println(Day11()) |
| 155 | +} |
| 156 | + |
| 157 | +func GetShortestDistordedDistance(distortion int) int { |
| 158 | + file, err := os.Open("inp.txt") |
| 159 | + |
| 160 | + if err != nil { |
| 161 | + log.Fatal(err) |
| 162 | + } |
| 163 | + |
| 164 | + scanner := bufio.NewScanner(file) |
| 165 | + |
| 166 | + //get all data from input file |
| 167 | + starChart := [][]rune{} |
| 168 | + for scanner.Scan() { |
| 169 | + starChart = append(starChart, []rune(scanner.Text())) |
| 170 | + } |
| 171 | + if scanner.Err() != nil { |
| 172 | + log.Fatal(scanner.Err()) |
| 173 | + } |
| 174 | + |
| 175 | + //find all rows that doesn't have a '#' |
| 176 | + rowsWithoutHash := []int{} |
| 177 | + for y := 0; y < len(starChart); y++ { |
| 178 | + found := false |
| 179 | + for x := 0; x < len(starChart[y]); x++ { |
| 180 | + if starChart[y][x] == '#' { |
| 181 | + found = true |
| 182 | + break |
| 183 | + } |
| 184 | + } |
| 185 | + if !found { |
| 186 | + rowsWithoutHash = append(rowsWithoutHash, y) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + //find all columns that doesn't have a '#' |
| 191 | + colsWithoutHash := []int{} |
| 192 | + for x := 0; x < len(starChart[0]); x++ { |
| 193 | + found := false |
| 194 | + for y := 0; y < len(starChart); y++ { |
| 195 | + if starChart[y][x] == '#' { |
| 196 | + found = true |
| 197 | + break |
| 198 | + } |
| 199 | + } |
| 200 | + if !found { |
| 201 | + colsWithoutHash = append(colsWithoutHash, x) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + //get the distoreded coordinates of all # symbols |
| 206 | + starPos := [][2]int{} |
| 207 | + for y := 0; y < len(starChart); y++ { |
| 208 | + for x := 0; x < len(starChart[y]); x++ { |
| 209 | + if starChart[y][x] == '#' { |
| 210 | + cumulDistortionX := 0 |
| 211 | + cumulDistortionY := 0 |
| 212 | + |
| 213 | + for i := 0; i < len(rowsWithoutHash); i++ { |
| 214 | + if y > rowsWithoutHash[i] { |
| 215 | + cumulDistortionY += distortion - 1 |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + for i := 0; i < len(colsWithoutHash); i++ { |
| 220 | + if x > colsWithoutHash[i] { |
| 221 | + cumulDistortionX += distortion - 1 |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + starPos = append(starPos, [2]int{x + cumulDistortionX, y + cumulDistortionY}) |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + //get the sum of shortest path |
| 231 | + sum := 0 |
| 232 | + for i := 0; i < len(starPos)-1; i++ { |
| 233 | + for j := i + 1; j < len(starPos); j++ { |
| 234 | + sum += ManhattanDistance(starPos[i], starPos[j]) |
| 235 | + } |
| 236 | + } |
| 237 | + return sum |
| 238 | +} |
| 239 | + |
| 240 | +func d11p1() int { |
| 241 | + return GetShortestDistordedDistance(2) |
| 242 | +} |
| 243 | + |
| 244 | +func d11p2() int { |
| 245 | + return GetShortestDistordedDistance(1000000) |
| 246 | +} |
| 247 | + |
| 248 | +// calculate the Manhattan Distance between A and B and return the distance |
| 249 | +func ManhattanDistance(posA [2]int, posB [2]int) int { |
| 250 | + return int(math.Abs(float64(posB[0])-float64(posA[0])) + math.Abs(float64(posB[1])-float64(posA[1]))) |
| 251 | +} |
0 commit comments