Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs
 and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"

Update: I didn't read other answers beforehand (wanted to figure it out myself), but it looks like I've got basically the same thing as mikera mikera. So maybe not a whole lot to learn here :)

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs
 and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"

Update: I didn't read other answers beforehand (wanted to figure it out myself), but it looks like I've got basically the same thing as mikera. So maybe not a whole lot to learn here :)

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs
 and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"

Update: I didn't read other answers beforehand (wanted to figure it out myself), but it looks like I've got basically the same thing as mikera. So maybe not a whole lot to learn here :)

Rename diff to same-pos. Fix incorrect comment.
Source Link

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs
 and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"

Update: I didn't read other answers beforehand (wanted to figure it out myself), but it looks like I've got basically the same thing as mikera . So maybe not a whole lot to learn here :)

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs
 and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs
 and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"

Update: I didn't read other answers beforehand (wanted to figure it out myself), but it looks like I've got basically the same thing as mikera . So maybe not a whole lot to learn here :)

Rename diff to same-pos. Fix incorrect comment.
Source Link

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn diffsame-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g. (diff
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (diffsame-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) //; printswill "i2+r"print "Answer: i2+r"

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn diff 
 "Return all the elements of xs and ys that match, position-wise. e.g. (diff
 [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (diff x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str answer)) // prints "i2+r"

Here is my version. Works on any seq (not just strings) and is fully lazy.

(def data (slurp "data.txt"))
(defn same-pos 
 "Return all the elements of xs and ys that match, position-wise. e.g.
 (same-pos [\\a \\x \\c] [\\a \\r \\c]) returns [\\a \\c]. Returns nil if xs and ys have different lengths."
 [xs ys]
 (when (= (count xs) (count ys))
 (filter (complement nil?) (map #(if (= %1 %2) %1) xs ys))))
(def answer
 (let [ps (partition 5 1 data)] ; build seq of sliding 5-tuples
 (->> (for [x ps y ps] (same-pos x y)) ; n^2 FTW
 (filter (comp (partial = 4) count)) ; 4 of 5 positions must match
 (first))))
(println (apply str "Answer: " answer)) ; will print "Answer: i2+r"
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /