Skip to content

Instantly share code, notes, and snippets.

@sarcilav
Created April 24, 2017 16:06
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 sarcilav/d64ece089af6450663daf22bef66a7a2 to your computer and use it in GitHub Desktop.
Save sarcilav/d64ece089af6450663daf22bef66a7a2 to your computer and use it in GitHub Desktop.
(ns Player
(:gen-class))
; Auto-generated code below aims at helping you parse
; the standard input according to the problem statement.
(def tick (atom 0))
(def distance-stress 11)
(defn log [& args]
(binding [*out* *err*]
(apply prn args)))
(defn distance [{x1 :x y1 :y} {x2 :x y2 :y :as target}]
[(Math/sqrt (+ (* (- x2 x1) (- x2 x1))
(* (- y2 y1) (- y2 y1)))) target])
(defn close-one [pivot targets]
(reduce (fn [[distance1 target1] [distance2 target2]]
(if (< distance1 distance2)
[distance1 target1]
[distance2 target2]))
[9999 {}] (map (partial distance pivot) targets)))
(defn entity-filter [entity]
(fn [{type :type}] (= type entity)))
(defn my-ships-filter []
(fn [{myship? :myship?}] (= myship? 1)))
(defn barrels-filter []
(entity-filter 'BARREL))
(defn enemy-filter []
(fn [{myship? :myship? type :type}] (and (= type 'SHIP) (= myship? 0))))
(defn mines-filter []
(entity-filter 'MINE))
(defn filter-entities [entities preds]
(map (fn [pred] (filter (pred) entities)) preds))
(defn move [target]
(println "MOVE" (:x target) (:y target)))
(defn next-place-keeping-momentum [pivot target]
(let [[dx dy] (if (= 0 (rem (:y target) 2))
;; even
(case (:rotation target)
0 [1 0]
1 [0 -1]
2 [-1 -1]
3 [-1 0]
4 [-1 1]
5 [0 1])
;; odd
(case (:rotation target)
0 [1 0]
1 [1 -1]
2 [0 -1]
3 [-1 0]
4 [0 1]
5 [1 1]))
speed (:speed target)
[dist _] (distance pivot target)
turns (+ 1 (int (/ dist 3)))]
(assoc target
:x (+ (:x target) (* speed dx turns))
:y (+ (:y target) (* speed dy turns)))))
(defn fire [pivot target]
(let [relocate-target (next-place-keeping-momentum pivot target)]
(println "FIRE" (:x relocate-target) (:y relocate-target))))
(defn -main [& args]
(while true
(let [myShipCount (read) entityCount (read)
[my-ships enemies barrels mines] (filter-entities (repeatedly entityCount (fn [] {:entityId (read) :type (read)
:x (read) :y (read)
:rotation (read) :speed (read)
:rum (read) :myship? (read)}))
[my-ships-filter enemy-filter barrels-filter mines-filter])]
(doseq [ship my-ships]
(if (= (rem @tick 4) 0)
(do
;;attack mine/fire
(let [[dist target] (close-one ship enemies)]
(if (< dist distance-stress)
(do
(log "dist" dist)
(log "target" target)
(fire ship target))
(if (not (empty? mines))
(let [[dist-mine mine] (close-one ship mines)]
(if (< dist-mine (/ distance-stress 2))
(fire ship mine)
(println "FASTER")))
(println "MINE")))))
;; move
(move (if (empty? barrels)
(let [[distance target] (close-one ship enemies)]
target)
(let [[distance target] (close-one ship barrels)]
target)))))
(swap! tick inc))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment