Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Developed with pleasure :)<br>
- * User: HoD<br>
- * Date: 27.05.11<br>
- * Time: 20:22<br>
- */
- object Riddle {
- implicit def enhance(t: (Int, Int)) = new {
- def sum = t._1 + t._2
- def product = t._1 * t._2
- def difference = math.abs(t._1 - t._2)
- }
- implicit def enhance[T](t: Traversable[T]) = new {
- def hasOneElement = t.size == 1
- def hasMoreThanOneElement = t.size > 1
- }
- def main(args: Array[String]) {
- val allPossiblePairs = {
- for (first <- 1 to 1000; second <- first to 1000) yield {
- first -> second
- }
- }
- //peter: don't know the solution
- val afterStep1 = {
- allPossiblePairs.groupBy(_.product).filter(_._2.hasMoreThanOneElement)
- }
- println("remaining " + afterStep1.values.flatten.size)
- //simon: i already knew
- val afterStep2 = {
- //simon: i don't know the solution
- val bySum = allPossiblePairs.groupBy(_.sum).filter(_._2.hasMoreThanOneElement)
- // -> every pair must not have a unique product
- val ambiguousProducts = afterStep1.keySet
- bySum.filter(_._2.forall(e => ambiguousProducts.contains(e.product)))
- }
- println("remaining " + afterStep2.values.flatten.size)
- //peter: now i know
- val afterStep3 = {
- // -> find all groups that contain a pair that has a unique product inside that group
- afterStep2.values.flatten.groupBy(_.product).filter(_._2.hasOneElement)
- }
- println("remaining " + afterStep3.values.flatten.size)
- //simon: now i know, too
- val afterStep4 = {
- // -> find all groups that contain a pair that has a unique sum inside that group
- afterStep3.values.flatten.groupBy(_.sum).filter(_._2.hasOneElement)
- }
- println("remaining " + afterStep4.values.flatten.size)
- //daniel: still not sure
- val afterStep5 = {
- val diff2Pairs = afterStep4.values.flatten.map(e => e.difference -> e).groupBy(_._1).map(e => e._1 -> e._2.map(_._2))
- diff2Pairs.filter(_._2.hasMoreThanOneElement)
- }
- println("possible differences daniel might know: " + afterStep5.keys)
- //to be able to suspect a number, there has to be a number that appears more often than all others
- val diff2NumberCount = {
- val diff2Numbers = afterStep5.map(e => e._1 -> e._2.flatMap(e => List(e._1, e._2)))
- afterStep5.map(e => e._1 -> diff2Numbers(e._1).groupBy(e => e).map(e => e._1 -> e._2.size))
- }
- println("candidates: " + diff2NumberCount)
- val mapWithHighestCount = diff2NumberCount.values.toList.sortBy(_.values.max).last
- val danielsSuspect = {
- val highestCount = mapWithHighestCount.values.max
- mapWithHighestCount.find(_._2 == highestCount).get
- }
- println("daniels suspect is: " + danielsSuspect._1)
- //since we know daniels suspect, we know the difference he knows
- val danielsDiff = {
- diff2NumberCount.find(_._2.exists(_ == danielsSuspect)).get
- }
- println("daniels diff: " + danielsDiff._1)
- println("solution:" + afterStep5(danielsDiff._1).find(!_.productIterator.contains(danielsSuspect._1)).get)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement