AoC, Day 11, Parts 1 and 2 (Solved)
This commit is contained in:
111
2023/11/README.md
Executable file
111
2023/11/README.md
Executable file
@@ -0,0 +1,111 @@
|
||||
## \-\-\- Day 11: Cosmic Expansion ---
|
||||
|
||||
You continue following signs for "Hot Springs" and eventually come across an [observatory](https://en.wikipedia.org/wiki/Observatory). The Elf within turns out to be a researcher studying cosmic expansion using the giant telescope here.
|
||||
|
||||
He doesn't know anything about the missing machine parts; he's only visiting for this research project. However, he confirms that the hot springs are the next-closest area likely to have people; he'll even take you straight there once he's done with today's observation analysis.
|
||||
|
||||
Maybe you can help him with the analysis to speed things up?
|
||||
|
||||
The researcher has collected a bunch of data and compiled the data into a single giant _image_ (your puzzle input). The image includes _empty space_ ( `.`) and _galaxies_ ( `#`). For example:
|
||||
|
||||
```
|
||||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
||||
|
||||
```
|
||||
|
||||
The researcher is trying to figure out the sum of the lengths of the _shortest path between every pair of galaxies_. However, there's a catch: the universe expanded in the time it took the light from those galaxies to reach the observatory.
|
||||
|
||||
Due to something involving gravitational effects, _only some space expands_. In fact, the result is that _any rows or columns that contain no galaxies_ should all actually be twice as big.
|
||||
|
||||
In the above example, three columns and two rows contain no galaxies:
|
||||
|
||||
```
|
||||
v v v
|
||||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
>..........<
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
>..........<
|
||||
.......#..
|
||||
#...#.....
|
||||
^ ^ ^
|
||||
|
||||
```
|
||||
|
||||
These rows and columns need to be _twice as big_; the result of cosmic expansion therefore looks like this:
|
||||
|
||||
```
|
||||
....#........
|
||||
.........#...
|
||||
#............
|
||||
.............
|
||||
.............
|
||||
........#....
|
||||
.#...........
|
||||
............#
|
||||
.............
|
||||
.............
|
||||
.........#...
|
||||
#....#.......
|
||||
|
||||
```
|
||||
|
||||
Equipped with this expanded universe, the shortest path between every pair of galaxies can be found. It can help to assign every galaxy a unique number:
|
||||
|
||||
```
|
||||
....1........
|
||||
.........2...
|
||||
3............
|
||||
.............
|
||||
.............
|
||||
........4....
|
||||
.5...........
|
||||
............6
|
||||
.............
|
||||
.............
|
||||
.........7...
|
||||
8....9.......
|
||||
|
||||
```
|
||||
|
||||
In these 9 galaxies, there are _36 pairs_. Only count each pair once; order within the pair doesn't matter. For each pair, find any shortest path between the two galaxies using only steps that move up, down, left, or right exactly one `.` or `#` at a time. (The shortest path between two galaxies is allowed to pass through another galaxy.)
|
||||
|
||||
For example, here is one of the shortest paths between galaxies `5` and `9`:
|
||||
|
||||
```
|
||||
....1........
|
||||
.........2...
|
||||
3............
|
||||
.............
|
||||
.............
|
||||
........4....
|
||||
.5...........
|
||||
.##.........6
|
||||
..##.........
|
||||
...##........
|
||||
....##...7...
|
||||
8....9.......
|
||||
|
||||
```
|
||||
|
||||
This path has length `9` because it takes a minimum of _nine steps_ to get from galaxy `5` to galaxy `9` (the eight locations marked `#` plus the step onto galaxy `9` itself). Here are some other example shortest path lengths:
|
||||
|
||||
- Between galaxy `1` and galaxy `7`: 15
|
||||
- Between galaxy `3` and galaxy `6`: 17
|
||||
- Between galaxy `8` and galaxy `9`: 5
|
||||
|
||||
In this example, after expanding the universe, the sum of the shortest path between all 36 pairs of galaxies is `374`.
|
||||
|
||||
Expand the universe, then find the length of the shortest path between every pair of galaxies. _What is the sum of these lengths?_
|
||||
114
2023/11/code.go
Normal file
114
2023/11/code.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/jpillora/puzzler/harness/aoc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
aoc.Harness(run)
|
||||
}
|
||||
|
||||
// on code change, run will be executed 4 times:
|
||||
// 1. with: false (part1), and example input
|
||||
// 2. with: true (part2), and example input
|
||||
// 3. with: false (part1), and user input
|
||||
// 4. with: true (part2), and user input
|
||||
// the return value of each run is printed to stdout
|
||||
func run(part2 bool, input string) any {
|
||||
|
||||
universe := make([][]rune, 0)
|
||||
for _, l := range strings.Split(input, "\n") {
|
||||
if l == "" {
|
||||
continue
|
||||
}
|
||||
line := make([]rune, 0)
|
||||
empty := true
|
||||
for r := range strings.Split(l, "") {
|
||||
if rune(l[r]) != '.' {
|
||||
empty = false
|
||||
}
|
||||
line = append(line, rune(l[r]))
|
||||
}
|
||||
|
||||
if empty {
|
||||
for e := range line {
|
||||
line[e] = '↕'
|
||||
}
|
||||
}
|
||||
universe = append(universe, line)
|
||||
}
|
||||
|
||||
// Now expand inner cols
|
||||
for h := 0; h < len(universe[0]); h++ {
|
||||
empty := true
|
||||
for v := range universe {
|
||||
if universe[v][h] != '.' && universe[v][h] != '↕' {
|
||||
empty = false
|
||||
}
|
||||
}
|
||||
|
||||
if empty {
|
||||
for v := range universe {
|
||||
if universe[v][h] == '↕' {
|
||||
universe[v][h] = '⥁'
|
||||
} else {
|
||||
universe[v][h] = '↔'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// when you're ready to do part 2, remove this "not implemented" block
|
||||
factor := 1
|
||||
if part2 {
|
||||
factor = 1000000
|
||||
} else {
|
||||
// solve part 1 here
|
||||
factor = 2
|
||||
}
|
||||
|
||||
coords := make([][]int, 0)
|
||||
|
||||
vr := 0
|
||||
for v := range universe {
|
||||
if universe[v][0] == '↕' || universe[v][0] == '⥁' {
|
||||
// expand else normal v
|
||||
vr += factor
|
||||
} else {
|
||||
vr += 1
|
||||
}
|
||||
|
||||
hr := 0
|
||||
for h := range universe {
|
||||
if universe[0][h] == '↔' || universe[0][h] == '⥁' {
|
||||
hr += factor
|
||||
} else {
|
||||
hr += 1
|
||||
}
|
||||
|
||||
if universe[v][h] == '#' {
|
||||
coords = append(coords, []int{vr, hr})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sumHyp := 0
|
||||
compared := 0
|
||||
for c1 := 0; c1 < len(coords)-1; c1++ {
|
||||
for c2 := c1 + 1; c2 < len(coords); c2++ {
|
||||
// add rise and run?
|
||||
lenv := int(math.Abs(float64(coords[c1][0] - coords[c2][0])))
|
||||
lenh := int(math.Abs(float64(coords[c1][1] - coords[c2][1])))
|
||||
|
||||
// convert to int?
|
||||
sumHyp += lenv + lenh
|
||||
|
||||
compared++
|
||||
}
|
||||
}
|
||||
|
||||
return sumHyp
|
||||
}
|
||||
10
2023/11/input-example.txt
Normal file
10
2023/11/input-example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
||||
140
2023/11/input-user.txt
Normal file
140
2023/11/input-user.txt
Normal file
@@ -0,0 +1,140 @@
|
||||
..........#............................................#..........#.....#...........................................#.......#.........#.....
|
||||
.....#...................#..............#....................................................#..............................................
|
||||
..............................#....................#...................................................#....................................
|
||||
.............................................#.....................................................................................#........
|
||||
............#................................................................#......#.......................................................
|
||||
......................................#..................#.........................................#........................................
|
||||
.............................................................................................................................#..............
|
||||
.................#.....#....................................................................................................................
|
||||
.....................................................#...........#..........................................#.............................#.
|
||||
.....#..........................#...............#.......................................#...........................#.......................
|
||||
....................#................#...............................#.........................#.....#......................................
|
||||
...........#.............................................#..................................................................................
|
||||
................................................................................................................................#.....#.....
|
||||
................#...................................#...........#......................................................#....................
|
||||
.........................#.................................................#.................................#.............................#
|
||||
...#..............................#...................................#................................#....................................
|
||||
...............................................................................#............................................................
|
||||
..................................................................#...........................#..........................#........#.........
|
||||
.........#....................#........................................................................................................#....
|
||||
..........................................................................#..............#........................#.........................
|
||||
....................#...............................#........#..............................................................................
|
||||
..#..............................#........#...................................#..........................#..................#...............
|
||||
......................................................................#...........................#............#...................#........
|
||||
........#................#..............................#.............................................................#.................#...
|
||||
......................................#......#....................#........#.....#.........#................................................
|
||||
.....................#............................#.............................................................................#...........
|
||||
....#.........................#.............................................................................#...............................
|
||||
............#..................................................#................................#...........................#...............
|
||||
....................................................................................#..................#.............#......................
|
||||
..........................................................................................................................................#.
|
||||
...................................#...............#.....................................#.......................#..........................
|
||||
..#................#.......................#................................................................................................
|
||||
...........#...............#...........................................#.................................................#.........#........
|
||||
......#.....................................................................#............................#..................................
|
||||
.........................................................#..................................................................................
|
||||
...................................................................#........................................................................
|
||||
.........#....................................................#......................................#............#....................#....
|
||||
...................................................#....................#...........#......................#...............#................
|
||||
..#..............................#......#........................................................#..........................................
|
||||
........................................................#...........................................................................#.......
|
||||
......................................................................................................................#.....................
|
||||
......#......................................................................#................#.............................................
|
||||
............................................#..............#............................................#...................#...............
|
||||
.......................#........#......#........................#..................................................#........................
|
||||
....................................................#......................................#.......#.........#..............................
|
||||
........#..........................................................................................................................#........
|
||||
................#...........................................................................................................................
|
||||
#..........................#.......#......#.............................#.................................#.................................
|
||||
......................................................#.............................#............#.......................#.....#............
|
||||
...................#.............................................#...........#...........#..............................................#...
|
||||
...............................#..................#..........................................................#..............................
|
||||
..............#..............................................#.......................................#......................................
|
||||
..#.....................#.............#........................................................#............................................
|
||||
...................................................................#........................................................................
|
||||
............................................................................................................................................
|
||||
........#.......#.....................................................................#.....................................................
|
||||
...............................#.....................#..................#.....................................#.........#...................
|
||||
..............................................#...............................#............#........#.......................................
|
||||
.#.................#........................................................................................................................
|
||||
..................................#................................................#.....................................................#..
|
||||
............................................................................................................................................
|
||||
...........#..................#....................#....................................#...................................................
|
||||
....#....................................#.........................#.....#......................#...............#..............#............
|
||||
.................................................................................#..........................................................
|
||||
...............................................#........................................................#...........#.....................#.
|
||||
........................#........#.........................#................................................................................
|
||||
....................................................................................#..............................................#........
|
||||
...#......................................#.................................................................................................
|
||||
.....................................................#.........................................#......#.........#...........................
|
||||
..............................#..........................................................#.................#................#...............
|
||||
..............#....................................................#......#.................................................................
|
||||
........#............#..........................................................................................................#...........
|
||||
............................................................................................................................................
|
||||
...........................#.....................#..............#..................................#................................#.......
|
||||
............................................................................#...............................................................
|
||||
.............................................#..........................................................#.................................#.
|
||||
.....................................................#..............#.......................................................................
|
||||
......#................#................#..................#.................................................#...........#..................
|
||||
................#.................#......................................#........................................#...........#.............
|
||||
...........#....................................#................#..........................#......................................#........
|
||||
............................................................................................................................................
|
||||
...................#..................................#.....................................................................................
|
||||
...#.......................#..............#.................#..............#..........#...................................................#.
|
||||
......................................................................................................#................#.............#......
|
||||
....................................................................#............................#..............................#...........
|
||||
...............................#..................#.......................................#...............#................#................
|
||||
............................................................................................................................................
|
||||
............................................................................................................................................
|
||||
......................#...............................#..........#.............................................#...................#........
|
||||
........................................#..............................................................#.................#..................
|
||||
..........................#........#.......................................#..........#....................................................#
|
||||
..................#...........................#.............................................................................................
|
||||
......#....................................................#...............................#................................................
|
||||
..............................#.............................................................................................................
|
||||
................................................................................................#........#..................................
|
||||
#.............#.......................................................#........#...............................................#.......#....
|
||||
...........................#...................................................................................#............................
|
||||
....#...................................#...................................................................................................
|
||||
................................................#......#..................................................................................#.
|
||||
.......................................................................................#.............#......................................
|
||||
................................#..........................#..................#...........................#.................................
|
||||
....................#...........................................................................................#...................#.......
|
||||
...................................................#..............#.......#............................................#....................
|
||||
..#.........................................................................................................................................
|
||||
........#.....#.......................#.......#..............................................................................#..............
|
||||
......................#.......#.........................#........................#...........#.......#...................................#..
|
||||
...............................................................................................................#............................
|
||||
.......................................................................................#..................#.................................
|
||||
........................................................................#.........................#..................#......................
|
||||
..........................................#.....#.........................................................................#.................
|
||||
..................#..........#...............................#..............................................................................
|
||||
......................................#........................................#.........#..................................................
|
||||
...#...............................................................................................................#....................#...
|
||||
........#..............................................#...............#.....................................................#..............
|
||||
...................................#........................................#...............#............#..........................#.......
|
||||
............#.................#.............................................................................................................
|
||||
....................#.............................#.........................................................................................
|
||||
....................................................................................#..............#........................................
|
||||
...............#......................................#.......................................#.........................................#...
|
||||
.#........................#........................................#...............................................#......#.................
|
||||
........................................#.................................................#.................................................
|
||||
.....#...................................................................#..................................................................
|
||||
..................................................................................................#............#............................
|
||||
............#.......#.........#.....#...............#.......#...........................................#...................................
|
||||
..#..........................................#......................#..................#...............................................#....
|
||||
............................................................................................................................................
|
||||
.........................#.............#....................................................................................................
|
||||
..................................................#............................#................................#...................#.......
|
||||
...................................#..............................................................#......#..................................
|
||||
............................................................................................................................................
|
||||
........................................................#.....#............................#..........................#.....#...............
|
||||
......................#.......................#..........................#.............................................................#....
|
||||
......#.....#.........................................................................................#.....................................
|
||||
..................................................................#...............................................................#.........
|
||||
...................................#.........................................#.................#..........................#...............#.
|
||||
..#.........................................#.........................#..............#......................................................
|
||||
...................................................#......................................#...................#.............................
|
||||
.........................................................#..................................................................................
|
||||
..............#................................................#..................................#.........................................
|
||||
...................#..........#..........#................................................................#......#............#............#
|
||||
Reference in New Issue
Block a user