Skip to content

Instantly share code, notes, and snippets.

@axayjha
Created May 16, 2025 14:41
Show Gist options
  • Save axayjha/0119cd0c3967f5e8656f498ce8d6ae16 to your computer and use it in GitHub Desktop.
Save axayjha/0119cd0c3967f5e8656f498ce8d6ae16 to your computer and use it in GitHub Desktop.
q9
import java.util.ArrayList;
import java.util.List;
// The Gson imports shown in the screenshot (com.google.gson.*) are not needed for this problem's core logic.
public class Player { // Assuming CoderPad uses a class named Player or similar
/**
* Calculates the sequence of actions to transform an atom from a starting state
* (protonsStart, neutronsStart) to a target state (protonsTarget, neutronsTarget).
*
* @param protonsStart The initial number of protons.
* @param neutronsStart The initial number of neutrons.
* @param protonsTarget The desired number of protons.
* @param neutronsTarget The desired number of neutrons.
* @return A list of strings representing the actions ("PROTON", "NEUTRON", "ALPHA").
*/
public static List<String> solve(int protonsStart, int neutronsStart, int protonsTarget, int neutronsTarget) {
List<String> actions = new ArrayList<>();
int currentP = protonsStart;
int currentN = neutronsStart;
// Loop until the target state is reached or the action limit is about to be exceeded.
while (currentP != protonsTarget || currentN != neutronsTarget) {
// Check defeat condition: List exceeds 1000 actions.
// We check before adding an action. If size is 1000, we can't add more.
if (actions.size() >= 1000) {
break;
}
// Decision logic for choosing the next action:
if (currentP > protonsTarget && currentN > neutronsTarget) {
// Both protons and neutrons are too high; ALPHA helps reduce both.
actions.add("ALPHA");
currentP -= 2;
currentN -= 2;
} else if (currentP > protonsTarget) {
// Protons are too high. ALPHA is the only way to reduce protons.
// This will also affect neutrons, which may need correction in subsequent steps.
actions.add("ALPHA");
currentP -= 2;
currentN -= 2;
} else if (currentN > neutronsTarget) {
// Neutrons are too high. ALPHA is the only way to reduce neutrons.
// This will also affect protons, which may need correction in subsequent steps.
actions.add("ALPHA");
currentP -= 2;
currentN -= 2;
} else if (currentP < protonsTarget) {
// Protons are too low. Add a proton.
actions.add("PROTON");
currentP++;
} else if (currentN < neutronsTarget) {
// At this point, currentP must be equal to protonsTarget.
// Neutrons are too low. Add a neutron.
actions.add("NEUTRON");
currentN++;
} else {
// This 'else' block should theoretically not be reached if the loop condition
// (currentP != protonsTarget || currentN != neutronsTarget) is true,
// because one of the preceding conditions should have matched.
// It acts as a safeguard against potential infinite loops not caught by the action limit.
break;
}
}
return actions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment