Skip to main content
added 9 characters in body
Source Link
Carcigenicate
  • 3.6k
  • 2
  • 22
  • 30
(defn count-single-eights [td-array width height]
  ; Define three helpershelper functions. One generates a list of coords for a given range of dimensions, another checks if an eight is
  ; at the given coord, and the other counts how many neighbors around a coord are an eight
  (letfn [(coords [x-min x-max y-min y-max]
            (for [y (range y-min y-max)
                  x (range x-min x-max)]
              [x y]))
          (eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
            (count (filter eight?
                           (coords (dec cx) (+ cx 2), (dec cy) (+ cy 2)))))]

    ; Gen a list of each coord of the matrix
    (->> (coords 0 width, 0 height)

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]
(defn count-single-eights [td-array width height]
  ; Define three helpers. One generates a list of coords for a given range of dimensions, another checks if an eight is
  ; at the given coord, and the other counts how many neighbors around a coord are an eight
  (letfn [(coords [x-min x-max y-min y-max]
            (for [y (range y-min y-max)
                  x (range x-min x-max)]
              [x y]))
          (eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
            (count (filter eight?
                           (coords (dec cx) (+ cx 2), (dec cy) (+ cy 2)))))]

    ; Gen a list of each coord of the matrix
    (->> (coords 0 width, 0 height)

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]
(defn count-single-eights [td-array width height]
  ; Define three helper functions. One generates a list of coords for a given range of dimensions, another checks if an eight is
  ; at the given coord, and the other counts how many neighbors around a coord are an eight
  (letfn [(coords [x-min x-max y-min y-max]
            (for [y (range y-min y-max)
                  x (range x-min x-max)]
              [x y]))
          (eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
            (count (filter eight?
                           (coords (dec cx) (+ cx 2), (dec cy) (+ cy 2)))))]

    ; Gen a list of each coord of the matrix
    (->> (coords 0 width, 0 height)

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]
added 43 characters in body
Source Link
Carcigenicate
  • 3.6k
  • 2
  • 22
  • 30

Clojure, 227227 198 bytes

(fn[t w h](let[elet[c #(for[y(range %3 %4)x(range % %2)][x y])e #(= 8(get-in t(reverse %)0))m(fn[[o p]](reducecount(fn [nfilter c](if(e c)(inc n)n))0(for[y(rangec(dec po)(+ po 2))x(range(dec op)(+ op 2))][x y])))](->> count(for[yfilter #(range= h)x1(rangem w%)][x y])(filter e)(filterc #(=0 1(mw %)0 h))(count))))

Ouch. Definitely not the shortest here by any means. 54 bytes of parenthesis is killer. I'm still relatively happy with it though. I think I could probably make

-29 bytes by creating a couple "shortcuts"helper function that reduce some duplication and get rid ofgenerates a range since I was doing that twice, changing the need for reduce to a (count (filter setup, but I doubt I'mand getting much better than 200 bytesrid of the threading macro after golfing.

(defn count-single-eights [td-array width height]
  ; Define twothree helpers. One generates a list of coords for a given range of dimensions, another checks if an eight is
  ; at the given coord,
  ;  and the other counts how many neighbors around a coord are an eight
  (letfn [(eight? [[x y]] (= 8coords (get[x-inmin tdx-array [y x] 0)))
         max (n-eightsy-around [[cxmin cy]]y-max]
             (reduce (fn [n coord]
                     for [y (ifrange (eight?y-min coordy-max)
                        x (incrange nx-min x-max)]
                        [x ny]))
            (eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
         (for [y (range (dec cy)count (+ cyfilter 2))eight?
                           x (rangecoords (dec cx) (+ cx 2))]
                   , (dec cy) (+ [xcy y]2)))))]

    ; Gen a list of each coord of the matrix
    (->> (for [y (range height)
               xcoords (range0 width)]
          , [x0 y]height)

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]

Try it online!Try it online!

Clojure, 227 bytes

(fn[t w h](let[e #(= 8(get-in t(reverse %)0))m(fn[[o p]](reduce(fn [n c](if(e c)(inc n)n))0(for[y(range(dec p)(+ p 2))x(range(dec o)(+ o 2))][x y])))](->> (for[y(range h)x(range w)][x y])(filter e)(filter #(= 1(m %)))(count))))

Ouch. Definitely not the shortest here by any means. 54 bytes of parenthesis is killer. I'm still relatively happy with it though. I think I could probably make a couple "shortcuts" that reduce some duplication and get rid of the need for reduce, but I doubt I'm getting much better than 200 bytes.

(defn count-single-eights [td-array width height]
  ; Define two helpers. One checks if an eight is at the given coord,
  ;  and the other counts how many neighbors around a coord are an eight
  (letfn [(eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
             (reduce (fn [n coord]
                       (if (eight? coord)
                         (inc n)
                         n))
                     0
                     (for [y (range (dec cy) (+ cy 2))
                           x (range (dec cx) (+ cx 2))]
                       [x y])))]

    ; Gen a list of each coord of the matrix
    (->> (for [y (range height)
               x (range width)]
           [x y])

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]

Try it online!

Clojure, 227 198 bytes

(fn[t w h](let[c #(for[y(range %3 %4)x(range % %2)][x y])e #(= 8(get-in t(reverse %)0))m(fn[[o p]](count(filter e(c(dec o)(+ o 2)(dec p)(+ p 2)))))](count(filter #(= 1(m %))(filter e(c 0 w 0 h))))))

Ouch. Definitely not the shortest here by any means. 54 bytes of parenthesis is killer. I'm still relatively happy with it though.

-29 bytes by creating a helper function that generates a range since I was doing that twice, changing the reduce to a (count (filter setup, and getting rid of the threading macro after golfing.

(defn count-single-eights [td-array width height]
  ; Define three helpers. One generates a list of coords for a given range of dimensions, another checks if an eight is
  ; at the given coord, and the other counts how many neighbors around a coord are an eight
  (letfn [(coords [x-min x-max y-min y-max]
            (for [y (range y-min y-max)
                  x (range x-min x-max)]
              [x y]))
          (eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
            (count (filter eight?
                           (coords (dec cx) (+ cx 2), (dec cy) (+ cy 2)))))]

    ; Gen a list of each coord of the matrix
    (->> (coords 0 width, 0 height)

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]

Try it online!

Source Link
Carcigenicate
  • 3.6k
  • 2
  • 22
  • 30

Clojure, 227 bytes

(fn[t w h](let[e #(= 8(get-in t(reverse %)0))m(fn[[o p]](reduce(fn [n c](if(e c)(inc n)n))0(for[y(range(dec p)(+ p 2))x(range(dec o)(+ o 2))][x y])))](->> (for[y(range h)x(range w)][x y])(filter e)(filter #(= 1(m %)))(count))))

Ouch. Definitely not the shortest here by any means. 54 bytes of parenthesis is killer. I'm still relatively happy with it though. I think I could probably make a couple "shortcuts" that reduce some duplication and get rid of the need for reduce, but I doubt I'm getting much better than 200 bytes.

(defn count-single-eights [td-array width height]
  ; Define two helpers. One checks if an eight is at the given coord,
  ;  and the other counts how many neighbors around a coord are an eight
  (letfn [(eight? [[x y]] (= 8 (get-in td-array [y x] 0)))
          (n-eights-around [[cx cy]]
             (reduce (fn [n coord]
                       (if (eight? coord)
                         (inc n)
                         n))
                     0
                     (for [y (range (dec cy) (+ cy 2))
                           x (range (dec cx) (+ cx 2))]
                       [x y])))]

    ; Gen a list of each coord of the matrix
    (->> (for [y (range height)
               x (range width)]
           [x y])

         ; Remove any coords that don't contain an eight
         (filter eight?)

         ; Then count how many "neighborhoods" only contain 1 eight
         (filter #(= 1 (n-eights-around %)))
         (count))))

(mapv #(count-single-eights % (count (% 0)) (count %))
      test-cases)
=> [3 0 0 2 2 1 3 1 4 3]

Where test-cases is an array holding all the "Python test cases"

Try it online!