请在 2024 年 Clojure 状态调查!中分享您的想法。

欢迎!请参阅 关于页面 获取更多有关如何使用本站的信息。

0
core.rrb-vector

请参阅以下 GitHub 问题的相关测试用例描述:https://github.com/brandonbloom/fipp/issues/53

2 答案

0
_评论者:mfikes_

以下是一个可能更小、或者至少更容易运行的复现。如果你取消注释常规 Clojure 向量调用并注释 {{core.rrb-vector}} 调用,你会看到事物开始正常工作。

如果你调用 {{test-1}},你将得到正确的答案 {{32}}(这来自 https://adventofcode.com/2018/day/9)。

如果你调用 {{test-2}},你会触发错误。以下是一些来自拼图的测试输入


(play 9 25)     ; 32
(play 10 1618)  ; 8317
(play 13 7999)  ; 146373
(play 17 1104)  ; 2764
(play 21 6111)  ; 54718
(play 30 5807)  ; 37305


以下是复现代码


ns advent-2018.day-09
  (:refer-clojure :exclude [subvec vector])
  (:require
   [clojure.core.rrb-vector :as fv]))

(defn vector [& more]
  (apply fv/vector more)
  #_(apply clojure.core/vector more))

(defn catvec [v1 v2]
  (fv/catvec v1 v2)
  #_(into v1 v2))

(defn subvec
  ([v x]
   (fv/subvec v x)
   #_(clojure.core/subvec v x))
  ([v x y]
   (fv/subvec v x y)
   #_(clojure.core/subvec v x y)))

(defn swap [marbles split-ndx]
  (catvec
    (subvec marbles split-ndx)
    (subvec marbles 0 split-ndx)))

(defn rotl [marbles n]
  (swap marbles (mod n (count marbles))))

(defn rotr [marbles n]
  (swap marbles (mod (- (count marbles) n) (count marbles))))

(defn place-marble [marbles marble]
  (let [marbles (rotl marbles 2)]
    [(catvec (vector marble) marbles) 0]))

(defn remove-marble [marbles marble]
  (let [marbles (rotr marbles 7)
         first-marble (nth marbles 0)]
    [(subvec marbles 1) (+ marble first-marble)]))

(defn play-round [marbles round]
  (if (zero? (mod round 23))
    (remove-marble marbles round)
    (place-marble marbles round)))

(defn add-score [scores player round-score]
  (if (zero? round-score)
    scores
    (assoc scores player (+ (get scores player 0) round-score))))

(defn play [players rounds]
  (loop [marbles (vector 0)
         round   1
         player  1
         scores  {}]
    (let [[marbles round-score] (play-round marbles round)
         scores (add-score scores player round-score)]
      (if (> round rounds)
        (apply max (vals scores))
        (recur
         marbles
         (inc round)
         (if (= player players) 1 (inc player))
         scores)))))

(defn test-1 []
  (play 9 25))

(defn test-2 []
  (play 10 1618))

0
参考: https://clojure.atlassian.net/browse/CRRBV-20 (jafingerhut 报告)
...