Skip to content

Instantly share code, notes, and snippets.

@jemand771
Last active October 6, 2018 11:41
Show Gist options
  • Save jemand771/35e24c1e394dae310f437580522d4dde to your computer and use it in GitHub Desktop.
Save jemand771/35e24c1e394dae310f437580522d4dde to your computer and use it in GitHub Desktop.
This will solve the "crack the code" riddle at https://www.instagram.com/p/BoeZ7IvC5gM/ (its 042)
import java.util.ArrayList;
public class CrackTheCode2 {
public static void main(String[] args) {
int totalPoss = 0;
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
if (wellPlaced(a, b, c, 6, 8, 2) != 1) continue;
if (wrongPlaced(a, b, c, 6, 1, 4) != 1) continue;
if (wrongPlaced(a, b, c, 2, 0, 6) != 2) continue;
if (wrongPlaced(a, b, c, 7, 3, 8) != 0) continue;
if (wrongPlaced(a, b, c, 7, 8, 0) != 1) continue;
totalPoss++;
System.out.println(numberFromDigits(a, b, c));
}
}
}
System.out.println();
System.out.println("Total: " + totalPoss);
}
public static int wrongPlaced(int a, int b, int c, int a1, int b1, int c1) {
if (wellPlaced(a, b, c, a1, b1, c1) != 0) return -1;
ArrayList<Integer> digits = new ArrayList<>();
digits.add(a);
digits.add(b);
digits.add(c);
int count = 0;
if (digits.contains(a1)) count++;
if (digits.contains(b1)) count++;
if (digits.contains(c1)) count++;
return count;
}
public static int wellPlaced(int a, int b, int c, int a1, int b1, int c1) {
int count = 0;
if (a == a1) count++;
if (b == b1) count++;
if (c == c1) count++;
return count;
}
public static String numberFromDigits(int a, int b, int c) {
return String.valueOf(a) + String.valueOf(b) + String.valueOf(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment