Skip to content

Instantly share code, notes, and snippets.

@antonharald
Last active February 21, 2016 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonharald/68bb0cbbebf964264270 to your computer and use it in GitHub Desktop.
Save antonharald/68bb0cbbebf964264270 to your computer and use it in GitHub Desktop.
(ns flip.core
(:gen-class)
(:require [clojure.core.matrix :as m]))
; a 1000 x 100 grid with random characters
(def grid (m/emap (fn [_] (rand-nth (seq "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
(m/new-matrix 1000 100)))
(defn abc? [xs]
"consecutive A B C"
(= '(\A \B \C) xs))
(defn find-consecutive-abc [pos-setter fields]
(not-empty (keep-indexed (fn [index segment]
(when (abc? segment)
(map-indexed (fn [i x] {:pos (pos-setter (+ index i)) :char x}) segment)))
(partition 3 1 fields))))
(defn flip [f & xs]
(apply f (reverse xs)))
(defn -main [& args]
"prints all all consecutive occurences of A-B-C, wheter vertically or horizontally"
(let [vertically (keep-indexed #(find-consecutive-abc (partial vector %1) %2) (m/columns grid))
horizontally (keep-indexed #(find-consecutive-abc (partial flip vector %1) %2) (m/rows grid))]
(doseq [a (concat vertically horizontally)]
(println a))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment