CF 102697091 - Spacelab
The two spacelabs are located on planets in our solar system. The planets have a fixed order by distance from the Sun: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune.
Rating: -
Tags: -
Solve time: 52s
Verified: yes
Solution
Problem Understanding
The two spacelabs are located on planets in our solar system. The planets have a fixed order by distance from the Sun:
Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune.
The input gives the planet containing your spacelab on the first line and the planet containing the rival spacelab on the second line. The task is simply to compare their positions in this ordered sequence. If your planet appears earlier, print CLOSER. If it appears later, print FARTHER AWAY. If both names are equal, print THE SAME.
There are only eight possible planet names, and the input always contains valid solar-system planets. The running time limit is one second and the memory limit is 256 MB. Since the input itself contains only two short strings from a fixed set of eight values, even a linear scan through all planets performs at most a handful of comparisons. There is no meaningful risk of a time-limit issue here. The main goal is to turn the verbal ordering of the planets into something the program can compare directly.
The first edge case is when both spacelabs are on the same planet. For example:
Neptune
Neptune
The correct output is:
THE SAME
A careless implementation that only checks whether the first planet comes before or after the second could fall through without producing the required equality result.
The second edge case is when the first planet is the farthest possible planet and the second is the closest:
Neptune
Mercury
The correct output is:
FARTHER AWAY
An implementation that accidentally interprets the planet order backwards would produce the opposite answer.
The third useful boundary case is two adjacent planets:
Earth
Mars
The correct output is:
CLOSER
This catches off-by-one mistakes in an index-based representation. Earth has index 2 and Mars has index 3 when counting from zero, so Earth must be classified as closer.
Approaches
The most direct approach is to store the eight planets in their order from the Sun and search through that array to find the position of each input planet. If the first planet is found at position a and the second at position b, comparing a and b gives the answer immediately. This is effectively brute force over a constant-sized collection. In the worst case, finding each planet requires checking all eight entries, so there are at most 16 planet-name comparisons.
Because the list has fixed size eight, this approach is already easily fast enough. There is no input size at which the scan becomes problematic for this particular problem. Still, the underlying idea can be made cleaner by observing that the only information we need from a planet name is its position in the ordering. A dictionary maps each name directly to its position, so each lookup takes expected O(1) time.
The brute-force scan works because the search space contains only eight planets, but the observation that the planet ordering is fixed lets us represent every planet by a numerical rank. Once both names have been converted to ranks, the rest of the problem is an ordinary integer comparison.
| Approach | Time Complexity | Space Complexity | Verdict |
|---|---|---|---|
| Brute Force | O(8), effectively O(1) | O(8), effectively O(1) | Accepted |
| Optimal | O(1) expected | O(8), effectively O(1) | Accepted |
The dictionary version is preferable in an editorial solution because it expresses the central idea directly: each planet has a fixed rank, and the answer depends only on comparing those ranks.
Algorithm Walkthrough
- Create a mapping from every planet name to its position in the order from the Sun. Mercury gets rank 0, Venus gets rank 1, and Neptune gets rank 7. The exact numerical values do not matter, only their relative order does.
- Read the two planet names from the input. The first name belongs to our spacelab and the second belongs to the rival spacelab.
- Look up both names in the mapping. This converts the original problem about planet names into a comparison between two integers.
- Compare the two ranks. If our rank is smaller, our planet is closer to the Sun, so print
CLOSER. If our rank is larger, printFARTHER AWAY. If the ranks are equal, both spacelabs are on the same planet, so printTHE SAME.
Why it works
The mapping preserves exactly the ordering specified by the problem: a smaller rank means a planet is closer to the Sun. Thus, after converting both planet names to ranks, every possible pair falls into exactly one of three cases. Our rank can be smaller, larger, or equal to the rival's rank, and those three cases correspond exactly to the three required output strings.
Python Solution
import sys
input = sys.stdin.readline
def solve():
order = {
"Mercury": 0,
"Venus": 1,
"Earth": 2,
"Mars": 3,
"Jupiter": 4,
"Saturn": 5,
"Uranus": 6,
"Neptune": 7,
}
mine = input().strip()
rival = input().strip()
a = order[mine]
b = order[rival]
if a < b:
print("CLOSER")
elif a > b:
print("FARTHER AWAY")
else:
print("THE SAME")
if __name__ == "__main__":
solve()
The order dictionary is the core data structure from Algorithm Walkthrough step 1. The values increase as the planets become farther from the Sun, so comparing them has the same meaning as comparing the physical locations in the problem.
The two calls to input() read exactly the two planet names. Using .strip() removes the newline without changing the planet name itself.
The three branches correspond directly to the three possible relationships between the two ranks. The equality branch is necessary because a planet can be the same for both spacelabs.
There are no integer-overflow concerns because the ranks range only from 0 through 7. There are also no boundary checks needed around the dictionary lookup because the statement guarantees that both names are valid planets.
Worked Examples
Sample 1
Input:
Mars
Jupiter
The execution is:
| Variable | Value |
|---|---|
mine |
Mars |
rival |
Jupiter |
a |
3 |
b |
4 |
| comparison | 3 < 4 |
| output | CLOSER |
Mars has rank 3 and Jupiter has rank 4, so Mars is closer to the Sun. This demonstrates the normal case where the two planets are different and the first one is closer.
Sample 2
Input:
Uranus
Mercury
The execution is:
| Variable | Value |
|---|---|
mine |
Uranus |
rival |
Mercury |
a |
6 |
b |
0 |
| comparison | 6 > 0 |
| output | FARTHER AWAY |
Uranus has a larger rank than Mercury, so it is farther from the Sun. This trace confirms that the comparison direction is not tied to which planet is named first in the statement.
Sample 3
Input:
Neptune
Neptune
The execution is:
| Variable | Value |
|---|---|
mine |
Neptune |
rival |
Neptune |
a |
7 |
b |
7 |
| comparison | a == b |
| output | THE SAME |
The two ranks are equal, so the equality branch is selected. This is the case that would be missed by an implementation containing only < and > comparisons.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(1) expected | There are two dictionary lookups and one integer comparison, with only eight possible keys. |
| Space | O(1) | The dictionary always contains exactly eight planet names. |
The input size is fixed by the problem itself, so the solution is comfortably within the one-second time limit and 256 MB memory limit. Even the linear-search alternative would be effectively constant time, while the dictionary implementation makes the comparison logic especially clear.
Test Cases
import sys
import io
def solve():
input = sys.stdin.readline
order = {
"Mercury": 0,
"Venus": 1,
"Earth": 2,
"Mars": 3,
"Jupiter": 4,
"Saturn": 5,
"Uranus": 6,
"Neptune": 7,
}
mine = input().strip()
rival = input().strip()
a = order[mine]
b = order[rival]
if a < b:
print("CLOSER")
elif a > b:
print("FARTHER AWAY")
else:
print("THE SAME")
def run(inp: str) -> str:
old_stdin = sys.stdin
old_stdout = sys.stdout
try:
sys.stdin = io.StringIO(inp)
sys.stdout = io.StringIO()
solve()
return sys.stdout.getvalue()
finally:
sys.stdin = old_stdin
sys.stdout = old_stdout
# Provided samples
assert run("Mars\nJupiter\n") == "CLOSER\n", "sample 1"
assert run("Uranus\nMercury\n") == "FARTHER AWAY\n", "sample 2"
assert run("Neptune\nNeptune\n") == "THE SAME\n", "sample 3"
# Minimum-distance pair
assert run("Mercury\nVenus\n") == "CLOSER\n", "closest adjacent planets"
# Reverse boundary pair
assert run("Neptune\nMercury\n") == "FARTHER AWAY\n", "farthest versus closest"
# All-equal case
assert run("Earth\nEarth\n") == "THE SAME\n", "same planet"
# Adjacent pair in the other direction
assert run("Mars\nEarth\n") == "FARTHER AWAY\n", "reverse adjacent planets"
| Test input | Expected output | What it validates |
|---|---|---|
Mars / Jupiter |
CLOSER |
Provided sample with the first planet closer |
Uranus / Mercury |
FARTHER AWAY |
Provided sample with the first planet farther |
Neptune / Neptune |
THE SAME |
Provided equality case |
Mercury / Venus |
CLOSER |
First boundary pair and smallest ranks |
Neptune / Mercury |
FARTHER AWAY |
Extreme opposite ends of the ordering |
Earth / Earth |
THE SAME |
Equality for a middle planet |
Mars / Earth |
FARTHER AWAY |
Reverse ordering of adjacent planets |
Edge Cases
For equal planets, consider:
Neptune
Neptune
Both dictionary lookups produce rank 7. Since a < b and a > b are both false, the algorithm reaches the equality branch and prints THE SAME. This prevents the common mistake of treating equality as either closer or farther.
For the extreme ordering, consider:
Neptune
Mercury
The ranks are 7 and 0. The first rank is larger, so the algorithm prints FARTHER AWAY. No special handling for the first or last planet is required because the rank representation naturally handles both boundaries.
For adjacent planets, consider:
Earth
Mars
Earth maps to 2 and Mars maps to 3. Since 2 is smaller than 3, the result is CLOSER. Reversing the input to Mars and Earth produces ranks 3 and 2 and consequently FARTHER AWAY. This confirms that the implementation compares the actual ordering rather than relying on alphabetical order or the textual appearance of the names.