public
Last active

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
(ns dojo.core
(:use [clojure.set]
[clojure.java.io]))
 
(def digits [
" _ _ _ _ _ _ _ "
" | _| _||_||_ |_ ||_||_|"
" ||_ _| | _||_| ||_| _|"])
 
(defn read-file [rdr]
(line-seq rdr))
 
 
(defn chunk-in-threes [line]
(partition 9 line))
 
;(doall (map println digits))
 
(defn nth-digit [lines n]
"0-indexed."
(let [x (* 3 n)
x2 (+ 3 x)
chunks (for [y (range 3)] (subs (nth lines y) x x2))]
(vec chunks)))
 
;(doall (map #(println (nth-digit digits %)) (range 9)))
 
(def input-file "/Users/nchubrich/numbers.txt")
 
(defn parse-line-1 [line]
(cond (= " _ " line) #{2 3 5 6 7 8 9}
(= " " line) #{1 4}))
 
(defn parse-line-2 [line]
(cond (= line " |") #{1 7}
(= line " _|") #{2 3}
(= line "|_ ") #{5 6}
(= line "|_|") #{4 8 9}))
(defn parse-line-3 [line]
(cond (= line " |") #{1 7 4}
(= line " _|") #{3 5 9}
(= line "|_ ") #{2}
(= line "|_|") #{6 8}))
 
(defn parse-cell [cell]
(first (intersection (parse-line-1 (cell 0)) (parse-line-2 (cell 1)) (parse-line-3 (cell 2)))))
 
(with-open [rdr (reader input-file)]
(let [lines (read-file rdr)
chunked-lines (chunk-in-threes (doall lines))]
(map (fn [line index] (parse-cell (nth-digit line index))) chunked-lines (range 0 9))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.