Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Campbell
committed
Dec 11, 2011
1 parent
0cd249d
commit 6187dff
Showing
2 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns aichallenge.collisions | ||
(:use [clojure.pprint :only (pprint)] | ||
[clojure.set :only (difference)]) | ||
(:require [aichallenge.ant :as ant])) | ||
|
||
(defn fpprint [filename data] | ||
(spit (java.io.File. filename) | ||
(with-out-str (pprint data)))) | ||
|
||
(defn init-matrix [x y] | ||
(letfn [(row [] (vec (take x (repeat 0))))] | ||
(vec (repeatedly y row)))) | ||
|
||
(defn collisions [moves] | ||
(loop [m {} | ||
ms moves] | ||
(if (seq ms) | ||
(recur (let [move (first ms) | ||
next-loc (apply ant/move-ant move)] | ||
(if (m next-loc) | ||
(update-in m [next-loc] conj move) | ||
(assoc m next-loc [move]))) | ||
(next ms)) | ||
m))) | ||
|
||
(def directions #{:north :south :east :west}) | ||
|
||
(defn find-alternative [m ant dirs] | ||
(let [poss (map (fn [ant dir] | ||
[ant dir (ant/move-ant ant dir)]) | ||
(repeat ant) | ||
dirs)] | ||
(first (remove (fn [[_ _ next-move]] (m next-move)) | ||
poss)))) | ||
|
||
|
||
(defn all-alternatives [collisions] | ||
(for [collision collisions | ||
:let [moves (val collision)] | ||
:when (> (count moves) 1) | ||
[ant dir] moves] | ||
(find-alternative collisions ant (difference directions #{dir})))) | ||
|
||
(defn fix-collisions [matrix moves] | ||
(let [cs (collisions moves) | ||
alts (reduce (fn [m [ant dir _]] | ||
(assoc m ant dir)) {} | ||
(all-alternatives cs))] | ||
(for [[ant dir] moves] | ||
[ant (if-let [new-dir (alts ant)] | ||
new-dir | ||
dir)]))) | ||
|
||
#_(zipmap (map first moves) | ||
(map second moves)) |