Comment detail

トランプの和と積のパズル (Nested Flatten)
Squeak Smalltalk で。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| すべての組み合わせ 和の逆引き 積の逆引き 積 答え 根拠 制約 |

積 := [:二要素配列 | 二要素配列 first * 二要素配列 second].

すべての組み合わせ := OrderedCollection new.
(1 to: 13) do: [:first | first to: 13 do: [:second | すべての組み合わせ add: {first. second}]].

和の逆引き := Dictionary new.
積の逆引き := Dictionary new.
すべての組み合わせ do: [:ペア |
    (積の逆引き at: (積 value: ペア) ifAbsentPut: [OrderedCollection new]) add: ペア.
    (和の逆引き at: ペア sum ifAbsentPut: [OrderedCollection new]) add: ペア].

根拠 := すべての組み合わせ select: [:ペア |
    | 和から |
    (和から := 和の逆引き at: ペア sum) size > 1 and: [
        和から allSatisfy: [:各々 | (積の逆引き at: (積 value: 各々)) size > 1]]].

制約 := [:ペア0 |
    ((積の逆引き at: (積 value: ペア0)) count: [:ペア1 |
        (和の逆引き at: ペア1 sum) allSatisfy: [:ペア2 |
            (積の逆引き at: (積 value: ペア2)) size > 1]]) = 1].

答え := (根拠 select: 制約) select: [:ペア | ((和の逆引き at: ペア sum) count: 制約) = 1].

^答え asArray

Index

Feed

Other

Link

Pathtraq

loading...