Skip to content

Instantly share code, notes, and snippets.

@djanatyn
Created August 14, 2012 03:54
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 djanatyn/3346147 to your computer and use it in GitHub Desktop.
Save djanatyn/3346147 to your computer and use it in GitHub Desktop.
salesman
(ns salesman.core
"finds the quickest path through cities"
(:require [clojure.core.logic :as l]
[clojure.contrib.math :as m]))
(def sample-city {:x 0 :y 0})
(def other-city {:x 0 :y 0})
(defn random-city []
{:x (int (rand 10)) :y (int (rand 10))})
(defn random-world [num-cities]
(take num-cities (repeatedly random-city)))
(defn distance [city-one city-two]
(let [square (fn [x] (* x x))]
(m/sqrt (+ (square (- (:x city-one) (:x city-two))) (square (- (:y city-one) (:y city-two)))))))
(def little-world (random-world 3))
(defn move-to-front [first others]
"Moves an item to the front of the list"
(cons first (remove #(identical? first %) others)))
(defn first-iteration [list]
"Returns a list of all first-level permutations."
(map #(recursive-thingy % list) list))
(defn second-iteration [list] (cons (first list) (first-iteration (next list))))
;; salesman.core> (first-iteration [1 2 3 4 5])
;; ((1 2 3 4 5) (2 1 3 4 5) (3 1 2 4 5) (4 1 2 3 5) (5 1 2 3 4))
;; salesman.core> (map second-iteration (first-iteration [1 2 3 4 5]))
;; ((1 (2 3 4 5) (3 2 4 5) (4 2 3 5) (5 2 3 4)) (2 (1 3 4 5) (3 1 4 5) (4 1 3 5) (5 1 3 4)) (3 (1 2 4 5) (2 1 4 5) (4 1 2 5) (5 1 2 4)) (4 (1 2 3 5) (2 1 3 5) (3 1 2 5) (5 1 2 3)) (5 (1 2 3 4) (2 1 3 4) (3 1 2 4) (4 1 2 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment