forked from manlycode/doll-smuggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doll_smuggler.clj
106 lines (92 loc) · 3.19 KB
/
doll_smuggler.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
95
96
97
98
99
100
101
102
103
104
105
106
;(use 'clojure.contrib.trace)
;;(def doll {:weight "4" :value "150"})
(defstruct doll :weight :value :name)
(def shipment[
(struct doll 9 150 "map")
(struct doll 13 35 "compass")
(struct doll 153 200 "water")
(struct doll 50 160 "sandwich")
(struct doll 15 60 "glucose")
(struct doll 68 45 "tin")
(struct doll 27 60 "banana")
(struct doll 39 40 "apple")
(struct doll 23 30 "cheese")
(struct doll 52 10 "beer")
(struct doll 11 70 "suntan")
(struct doll 32 30 "camera")
(struct doll 24 15 "t-shirt")
(struct doll 48 10 "trousers")
(struct doll 73 40 "umbrella")
(struct doll 42 70 "waterproof trowsers")
(struct doll 43 75 "waterproof overclothes")
(struct doll 22 80 "note-case")
(struct doll 7 20 "sunglasses")
(struct doll 18 12 "towel")
(struct doll 4 50 "socks")
(struct doll 30 10 "boots")
])
(declare memoize-calculate-optimal)
(defn calculate-optimal [index mWeight]
;(println "calling calculate optimal for" index mWeight)
;if invalid object index, or weight is zero
(if (or (< index 0) (= mWeight 0))
[0[]]
;;get the doll at this index, get references to its weight and values
(let [{itemWeight :weight itemValue :value} (get shipment index)]
;(println itemWeight " and " itemValue)
;if the doll won't fit
(if (> itemWeight mWeight)
;skip to the next doll
(memoize-calculate-optimal (dec index) mWeight)
; else try see if this doll is optimal - first bind variables to some results using let
(let [[valueNotPacking setNotPacking] (memoize-calculate-optimal (dec index) mWeight)
[valuePacking setPacking] (memoize-calculate-optimal (dec index) (- mWeight itemWeight) ) ]
;see what is optimal - packing the doll or not packing the doll
(if (> (+ valuePacking itemValue) valueNotPacking )
;its a keeper - add value to the total, add item to the list
[(+ valuePacking itemValue) (conj setPacking index)]
;its not a keeper - use value and list from not packing
[ valueNotPacking setNotPacking ]
)
)
)
)
)
)
(def memoize-calculate-optimal(memoize calculate-optimal))
(defn enter-dolls[dolls]
(println "Enter the weight of the doll")(flush)
(println "do you want to enter another doll? (y or n)")(flush)
(if (= (read-line) "y")
(conj dolls (enter-dolls[]))
[]
)
)
(defn use-known-data[]
(println "using weight" (def max-weight 400))
(println "using shipment data" shipment)
(println "")
(println "################ RESULTS #################")
(println "")
(let[[totalValue packedItems](calculate-optimal (dec (count shipment)) max-weight) ]
(println "packed " (count packedItems) " items including indexes " packedItems " with total value " totalValue)
(doseq [doll packedItems] (println (shipment doll)))
)
)
(defn use-custom-data []
(println "######### Custom Data Mode ############")
(println "")
(println "Enter the carrying capacity (in grams) of today's old lady")(flush)
(def max-weight (read-line))
(println "----Max weight set to "max-weight "grams---" )
(def todays-shipment (enter-dolls {}))
)
(defn setup []
;(println "do you want to enter your own dataset? (y or n)")
;(def custom-data (read-line))(flush)
;(if(= custom-data "y")
; (use-custom-data )
(use-known-data )
;)
)
(setup)