Skip to content

Instantly share code, notes, and snippets.

Last active January 13, 2016 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/90571ba743fef2b175b6 to your computer and use it in GitHub Desktop.
Save anonymous/90571ba743fef2b175b6 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 efficiency-let
"Efficiency percentage of city."
[city]
(let [low (/ 1.0 (distance city (owner city)))]
(+ low (* (freedown (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