problem12 effective solve

This commit is contained in:
Xevion
2019-09-20 20:31:13 -05:00
parent cfbc66b891
commit 7d635a55d6
7 changed files with 242 additions and 77 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="1.8 (1)" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8 (1)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
</project>

View File

@@ -2,15 +2,13 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="1fd89a9c-d895-4e45-acab-398cb78a9403" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/12.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/bin/CheckerBoard.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/bin/Point.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/bin/problem12.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/input.dat" beforeDir="false" afterPath="$PROJECT_DIR$/input.dat" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/problem12.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/problem12.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../3/java/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../3/java/.idea/workspace.xml" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/bin/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@@ -24,43 +22,48 @@
<usages-collector id="statistics.lifecycle.project">
<counts>
<entry key="project.closed" value="1" />
<entry key="project.open.time.4" value="1" />
<entry key="project.open.time.6" value="1" />
<entry key="project.opened" value="1" />
<entry key="project.opened" value="2" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.open">
<counts>
<entry key="java" value="1" />
<entry key="dat" value="1" />
<entry key="java" value="3" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.open">
<counts>
<entry key="JAVA" value="1" />
<entry key="JAVA" value="3" />
<entry key="PLAIN_TEXT" value="1" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.edit">
<counts>
<entry key="java" value="42" />
<entry key="dat" value="127" />
<entry key="java" value="2514" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.edit">
<counts>
<entry key="JAVA" value="42" />
<entry key="JAVA" value="2514" />
<entry key="PLAIN_TEXT" value="127" />
</counts>
</usages-collector>
</session>
</component>
<component name="FileEditorManager">
<leaf>
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/problem12.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="153">
<caret line="29" lean-forward="true" selection-start-line="29" selection-end-line="29" />
<state relative-caret-position="192">
<caret line="147" column="24" selection-start-line="147" selection-start-column="24" selection-end-line="147" selection-end-column="24" />
<folding>
<element signature="imports" expanded="true" />
<element signature="e#880#881#0" expanded="true" />
<element signature="e#901#902#0" expanded="true" />
<element signature="e#4994#5001#0" expanded="true" />
<element signature="e#4159#4166#0" expanded="true" />
<element signature="e#4307#4316#0" expanded="true" />
</folding>
</state>
</provider>
@@ -68,6 +71,12 @@
</file>
</leaf>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>debug</find>
<find>offsets</find>
</findStrings>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../.." />
</component>
@@ -83,6 +92,7 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/input.dat" />
<option value="$PROJECT_DIR$/src/problem12.java" />
</list>
</option>
@@ -99,6 +109,7 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="PackagesPane" />
<pane id="ProjectPane">
<subPane>
@@ -122,12 +133,14 @@
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1568926242667" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1569029426079" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="project.structure.last.edited" value="Project" />
<property name="project.structure.proportion" value="0.15" />
<property name="project.structure.side.proportion" value="0.2" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
@@ -168,49 +181,80 @@
</task>
<servers />
</component>
<component name="TodoView">
<todo-panel id="selected-file">
<is-autoscroll-to-source value="true" />
</todo-panel>
<todo-panel id="all">
<are-packages-shown value="true" />
<is-autoscroll-to-source value="true" />
</todo-panel>
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1066" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="Image Layers" />
<window_info id="Designer" />
<window_info id="UI Designer" />
<window_info id="Capture Tool" />
<window_info id="Favorites" side_tool="true" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.25" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.25266525" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="Version Control" show_stripe_button="false" />
<window_info anchor="bottom" id="Terminal" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info active="true" anchor="bottom" id="Messages" visible="true" weight="0.32939914" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="UI Designer" order="4" />
<window_info id="Capture Tool" order="5" />
<window_info id="Favorites" order="6" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32939914" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.39914164" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="right" id="Palette" />
<window_info anchor="right" id="Theme Preview" />
<window_info anchor="right" id="Capture Analysis" />
<window_info anchor="right" id="Palette&#9;" />
<window_info anchor="right" id="Maven Projects" />
<window_info anchor="bottom" id="TODO" order="6" weight="0.32939914" />
<window_info anchor="bottom" id="Terminal" order="7" />
<window_info anchor="bottom" id="Event Log" order="8" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="9" show_stripe_button="false" />
<window_info anchor="bottom" id="Messages" order="10" weight="0.32939914" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Capture Analysis" order="4" />
<window_info anchor="right" id="Theme Preview" order="5" />
<window_info anchor="right" id="Palette&#9;" order="6" />
<window_info anchor="right" id="Maven Projects" order="7" />
</layout>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="editorHistoryManager">
<entry file="jar://C:/Program Files/AdoptOpenJDK/jdk-8.0.212.04-hotspot/src.zip!/com/sun/xml/internal/ws/api/message/HeaderList.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="178">
<caret line="112" column="13" selection-start-line="112" selection-start-column="13" selection-end-line="112" selection-end-column="13" />
</state>
</provider>
</entry>
<entry file="jar://C:/Program Files/AdoptOpenJDK/jdk-8.0.212.04-hotspot/src.zip!/java/util/ArrayList.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="442">
<caret line="444" column="13" selection-start-line="444" selection-start-column="13" selection-end-line="444" selection-end-column="13" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/input.dat">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="221">
<caret line="13" column="8" lean-forward="true" selection-start-line="13" selection-start-column="8" selection-end-line="13" selection-end-column="8" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/problem12.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="153">
<caret line="29" lean-forward="true" selection-start-line="29" selection-end-line="29" />
<state relative-caret-position="192">
<caret line="147" column="24" selection-start-line="147" selection-start-column="24" selection-end-line="147" selection-end-column="24" />
<folding>
<element signature="imports" expanded="true" />
<element signature="e#880#881#0" expanded="true" />
<element signature="e#901#902#0" expanded="true" />
<element signature="e#4994#5001#0" expanded="true" />
<element signature="e#4159#4166#0" expanded="true" />
<element signature="e#4307#4316#0" expanded="true" />
</folding>
</state>
</provider>
@@ -218,6 +262,66 @@
</component>
<component name="masterDetails">
<states>
<state key="ArtifactsStructureConfigurable.UI">
<settings>
<artifact-editor />
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="FacetStructureConfigurable.UI">
<settings>
<last-edited>No facets are configured</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="GlobalLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="JdkListConfigurable.UI">
<settings>
<last-edited>1.8 (2)</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ModuleStructureConfigurable.UI">
<settings>
<last-edited>12</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
<option value="0.6" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>11</last-edited>
@@ -230,6 +334,17 @@
</splitter-proportions>
</settings>
</state>
<state key="ProjectLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -1,4 +1,4 @@
1
2
B B
B B B
B B B
@@ -6,4 +6,12 @@ B B B
B B R
R R R R
R R R R
R R R
R R R
B B B B
B
B B B R
R
B
R R
R B R
R R R R

View File

@@ -17,6 +17,7 @@ class Point {
Point(int x, int y) {
this.x = x;
this.y = y;
this.type = "";
}
// Typed Point Constructor
@@ -28,7 +29,7 @@ class Point {
// Returns a point which is the translated end-point based on the other point (offset).
Point merge(Point other) {
return new Point(this.x + other.x, this.y + other.y);
return new Point(this.x + other.x, this.y + other.y, this.type);
}
void offset(Point other) {
@@ -46,13 +47,13 @@ class Point {
);
}
public boolean isEmpty() {
return this.type == "";
boolean isEmpty() {
return this.type.equals("");
}
// equals() method for testing equality of two Point() objects
// positional untyped equality test, see fullEquals()
public boolean equals(Point other) {
boolean equals(Point other) {
return this.x == other.x && this.y == other.y;
}
@@ -62,7 +63,7 @@ class Point {
}
// Returns whether a point could be found in a collection
public boolean collectionContains(Collection<Point> collection) {
boolean collectionContains(Collection<Point> collection) {
for(Point other : collection) {
if(this.x == other.x && this.y == other.y) {
return true;
@@ -81,12 +82,10 @@ class Point {
return -1;
}
public static List<Point> asList(int[][] primitivePoints) {
static List<Point> asList(int[][] primitivePoints) {
List<Point> points = new ArrayList<Point>();
for(int x = 0; x < primitivePoints.length; x++) {
for(int y = 0; y < primitivePoints[x].length; y++) {
points.add(new Point(x, y));
}
for (int[] primPoint: primitivePoints) {
points.add(new Point(primPoint[0], primPoint[1]));
}
return points;
}
@@ -95,11 +94,13 @@ class Point {
// Represents a checkers checkerboard
class CheckerBoard {
// Offsets
List<Point> offsets = Point.asList(new int[][]{{1, 1}, {-1, -1}, {-1, 1}, {1, -1}});
Point[][] matrix;
private List<Point> team1offsets = Point.asList(new int[][]{{-1, -1}, {1, -1}});
private List<Point> team2offsets = Point.asList(new int[][]{{1, 1}, {-1, 1}});
// private List<Point> offsets = Point.asList(new int[][]{{1, 1}, {-1, -1}, {-1, 1}, {1, -1}});
private Point[][] matrix;
// Team Constants, Team1 is default team.
String team1 = "R";
String team2 = "B";
private String team1 = "R";
private String team2 = "B";
CheckerBoard(String[][] matrix) {
// Build the point matrix
@@ -111,11 +112,22 @@ class CheckerBoard {
}
}
public String toString() {
String result = "";
for(int x = 0; x < this.matrix.length; x++) {
String[] temp = new String[this.matrix[x].length];
for(int y = 0; y < this.matrix[x].length; y++) {
temp[y] = this.matrix[x][y].type.equals("") ? " " : this.matrix[x][y].type; }
result += String.join(" - ", temp) + "\n";
}
return result;
}
public static boolean inBounds(Point point) {return CheckerBoard.inBounds(point.x, point.y);}
public static boolean inBounds(int x, int y) {return x >= 0 && y >= 0 && x < 8 && y < 8;}
Point getPoint(Point point) {return this.matrix[point.x][point.y];}
String getType(Point point) {return this.matrix[point.x][point.y].type;}
boolean isReverse(Point first, Point second) {return first.type == team1 ? second.type == team2 : (first.type == team2 ? second.type == team1 : false);}
boolean isReverse(Point first, Point second) {return (first.type.equals(team1) && second.type.equals(team2)) || (first.type.equals(team2) && second.type.equals(team1));}
// Just returns all Points with type designated
List<Point> getCheckers(String type) {
@@ -129,23 +141,45 @@ class CheckerBoard {
return found;
}
int getMaxJumps(Point point) {return getMaxJumps(point, 0, Arrays.asList(point));}
List<Point> getOffsets(Point point) {
if(point.type.equals(team1))
return team1offsets;
else if(point.type.equals(team2))
return team2offsets;
else {
out.println(String.format("Invalid Team Type Detected - \"%s\"", point.type));
List<Point> offsets = new ArrayList<Point>();
offsets.addAll(team1offsets);
offsets.addAll(team2offsets);
return offsets;
}
}
int getMaxJumps(Point point) {
List<Point> previous = new ArrayList<Point>();
previous.add(point);
return getMaxJumps(point, 0, previous);
}
int getMaxJumps(Point point, int score, List<Point> previous) {
List<Integer> offsetScores = Arrays.asList(0);
for(Point offset : this.offsets) {
List<Integer> offsetScores = new ArrayList<Integer>();
offsetScores.add(score);
for(Point offset : this.getOffsets(point)) {
Point newPoint = point.merge(offset);
// Ensure new point is in matrix bounds
if(CheckerBoard.inBounds(newPoint)) {
// Ensure new point is of reverse type
if(this.isReverse(newPoint, this.getPoint(newPoint))) {
newPoint = point.merge(offset);
Point realPoint = this.getPoint(newPoint);
if(this.isReverse(point, realPoint)) {
newPoint.offset(offset);
// Ensure second new point is inBounds & Empty
if(CheckerBoard.inBounds(newPoint)) {
if(this.getPoint(newPoint).isEmpty()) {
// add new point to blacklist, ensuring we don't get a infinite recursive nightmare
previous.add(newPoint);
// will return at least 1
offsetScores.add(getMaxJumps(newPoint, score + 1, previous));
if(!newPoint.collectionContains(previous)) {
// add new point to blacklist, ensuring we don't get a infinite recursive nightmare
previous.add(new Point(newPoint.x, newPoint.y));
// will return at least 1
offsetScores.add(getMaxJumps(newPoint, score + 1, previous));
}
}
}
}
@@ -158,15 +192,24 @@ class CheckerBoard {
String scan() {return this.scan(this.team1);} // default to Red checkers
String scan(String team) {
List<Point> entries = getCheckers(team);
out.println(entries);
List<Integer> jumps = new ArrayList<Integer>();
// Scan each entry point for jumps
for(Point entry : entries) {
jumps.add(getMaxJumps(entry));
}
// Return the best jump
out.println(jumps);
return Integer.toString(Collections.max(jumps));
// Find the best origin jump and it's score
Point bestpoint = new Point(-1, -1);
int bestscore = 0;
for(int i = 0; i < entries.size(); i++) {
if(bestscore < jumps.get(i)) {
bestscore = jumps.get(i);
bestpoint = entries.get(i);
}
}
return String.format("%s %s %s", bestpoint.x, bestpoint.y, bestscore);
}
}
@@ -185,12 +228,11 @@ class problem12 {
for(int y = 0; y < 8; y++)
rawMatrix[x][y] = line.substring(y, y+1);
}
for(String[] x : rawMatrix)
out.println(Arrays.toString(x));
CheckerBoard cb = new CheckerBoard(rawMatrix);
out.println(cb.scan());
out.println(cb);
out.println(cb.scan());
}
read.close();