Comment detail

四字熟語パズルの作成 (Nested Flatten)
188秒@Pen4 2.6GHz 12118件でした。
頭のハッシュテーブル、尻のハッシュテーブルを作って、そこから頭尻のハッシュテーブルを作る。
結果から回転や反転などの重複を消すのにもハッシュテーブルを使う。
ハッシュテーブルとダンスでもしてな。
 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(use srfi-1)
(use util.combinations)

(define ht-head (make-hash-table 'eqv?))
(define ht-tail (make-hash-table 'eqv?))
(define ht-headtail (make-hash-table 'string=?))

(define (print-puzzle j0 j1 j2 j3)
  (print j0)
  (print (string-ref j2 1) "  " (string-ref j1 1))
  (print (string-ref j2 2) "  " (string-ref j1 2))
  (print j3)
  (newline))

(define (main args)
  (call-with-input-file (cadr args) (lambda (in)
                                      (port-for-each
                                       (lambda (x)
                                         (hash-table-push! ht-head (string-ref x 0) x)
                                         (hash-table-push! ht-tail (string-ref x 3) x))
                                       (lambda () (read-line in)))))
  (hash-table-for-each ht-tail (lambda (key-tail value-tail)
                                 (for-each (lambda (x)
                                             (hash-table-push! ht-headtail (string (string-ref x 0) key-tail) x))
                                           (filter (lambda (x) (hash-table-exists? ht-head (string-ref x 0))) value-tail))))
  (let ((all-keys (hash-table-keys ht-headtail))
        (ht-dup (make-hash-table 'equal?)))
    (for-each (lambda (w0)
                (for-each (lambda (w3)
                            (let ((w1 (string (string-ref w0 1) (string-ref w3 1)))
                                  (w2 (string (string-ref w0 0) (string-ref w3 0))))
                              (when (and (not (char=? (string-ref w1 0) (string-ref w3 0)))
                                         (hash-table-exists? ht-headtail w1)
                                         (hash-table-exists? ht-headtail w2))
                                (hash-table-put! ht-dup (sort (list w0 w1 w2 w3)) (list w0 w1 w2 w3)))))
                          all-keys))
              all-keys)
    (hash-table-for-each ht-dup (lambda (key value)
                                  (for-each (lambda (x) (apply print-puzzle x))
                                            (cartesian-product (map (lambda (x)
                                                                      (hash-table-get ht-headtail x))
                                                                    value)))))))

Index

Feed

Other

Link

Pathtraq

loading...