Created
October 28, 2020 21:47
-
-
Save kclejeune/8908f9e5ab5d519de5fff8ada91c72d3 to your computer and use it in GitHub Desktop.
This file contains 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.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class Solver { | |
public static void main(String[] args) { | |
// quit if no input argument is provided | |
if (args.length == 0) { | |
System.out.println("No input file provided."); | |
System.exit(-1); | |
} | |
final ArrayList<String[]> lines = new ArrayList<>(); | |
final File file = new File(args[0]); | |
try (final Scanner scan = new Scanner(file)) { | |
// read the file commands into a list of commands | |
// and tokenize them into String[] | |
while (scan.hasNextLine()) { | |
// tokenize the line with whitespace | |
final String[] line = scan.nextLine().trim().split("\\s+"); | |
// ignore blank lines | |
if (line.length > 0) { | |
lines.add(line); | |
} | |
} | |
} catch (FileNotFoundException e) { | |
System.out.printf("%s could not be found.", file.toString()); | |
e.printStackTrace(); | |
} | |
// handle commands | |
for (String[] line : lines) { | |
try { | |
// handle input commands randomizeState, setState, etc. in the switch block | |
final String command = line[0]; | |
if (command.equalsIgnoreCase("randomizeState")) { | |
final int moveCount = Integer.parseInt(line[1]); | |
// apply randomizeState function | |
} else if (command.equalsIgnoreCase("setState")) { | |
// access the puzzle rows in line[1], line[2], line[3] | |
// for input such as "b12 345 678" | |
// call setState using these values | |
} else if (command.equalsIgnoreCase("move")) { | |
final String direction = line[1]; | |
// move the puzzle in the specified direction | |
} else if (command.equalsIgnoreCase("printState")) { | |
// print the state of the puzzle | |
} else if (command.equalsIgnoreCase("maxNodes")) { | |
final int maxNodes = Integer.parseInt(line[1]); | |
// set the maximum nodes | |
} else if (command.equalsIgnoreCase("solve")) { | |
final String algorithm = line[1]; | |
if (algorithm.equalsIgnoreCase("A-star")) { | |
// h1 or h2 | |
final String heuristic = line[2]; | |
// call A-star here with the specified heuristic | |
} else if (algorithm.equalsIgnoreCase("beam")) { | |
final int k = Integer.parseInt(line[2]); | |
} else { | |
System.out.printf("Invalid algorithm: %s", algorithm); | |
} | |
} else { | |
System.out.printf("Invalid Command: %s", command); | |
} | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains 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
from sys import argv | |
import os | |
def main(): | |
if len(argv) == 1: | |
raise ValueError("No input file provided") | |
input_file = os.path.realpath(argv[1]) | |
if not os.path.exists(input_file): | |
raise ValueError("Input file could not be found.") | |
# read file lines into a list | |
with open(input_file, "r") as f: | |
lines = [ | |
line.strip().split() for line in f.readlines() if len(line.strip()) != 0 | |
] | |
for line in lines: | |
try: | |
command = line[0] | |
if command == "randomizeState": | |
move_count = int(line[1]) | |
print(f"move_count={move_count}") | |
elif command == "setState": | |
# for state b12 345 678, rows[0] = "b12", rows[1] = "345", etc. | |
rows = line[1:] | |
# set the state using the given rows here | |
elif command == "move": | |
direction = line[1] | |
# call stuff to move the puzzle here | |
elif command == "printState": | |
# call stuff to print the state here | |
pass | |
elif command == "maxNodes": | |
max_nodes = int(line[1]) | |
# set this value in your puzzle state here | |
elif command == "solve": | |
algorithm = line[1] | |
if algorithm == "A-star": | |
heuristic = line[2] | |
# call A* with the specified heuristic here | |
elif algorithm == "beam": | |
k = int(line[2]) | |
# call beam search here with the specified number of successors to expand at each step | |
else: | |
print(f"Invalid algorithm: {algorithm}") | |
else: | |
print(f"Invalid Command: {command}") | |
except Exception: | |
print("An error occurred.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment