Created
October 27, 2013 17:02
-
-
Save johnwalker/7185022 to your computer and use it in GitHub Desktop.
Works
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 ieee2013.icecream) | |
(require 'clojure.set) | |
(defn- dfs | |
[graph goal] | |
(fn search | |
[path visited] | |
(let [current (peek path)] | |
(if (= goal current) | |
[path] | |
(->> current | |
graph | |
(remove visited) | |
(mapcat #(search (conj path %) (conj visited %)))))))) | |
(defn findpath | |
[graph start goal] | |
((dfs graph goal) [start] #{start})) | |
(defn read-in [] | |
(->> (for [line (line-seq (java.io.BufferedReader. *in*)) | |
:while (not= line "A A")] (str line " ")) | |
(doall) | |
(apply str))) | |
(defn prstrln [& x] | |
(println (apply str x))) | |
(defn problem [] | |
(let [input (read-in) | |
inputlist (->> (clojure.string/split input #"\s") | |
(filter (complement empty?)) | |
(map keyword)) | |
target (first inputlist) | |
undirected-edges1 (partition 2 (rest inputlist)) | |
undirected-edges2 (map reverse undirected-edges1) | |
bidirectional-edges (into undirected-edges1 undirected-edges2) | |
edges (->> bidirectional-edges | |
(map #(hash-map (first %1) #{(second %1)}))) | |
graph (reduce #(merge-with clojure.set/union %1 %2) {} edges) | |
res (findpath graph :F target) | |
shortest-length (reduce #(min %1 (count %2)) 50000 res)] | |
(if (empty? res) | |
(prstrln "No Route Available from F to " (name target)) | |
(do | |
(prstrln "Total Routes: " (count res)) | |
(prstrln "Shortest Route Length: " shortest-length) | |
(let [shortest-paths (filter #(= (count %1) shortest-length) res) | |
shortest-alphabetical (-> (map #(clojure.string/join " " (map name %1)) shortest-paths) | |
(sort) | |
(first))] | |
(println (str "Shortest Route after Sorting of Routes of length " shortest-length ": " shortest-alphabetical))))))) | |
(problem) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment