Created
February 9, 2022 02:42
-
-
Save quackduck/f1f8e98b5720f94694d6119c6c3d25e3 to your computer and use it in GitHub Desktop.
Solve AP CS A assertion problems automatically
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
package assertion; | |
import java.util.ArrayList; | |
public class AssertVerifier { | |
static int numConditions = 3; | |
static int numPoints = 5; | |
// numbers to test | |
static int start = -100000; | |
static int end = 100000; | |
public static int[] evalConditions(int x, int y) { | |
return new int[]{ // 1 means false, 2 means true | |
x == 1 ? 2 : 1, | |
x % 2 == 1 ? 2 : 1, | |
y == 0 ? 2 : 1 | |
}; | |
} | |
public static ArrayList<int[]>[] mystery(ArrayList<int[]>[] points, int x) { | |
if (x <= 0) { | |
x = 42; | |
} | |
int y = 0; | |
// Point A | |
points[0].add(evalConditions(x, y)); | |
while (x != 1) { | |
// Point B | |
points[1].add(evalConditions(x, y)); | |
if (x % 2 == 0) { | |
y++; | |
x = x / 2; | |
// Point C | |
points[2].add(evalConditions(x, y)); | |
} else { | |
x = 3 * x + 1; | |
// Point D | |
points[3].add(evalConditions(x, y)); | |
} | |
} | |
// Point E | |
points[4].add(evalConditions(x, y)); | |
return points; | |
} | |
public static void main(String[] args) { | |
solution(); | |
} | |
// use 1 == false, 2 == true | |
// 0 is used elsewhere when the point is not reached | |
// 3 is used to indicate that in a single run this returned both true and false | |
public static void solution() { | |
int[][][] results = new int[end - start + 1][numPoints][numConditions]; | |
for (int i = start; i <= end; i++) { | |
ArrayList<int[]>[] points = new ArrayList[numPoints]; // make the arraylist thing so it's easier for the user | |
for(int j = 0; j < points.length; j++){ | |
points[j] = new ArrayList<>(); | |
} | |
results[i - start] = collapseAndCheckForSometimes(mystery(points, i)); | |
} | |
String[][] table = new String[numPoints][numConditions]; | |
for (int p = 0; p < numPoints; p++) { | |
for (int c = 0; c < numConditions; c++) { | |
boolean wasTrueAtLeastOnce = false; | |
boolean wasFalseAtLeastOnce = false; | |
for (int num = 0; num < results.length; num++) { | |
if (results[num][p][c] == 2) { | |
wasTrueAtLeastOnce = true; | |
} else if (results[num][p][c] == 1) { | |
wasFalseAtLeastOnce = true; | |
} | |
if ((wasFalseAtLeastOnce && wasTrueAtLeastOnce) || results[num][p][c] == 3) { | |
table[p][c] = "S"; // "Sometimes" | |
break; | |
} | |
if (num == results.length - 1) { | |
if (wasTrueAtLeastOnce) { | |
table[p][c] = "A"; // "Always" | |
} else { | |
table[p][c] = "N"; // "Never" | |
} | |
} | |
} | |
} | |
} | |
printSolution(table); | |
} | |
public static void printSolution(String[][] table) { | |
for (String[] point : table) { | |
for (String value : point) { | |
System.out.print(value + " "); | |
} | |
System.out.println(); | |
} | |
} | |
public static int[][] collapseAndCheckForSometimes(ArrayList<int[]>[] points) { | |
int[][] result = new int[numPoints][numConditions]; | |
for (int p = 0; p < points.length; p++) { // for every point | |
for (int c = 0; c < numConditions; c++) { // for every condition | |
boolean wasTrueAtLeastOnce = false; | |
boolean wasFalseAtLeastOnce = false; | |
for (int i = 0; i < points[p].size(); i++) { // for every value of that point and condition | |
if (points[p].get(i)[c] == 2) { | |
wasTrueAtLeastOnce = true; | |
} | |
if (points[p].get(i)[c] == 1) { | |
wasFalseAtLeastOnce = true; | |
} | |
if (wasFalseAtLeastOnce && wasTrueAtLeastOnce) { | |
result[p][c] = 3; | |
break; | |
} | |
} | |
if (wasTrueAtLeastOnce && !wasFalseAtLeastOnce) { | |
result[p][c] = 2; | |
} | |
if (wasFalseAtLeastOnce && !wasTrueAtLeastOnce) { | |
result[p][c] = 1; | |
} | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment