Skip to content

Commit

Permalink
feat(2019): day 6 - universal orbit map
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Dec 11, 2023
1 parent a5b10c8 commit 8211e01
Show file tree
Hide file tree
Showing 9 changed files with 1,276 additions and 0 deletions.
124 changes: 124 additions & 0 deletions 2019/Day06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# [--- Day 6: Universal Orbit Map ---](https://adventofcode.com/2019/day/6)

You've landed at the Universal Orbit Map facility on Mercury. Because navigation in space often involves transferring between orbits, the orbit maps here are useful for finding efficient routes between, for example, you and Santa. You download a map of the local orbits (your puzzle input).

Except for the universal Center of Mass (COM), every object in space is in [orbit](https://en.wikipedia.org/wiki/Orbit) around exactly one other object. An orbit looks roughly like this:

```txt
\
\
|
|
AAA--> o o <--BBB
|
|
/
/
```

In this diagram, the object BBB is in orbit around AAA. The path that BBB takes around AAA (drawn with lines) is only partly shown. In the map data, this orbital relationship is written AAA)BBB, which means "BBB is in orbit around AAA".

Before you use your map data to plot a course, you need to make sure it wasn't corrupted during the download. To verify maps, the Universal Orbit Map facility uses orbit count checksums - the total number of direct orbits (like the one shown above) and indirect orbits.

Whenever A orbits B and B orbits C, then A indirectly orbits C. This chain can be any number of objects long: if A orbits B, B orbits C, and C orbits D, then A indirectly orbits D.

For example, suppose you have the following map:

```txt
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
```

Visually, the above map of orbits looks like this:

```txt
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I
```

In this visual representation, when two objects are connected by a line, the one on the right directly orbits the one on the left.

Here, we can count the total number of orbits as follows:

- D directly orbits C and indirectly orbits B and COM, a total of 3 orbits.
- L directly orbits K and indirectly orbits J, E, D, C, B, and COM, a total of 7 orbits.
- COM orbits nothing.

The total number of direct and indirect orbits in this example is `42`.

**What is the total number of direct and indirect orbits in your map data?**

> Your puzzle answer was `150150`.
## --- Part Two ---

Now, you just need to figure out how many orbital transfers you (YOU) need to take to get to Santa (SAN).

You start at the object YOU are orbiting; your destination is the object SAN is orbiting. An orbital transfer lets you move from any object to an object orbiting or orbited by that object.

For example, suppose you have the following map:

```txt
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN
```

Visually, the above map of orbits looks like this:

```txt
YOU
/
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I - SAN
```

In this example, YOU are in orbit around K, and SAN is in orbit around I. To move from K to I, a minimum of 4 orbital transfers are required:

```txt
K to J
J to E
E to D
D to I
```

Afterward, the map of orbits looks like this:

```txt
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I - SAN
\
YOU
```

**What is the minimum number of orbital transfers required to move from the object YOU are orbiting to the object SAN is orbiting?** _(Between the objects they are orbiting - not between YOU and SAN.)_

> Your puzzle answer was `352`.
47 changes: 47 additions & 0 deletions 2019/Day06/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from collections import defaultdict


def get_adjacency_list(input: list[str]) -> dict[str, list[str]]:
adjacency_list = defaultdict(list)
for line in input:
parent, child = line.split(")")
adjacency_list[parent].append(child)
adjacency_list[child].append(parent)
return dict(adjacency_list)


def get_total_orbits(inputs: list[str]) -> int:
adj_list = get_adjacency_list(inputs)

stack = [("COM", 0)]
total_orbits = 0
visited = set(["COM"])

while stack:
node, depth = stack.pop()
total_orbits += depth

for child in adj_list[node]:
if child not in visited:
visited.add(child)
stack.append((child, depth + 1))

return total_orbits


def get_num_of_orbital_transfers(inputs: list[str]) -> int:
adj_list = get_adjacency_list(inputs)

stack = [("YOU", 0)]
visited = set(["YOU"])

while stack:
node, depth = stack.pop()

if node == "SAN":
return depth - 2

for child in adj_list[node]:
if child not in visited:
stack.append((child, depth + 1))
visited.add(node)
Loading

0 comments on commit 8211e01

Please sign in to comment.