Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.WeakHashMap;
- public class KenKen {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner in = new Scanner(new File("KenKen.dat"));
- while (in.hasNextLine()) {
- //String line = in.nextLine();
- Map<String, Integer> map = new WeakHashMap<String, Integer>();
- List<Integer> intsList = new ArrayList<Integer>() {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- {
- add(1);
- add(2);
- add(3);
- add(4);
- add(5);
- add(6);
- }
- };
- String line1 = in.nextLine();
- String line2 = in.nextLine();
- String line3 = in.nextLine();
- System.out.println(line1 + " " + line2 + " " + line3);
- map.put(line1.split(" ")[0],
- Integer.parseInt(line1.split(" ")[1]));
- map.put(line2.split(" ")[0],
- Integer.parseInt(line2.split(" ")[1]));
- map.put(line3.split(" ")[0],
- Integer.parseInt(line3.split(" ")[1]));
- if (in.hasNextLine())
- in.nextLine();
- System.out.println("Size: " + map.size());
- List<String> solutions = new ArrayList<String>();
- List<Integer> usedNums = new ArrayList<Integer>();
- for (Entry<String, Integer> entry : map.entrySet()) {
- System.out.println(entry.getKey() + " " + entry.getValue());
- innerloop: for (int i = 0; i < intsList.size(); i++) {
- int num1 = intsList.get(i);
- for (int j = 0; j < intsList.size(); j++) {
- int num2 = intsList.get(j);
- if (makesNum(num1, num2, entry.getValue(),
- entry.getKey())
- && !usedNums.contains(num1)
- && !usedNums.contains(num2)) {
- String s = num1 + " " + num2;
- solutions.add(s);
- System.out.println("Found solution for "
- + entry.getKey() + " " + entry.getValue()
- + ". It's " + num1 + " " + num2);
- usedNums.add(num1);
- usedNums.add(num2);
- break innerloop;
- }
- }
- }
- }
- if (solutions.size() == 3) {
- System.out.println("Possible.");
- } else {
- System.out.println("Not Possible.");
- }
- System.out.println("\n");
- }
- in.close();
- }
- private static boolean makesNum(int i, int j, int num, String op) {
- if (op.equalsIgnoreCase("+"))
- return i + j == num;
- else if (op.equalsIgnoreCase("-"))
- return (i - j == num) || (j - i == num);
- else if (op.equalsIgnoreCase("*"))
- return i * j == num;
- else {
- return ((((double)i) / j) == (double) num) || ((((double)j) / i) == (double) num);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement