Last active
February 21, 2016 22:08
-
-
Save antonharald/68bb0cbbebf964264270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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