Sum and product puzzle: Difference between revisions

Line 1,424:
<lang JavaScript>[[4, 13]]</lang>
(Finished in 0.307s)
 
=={{header|jq}}==
'''Works with: gojq''' (the Go implementation of jq)
 
A transcription from the problem statement, with these helper functions:
<lang jq>
# For readability:
def collect(c): map(select(c));
 
# stream-oriented checks:
def hasMoreThanOne(s): [limit(2;s)] | length > 1;
 
def hasOne(s): [limit(2;s)] | length == 1;
 
def prod: .[0] * .[1];
 
## A stream of admissible [x,y] values
def xy:
[range(2;50) as $x # 1 < X < Y < 100
| range($x+1; 101-$x) as $y
| [$x, $y] ] ;
# The stream of [x,y] pairs matching "S knows the sum is $sum"
def sumEq($sum): select( $sum == add );
 
# The stream of [x,y] pairs matching "P knows the product is $prod"
def prodEq($prod): select( $prod == prod );
 
## The solver:
def solve:
xy as $s0
 
# S says P does not know:
| $s0
| collect(add as $sum
| all( $s0[]|sumEq($sum);
prod as $p
| hasMoreThanOne($s0[] | prodEq($p)))) as $s1
 
# P says: Now I know:
| $s1
| collect(prod as $prod | hasOne( $s1[]|prodEq($prod)) ) as $s2
 
# S says: Now I also know
| $s2
| collect(add as $sum | hasOne( $s2[] | sumEq($sum)) ) ;
 
solve
</lang>
{{out}}
<pre>[4,13]</pre>
 
=={{header|Julia}}==
2,442

edits