Last active
March 30, 2024 06:06
-
-
Save YuanLiou/f9649cdacbc6f1c83b3aea8d0078ec28 to your computer and use it in GitHub Desktop.
Musical scales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class MusicalScaleGenerator { | |
private final Map<Integer, List<Integer>> scaleTransitions = new HashMap<>(); | |
private final int TARGET_SUM = 12; | |
private final List<List<Integer>> validScales = new ArrayList<>(); | |
public static void main(String[] args) { | |
scaleTransitions.put(1, List.of(2, 3)); | |
scaleTransitions.put(2, List.of(1, 2)); | |
scaleTransitions.put(3, List.of(1)); | |
generateScales(); | |
System.out.println(validScales); | |
} | |
private static void generateScales() { | |
for (int startScale = 1; startScale <= 2; startScale++) { | |
exploreScaleCombinations(startScale, 0, new ArrayList<>(), startScale); | |
} | |
} | |
private static void exploreScaleCombinations( | |
int currentScale, | |
int currentSum, | |
List<Integer> currentPath, | |
int startScale) { | |
int newSum = currentSum + currentScale; | |
if (newSum == TARGET_SUM && scaleTransitions.get(startScale).contains(currentScale)) { | |
List<Integer> newPath = new ArrayList<>(currentPath); | |
newPath.add(currentScale); | |
validScales.add(newPath); | |
return; | |
} | |
if (newSum > TARGET_SUM) return; | |
for (Integer nextScale : scaleTransitions.get(currentScale)) { | |
List<Integer> newPath = new ArrayList<>(currentPath); | |
newPath.add(currentScale); | |
exploreScaleCombinations(nextScale, newSum, newPath, startScale); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment