Last active
February 29, 2016 18:53
-
-
Save Isaac-Kleinman/f4f60962390927fa9047 to your computer and use it in GitHub Desktop.
Solution to Jane Street "Think of the Children" Puzzle : https://www.janestreet.com/puzzles/think-of-the-children/
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ThinkOfTheChildren | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var x = Enumerable.Range(1, 18) | |
.Select(i => new | |
{ | |
i = i, | |
jRange = Enumerable.Range(1, i) | |
}) | |
.SelectMany(e => e.jRange, (e, j) => new | |
{ | |
i = e.i, | |
j = j, | |
kRange = Enumerable.Range(1, j) | |
}) | |
.SelectMany(e => e.kRange, (e, k) => new int[] | |
{ e.i, e.j, k }) | |
.GroupBy(triple => triple, new Comparer()) | |
.Where(group => group.Count() > 1 && | |
group | |
.Where(triple => triple[0] > triple[1]) | |
.Count() == 1) | |
.First(group => group.Key | |
.Product() > 72); | |
Console.WriteLine(x.Key.Product()); | |
} | |
} | |
public class Comparer : IEqualityComparer<int[]> | |
{ | |
public bool Equals(int[] a1, int[] a2) | |
{ | |
return a1.Product() == a2.Product() && | |
a1.Sum() == a2.Sum(); | |
} | |
public int GetHashCode(int[] a) | |
{ | |
return 1; | |
} | |
} | |
public static class Extensions | |
{ | |
public static int Product(this int [] arr) | |
{ | |
return arr.Aggregate((product, next) => product * next); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment