diff --git a/uil/october-2013/3/input-generator/main.py b/uil/october-2013/3/input-generator/main.py index 3092b18..4dcd560 100644 --- a/uil/october-2013/3/input-generator/main.py +++ b/uil/october-2013/3/input-generator/main.py @@ -7,11 +7,15 @@ class Node(object): self.g, self.h, self.f = 0, 0, 0 def __eq__(self, other): - assert type(other) == Node, "Can only compare equality against other Node objects" + assert type(other) == Node, "Node cannot compared against type \"{}\"".format(str(type(other))) return self.position == other.position def __repr__(self): return f'' + + @classmethod + def positionify(self, nodes): + return [node.position for node in nodes] # Generate a grid and return it to the user class SnakeGrid(object): @@ -81,6 +85,7 @@ class SnakeGrid(object): def bestPellet(self, curpos, blacklist=[]): potential = self.pellets() if blacklist: + print(curpos, blacklist) potential = list(filter(lambda item : item not in blacklist, potential)) # Returns None if no potential pellets are in due to blacklist if not potential: @@ -91,15 +96,22 @@ class SnakeGrid(object): def merge(self, pos1, pos2): return [pos1[0] + pos2[0], pos1[1] + pos2[1]] + def solution(self, maxdist=20): + path = self.generateSolutions() + for i, pos in enumerate(path): + if self.available(pos): + self.mark(pos, str(i + 1)) + return path + # Generater a solution for the maz - def solution(self, length=20): + def generateSolutions(self, maxdist=30): def build_path(current_node): path = [] current = current_node while current is not None: path.append(current.position) current = current.parent - return str(path[::-1]) + return path[::-1] # Pathfinding initial constants start_node = Node(None, self.positions[-1]) @@ -110,7 +122,7 @@ class SnakeGrid(object): output = "" while len(open_list) > 0: - self.sleep(0.125) + # self.sleep(0.125) pathdist += 1 # Choose the best node to work on current_index, current_node = min(enumerate(open_list), key=lambda item : item[1].f) @@ -119,17 +131,22 @@ class SnakeGrid(object): # Check if we've hit the maximum distance - if pathdist >= length: + if pathdist >= maxdist: return build_path(current_node) # If we've hit the "end node", but still distance to travel, setup a new one if current_node == end_node: finished_end_nodes.append(end_node) - end_node = self.bestPellet(blacklist=finished_end_nodes) + start_node = end_node + open_list = [start_node] + closed_list.append(end_node) + end_node = position=self.bestPellet(current_node.position, blacklist=Node.positionify(finished_end_nodes)) # if we've acquired all pellets by chance if end_node is None: return build_path(current_node) - + else: + end_node = Node(parent=None, position=end_node) + # Basically iterates upon all positions next to the current node dependent on the cardinal directions for offset in self.offsets: child = self.merge(current_node.position, offset) @@ -137,20 +154,18 @@ class SnakeGrid(object): if self.inBounds(child): child = Node(parent=current_node, position=child) + if child in closed_list: + continue + # Ensure not already a closed position - if not child in closed_list: - child.g = current_node.g + 1 - child.h = self.distance(child.position, end_node.position) - child.f = child.g + child.h + child.g = current_node.g + 1 + child.h = self.distance(child.position, end_node.position) + child.f = child.g + child.h # Ensure that child node is - for open_node in open_list: - if child == open_node: - if child.g > open_node.g: - continue - - open_list.append(child) - + if child not in open_list: + open_list.append(child) + return output # Quick method for getting a position with the offsets provided @@ -170,7 +185,8 @@ class SnakeGrid(object): return (self.matrix[pos[0]][pos[1]] == look) if self.inBounds(pos) else False def __repr__(self): - return '\n'.join(' - '.join(_) for _ in self.matrix) + length = max([max(len(item) for item in sub) for sub in self.matrix]) + return '\n'.join(' - '.join(map(lambda item : item.ljust(length) if item != ' ' else (' ' * length), line)) for line in self.matrix) # Driver Code if __name__ == "__main__": @@ -190,10 +206,11 @@ if __name__ == "__main__": # Build and prints matrixes for x in range(iterations): snakegrid = SnakeGrid(size[0], size[1], 3) + solution = snakegrid.solution() print(dividerCustom.format(str(x+1).zfill(len(str(iterations))))) print(snakegrid) print(dividerTotal) - print(snakegrid.solution()) + print(solution) time.sleep(timing) print(dividerTotal) diff --git a/uil/uil-practice-armstrong/UIL.md b/uil/uil-practice-armstrong/UIL.md index ef39864..fc17434 100644 --- a/uil/uil-practice-armstrong/UIL.md +++ b/uil/uil-practice-armstrong/UIL.md @@ -445,4 +445,6 @@ This is a rather long problem, so let's split it up into parts. Class X is a implementation of the Comparable Interface, and is essentially a method for comparing Strings when sorted. Focus on the `compareTo` method. -The `compareTo` method is how comparables "compare", and it returns a integer value to representation how it should be placed in the array. The array is sorted in descending order when `Collections.sort` is called on it. \ No newline at end of file +The `compareTo` method is how comparables "compare", and it returns a integer value to representation how it should be placed in the array. The array is sorted in descending order when `Collections.sort` is called on it. + +The integer it returns is calculated based on the length first: if the two Strings have the same length, it returns the result of the two strings `compareTo` method, but if they're not the same length, it returns the difference in length of the two Strings. \ No newline at end of file diff --git a/uil/uil-practice-armstrong/java/.idea/workspace.xml b/uil/uil-practice-armstrong/java/.idea/workspace.xml index 12913b7..f60e2c4 100644 --- a/uil/uil-practice-armstrong/java/.idea/workspace.xml +++ b/uil/uil-practice-armstrong/java/.idea/workspace.xml @@ -1,7 +1,10 @@ - + + + + - - + + + @@ -65,6 +87,9 @@ + + + + + @@ -108,7 +136,7 @@ - + @@ -152,8 +180,9 @@ + - + @@ -162,7 +191,7 @@ - + @@ -170,7 +199,7 @@ - + @@ -187,10 +216,11 @@ - - + + + diff --git a/uil/uil-practice-armstrong/java/out/production/java/X.class b/uil/uil-practice-armstrong/java/out/production/java/X.class index ff41bf3..7d89250 100644 Binary files a/uil/uil-practice-armstrong/java/out/production/java/X.class and b/uil/uil-practice-armstrong/java/out/production/java/X.class differ diff --git a/uil/uil-practice-armstrong/java/out/production/java/question26.class b/uil/uil-practice-armstrong/java/out/production/java/question26.class index 8822f79..6a46af3 100644 Binary files a/uil/uil-practice-armstrong/java/out/production/java/question26.class and b/uil/uil-practice-armstrong/java/out/production/java/question26.class differ diff --git a/uil/uil-practice-armstrong/java/src/question26.java b/uil/uil-practice-armstrong/java/src/question26.java index 1fe831e..a2134f1 100644 --- a/uil/uil-practice-armstrong/java/src/question26.java +++ b/uil/uil-practice-armstrong/java/src/question26.java @@ -1,5 +1,6 @@ import static java.lang.System.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -16,6 +17,20 @@ class X implements Comparable { public String toString() { return this.str; } + public int[] getValues() { + int[] values = new int[this.str.length()]; + for(int i = 0; i < this.str.length(); i++) { + values[i] = (int) this.str.charAt(i); + } + return values; + } + public int getSum() { + int sum = 0; + for(int val : this.getValues()) { + sum += val; + } + return sum; + } } class question26 { @@ -26,5 +41,6 @@ class question26 { w.add(new X(sub)); Collections.sort(w); out.println(w); + out.println(Arrays.toString(new X("Hello").getValues())); } } \ No newline at end of file