Skip to content

Instantly share code, notes, and snippets.

@exoego
Last active December 19, 2015 02:39
Show Gist options
  • Save exoego/5884390 to your computer and use it in GitHub Desktop.
Save exoego/5884390 to your computer and use it in GitHub Desktop.
Using Queen to solve the math puzzle "SEND + MORE = MONEY"
final int m = 1;
List<Integer> answer = range(9, 0).except(m).permutations(7).first(new Predicate<List<Integer>>() {
@Override
public boolean evaluate(final List<Integer> perm) {
int s = perm.get(0);
int e = perm.get(1);
int n = perm.get(2);
int d = perm.get(3);
int o = perm.get(4);
int r = perm.get(5);
int y = perm.get(6);
return _(s, e, n, d) + _(m, o, r, e) == _(m, o, n, e, y);
}
private int _(int a, int b, int c, int d) {
return 1000 * a + 100 * b + 10 * c + d;
}
private int _(int a, int b, int c, int d, int e) {
return _(a, b, c, d) * 10 + e;
}
}).getOrElse(Collections.<Integer>emptyList());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment