Created
August 29, 2012 04:12
-
-
Save naylor127/3506742 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 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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment