LoginSignup
2
2

More than 5 years have passed since last update.

POH 「俺の許嫁と幼なじみが修羅場すぎる」をClojureで解く

Last updated at Posted at 2015-05-10

前書き

いつの間にかClojureが使用できるようになっていたので、俺の許嫁と幼なじみが修羅場すぎるを解いてみました。

Mission1

(require '[clojure.string :as str])
(use '[clojure.java.io])

(doseq [line (line-seq (reader *in*))]
  (println (str/join
    (map #(nth % 1) (filter #(even? (nth % 0))
      (map-indexed #(vector %1 %2) (str/split line #"")))))))

結果:
 提出言語:Clojure(Beta)
 得点:100 点

 結果:
 テストケース1:success 1.22秒
 テストケース2:success 1.17秒
 テストケース3:success 1.2秒
 テストケース4:success 1.21秒
 テストケース5:success 1.16秒

...Clojure遅い。
おそらく1秒は起動時のcoreの読み込みに使っているはず。

Mission2

(use '[clojure.java.io])
(doseq [line (reduce #(map + %1 %2)
  (partition 7 (map read-string
    (rest (line-seq (reader *in*))))))] (println line))

結果:
 提出言語:Clojure(Beta)
 得点:100 点

 結果:
 テストケース1:success 1.15秒
 テストケース2:success 1.25秒
 テストケース3:success 1.22秒
 テストケース4:success 1.19秒
 テストケース5:success 1.16秒

Mission3

(println "RENA")

結果:
 提出言語:Clojure(Beta)
 得点:100 点

 結果:
 テストケース1:success 1.11秒

Mission RENA

(use '[clojure.java.io])
(require '[clojure.string :as str])

(defn str-to-numseq [s]
  (map read-string (str/split s #" ")))

(defn fr-pair [v]
  (map #(vector (first v) %) (rest v)))

(defn reduce-concat [v]
  (reduce #(concat %1 %2) v))

(defn combine [s1 s2]
  (reduce-concat
    (map #(fr-pair (cons % s1)) s2)))

(defn seq-to-grid [s]
  (let [[x1 y1 x2 y2] s]
    (combine (range x1 (+ 1 x2)) (range y1 (+ 1 y2)))))

(defn m-val [x y m]
  (nth (nth m (- x 1)) (- y 1)))

(def input-data (line-seq (reader *in*)))

(def settings (-> input-data first str-to-numseq))
(def width (first settings))
(def height (second settings))

(def num-matrix
  (map #(str-to-numseq %) (take height (rest input-data))))

(def selectors
  (distinct (reduce-concat
     (map seq-to-grid
          (map str-to-numseq (drop (+ 1 height) input-data))))))

(def result
  (reduce + (map #(m-val (first %) (last %) num-matrix) selectors)))

(println result)

結果:
 提出言語:Clojure(Beta)
 得点:100 点

 結果:
 テストケース1:success 1.32秒
 テストケース2:success 1.28秒
 テストケース3:success 1.34秒
 テストケース4:success 1.28秒
 テストケース5:success 1.28秒

今回は対象のマスを全部求めてから重複を弾いて集計する流れにしたけれど、各マスごとにフラグ立ててから集計した方がおそらく早いと思われる。

感想

速さを求めるためのチューニングをしていないので、全体的にけっこう遅い。
あと、Clojureはテストケースごとに新たに実行環境を起動しているようなので、その分のオーバーヘッドが1秒ほどあるように思われる。(Mission3でも1.1秒かかっているため)
全体的に不利かも。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2