Skip to content

Instantly share code, notes, and snippets.

@Chouser
Forked from anonymous/citystate.clj
Last active January 13, 2016 16:04
Show Gist options
  • Save Chouser/c56763c4c2bc3cb14c13 to your computer and use it in GitHub Desktop.
Save Chouser/c56763c4c2bc3cb14c13 to your computer and use it in GitHub Desktop.
(ns n01se.citystate)
(defn vitality
"Vitality of city."
[city])
(defn owner
"Owning city. None is returned if no owner exists."
[city])
(defn freedom
"Amount of freedom granted to city by owner."
[city]
(condp = (owner city)
city 0.0 ;; TODO
None 1.0 ;; No owner means maximum freedom.
(freedom (owner city))))
(defn contacts
"All influencing cities, including self."
[city])
(defn peers
"All influencing cities except the owner."
[city]
(disj (contacts city)
(owner city)))
(defn distance
"Distance between two cities.
If city and source are the same, distance is 1.0."
[city source]
(condp = source
city 1.0
None 1.0
3.0 ;; TODO))
(defn efficiency-floor
"Efficiency of city based on distance to owner."
[city]
(/ 1.0 (distance city (owner city))))
(defn efficiency
"Efficiency percentage of city."
[city]
(+ (efficiency-floor city)
(* (1.0 - (efficiency-floor city))
(freedom city)
)))
(defn weighted-avg
"Returns a number between the numbers a and b.
If factor is 0, returns a; if factor is 1, returns b"
[factor a b]
(+ (* factor (- b a)) a))
(defn efficiency-wa
"Efficiency percentage of city."
[city]
(weighted-avg (freedom (owner city))
(/ 1.0 (distance city (owner city)))
1.0))
(defn efficiency-let
"Efficiency percentage of city."
[city]
(let [low (/ 1.0 (distance city (owner city)))]
(+ low (* (freedom (owner city))
(- 1.0 low)))))
(defn influence
"Influence on city from source."
[city source]
(* (/ (vitality source)
(distance city source))
(if (= source (owner city))
1.0 ;; owners always get full influence
(freedom city))))
(defn total-influence
"Influence on city from all contacts."
[city]
(apply + (map #(influence city %) (contacts city))))
(defn new-owner
"new owner of city. It is possible for the new owner be the same
as the old owner or there be no new owner."
[city]
(some (fn [contact]
(when (> (/ (influence city contact)
(total-influence city))
0.5)
city))
(contacts city)))
(defn new-owner-let
"new owner of city. It is possible for the new owner be the same
as the old owner or there be no new owner."
[city]
(let [owner-inf [(owner city) (influence city (owner city))]
peers-inf (map (fn [peer]
[peer (* (freedom (owner city))
(influence city peer))])
(peers city))
total-inf (apply + (last owner-inf) (map last peers-inf))]
(some (fn [[city inf]]
(when (> (/ inf total-inf) 0.5)
city))
(cons owner-inf
peers-inf))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment