-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
95 lines (83 loc) · 2.52 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(ns tondeuse.core
(:gen-class))
(def regex-map #"(\d+) (\d+)")
(def regex-tondeuse #"(\d+) (\d+) (\w)")
(def Orientation [{:dir "N" :d [0 1]}
{:dir "E" :d [1 0]}
{:dir "S" :d [0 -1]}
{:dir "W" :d [-1 0]}])
(def x 0)
(def y 0)
(defn outMap?
[tondeuse-info]
(let [xDest (first tondeuse-info)
yDest (second tondeuse-info)]
(or (or (> xDest x) (> yDest y))
(or (neg? xDest) (neg? yDest)))))
(defn advance
"avance la tondeuse d'une case si possible"
[tondeuse-info]
(let [future-tondeuse
(map + tondeuse-info
(:d (first (filter #(= (:dir %) (last tondeuse-info)) Orientation))))]
(if (outMap? future-tondeuse)
tondeuse-info
(conj (vec future-tondeuse) (last tondeuse-info)))))
(defn rotate
"change orientation"
[tondeuse-info spin]
(let [inf-or (flatten (repeat 2 Orientation))
curOrient (last tondeuse-info)]
(->> (drop-while
#(not= curOrient (:dir %))
(if (= spin :clockwise)
inf-or
(reverse inf-or)))
(second)
(:dir)
(conj (vec (butlast tondeuse-info))))))
(defn move
"si donne vector avec xyo et nouvel ordre alors replace la tondeuse"
[tondeuse-info ordre]
(if (= ordre "A")
(advance tondeuse-info)
(if (= ordre "D")
(rotate tondeuse-info :clockwise)
(rotate tondeuse-info :counter))))
(defn str-tondeuse-into-hash
"parse a string into an hash"
[s]
(print "TODO"))
(defn str-tondeuse-info-to-vector
"parse a string into a vec"
[s]
(let [parsed-s (re-find regex-tondeuse s)
[_ xt yt ot] parsed-s]
(conj [] (Integer/parseInt xt) (Integer/parseInt yt) ot)))
(defn parse-tondeuse
[lines]
(let [tondeuse-info (str-tondeuse-info-to-vector (first lines))
orders (second lines)]
(reduce #(move %1 (str %2)) tondeuse-info orders)))
(defn consume-tondeuse
[lines]
(do
(println (parse-tondeuse (take 2 lines)))
(if (not (empty? (nnext lines)))
(consume-tondeuse (nnext lines))
"Done")))
(defn size-map!
"init la carte et renvoie le reste des args non utilisé"
[lines]
(let [parsedSize (re-find regex-map (first lines))
[width length] (rest parsedSize)]
(def x (Integer/parseInt width))
(def y (Integer/parseInt length))
(rest lines)))
(defn -main
"mange le fichier d'input et calcul le mouvement des tondeuses"
[path]
(-> (slurp path)
(clojure.string/split-lines)
(size-map!)
(consume-tondeuse)))