Skip to content

Instantly share code, notes, and snippets.

@dannyduc
Last active November 8, 2016 19:01
Show Gist options
  • Save dannyduc/e6f7aad009da23c4c1c3d0773209eaea to your computer and use it in GitHub Desktop.
Save dannyduc/e6f7aad009da23c4c1c3d0773209eaea to your computer and use it in GitHub Desktop.
Ages Of Three Children Puzzle Solution http://gohighbrow.com/how-old-are-my-children-solution/
import java.util.*;
public class Main {
public static void main(String[] args) {
int max = 72;
int maxDate = 31;
Set<Ages> agesPerms = getProducPerms(max, maxDate);
Map<Integer, List<Ages>> sumToAgesMap = getSumToAgesMap(agesPerms);
Map<Integer, List<Ages>> sumWithDups = getSumWithDups(sumToAgesMap);
assert (sumWithDups.size() == 1);
int found = 0;
List<Ages> dups = sumWithDups.values().iterator().next();
for (Ages dup : dups) {
if (dup.hasDistinctEldest()) {
found++;
System.out.println(String.format("\nAnswer: %s", dup));
}
}
assert found == 1;
}
private static Map<Integer, List<Ages>> getSumWithDups(Map<Integer, List<Ages>> sumToAgesMap) {
Map<Integer, List<Ages>> sumWithDups = new HashMap<>();
for (Map.Entry<Integer, List<Ages>> entry : sumToAgesMap.entrySet()) {
if (entry.getValue().size() > 1) {
sumWithDups.put(entry.getKey(), entry.getValue());
}
}
return sumWithDups;
}
private static Map<Integer, List<Ages>> getSumToAgesMap(Set<Ages> agesPerms) {
Map<Integer, List<Ages>> m = new HashMap<>();
for (Ages ages : agesPerms) {
int sum = ages.getSum();
if (!m.containsKey(sum)) {
List<Ages> agesList = new ArrayList<>();
m.put(sum, agesList);
}
m.get(sum).add(ages);
}
return m;
}
private static Set<Ages> getProducPerms(int max, int maxDate) {
Set<Ages> agePerms = new HashSet<>();
for (int i = 0; i <= max; i++) {
for (int j = 0; j <= max; j++) {
for (int k = 0; k <= max; k++) {
Ages ages = new Ages(i, j, k);
if (!agePerms.contains(ages)
&& ages.getProduct() == max && ages.getSum() <= maxDate) {
agePerms.add(ages);
String combo = String.format("%s,%s,%s", ages.x, ages.y, ages.z);
System.out.println(String.format("%s - sum(%s) = %s", combo, combo, ages.getSum()));
}
}
}
}
return agePerms;
}
static class Ages {
int x, y, z;
public Ages(int a, int b, int c) {
List<Integer> ascAges = Arrays.asList(a, b, c);
Collections.sort(ascAges);
this.x = ascAges.get(0);
this.y = ascAges.get(1);
this.z = ascAges.get(2);
assert x <= y && y <= z;
}
public boolean hasDistinctEldest() {
return z > y;
}
int getProduct() {
return x*y*z;
}
int getSum() {
return x+y+z;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ages ages = (Ages) o;
if (x != ages.x) return false;
if (y != ages.y) return false;
return z == ages.z;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
result = 31 * result + z;
return result;
}
@Override
public String toString() {
return "Ages{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment