-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsolution.js
131 lines (89 loc) · 3.13 KB
/
solution.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict'
const MIN_OFFER_VALUE = 4
module.exports = class Agent {
constructor (me, counts, values, max_rounds, log) {
this.values = values
this.myOffers = this.getMyOffers(counts, max_rounds, me)
this.nextOfferIndex = 0
}
offer (offer) {
const myOffer = this.myOffer()
if (this.offerValue(offer) >= this.offerValue(myOffer)) {
return
}
this.patchMyOffers(offer)
return myOffer
}
myOffer () {
const offer = this.myOffers[this.nextOfferIndex++]
return offer ? offer : this.myOffers[this.myOffers.length - 1]
}
patchMyOffers (offer) {
const offerValue = this.offerValue(offer)
if (offerValue < MIN_OFFER_VALUE) {
return
}
const offerToReplaceIndex = this.myOffers
.findIndex(myOffer => offerValue >= this.offerValue(myOffer))
this.myOffers[offerToReplaceIndex] = offer
}
offerValue (offer = []) {
return offer.reduce((value, itemsCount, item) => {
const itemsValue = itemsCount * this.values[item]
return value + itemsValue
}, 0)
}
getMyOffers (counts, rounds, me) {
if (me) { rounds-- }
const offers = this.getAcceptableOffersByDesc(counts)
const maxValue = this.offerValue(offers[0])
const minValue = this.offerValue(offers[offers.length - 1])
const range = maxValue - minValue
const step = range / ( rounds - 1 )
const myOffers = []
let lastStoredOfferIndex = -1
for (let v = maxValue, i = 0; i < rounds; v -= step, i++) {
let closestItemIndex = lastStoredOfferIndex + 1,
minDistance = Infinity
for (let j = lastStoredOfferIndex + 1; j < offers.length; j++) {
const currentDistance = Math.abs(this.offerValue(offers[j]) - v)
if (currentDistance < minDistance) {
minDistance = currentDistance
closestItemIndex = j
}
}
if (offers[closestItemIndex]) {
myOffers.push(offers[closestItemIndex])
lastStoredOfferIndex = closestItemIndex
}
}
return myOffers
}
getAcceptableOffersByDesc (counts) {
const offers = allPossibleOffers(counts)
return offers
.filter(offer => this.offerValue(offer) >= MIN_OFFER_VALUE)
.filter(offer => offer.every((n, index) => !n || this.values[index] !== 0))
.sort((o1, o2) => this.offerValue(o2) - this.offerValue(o1))
}
}
function allPossibleOffers (counts) {
let offers = []
for (const count of counts) {
offers = combine(offers, count)
}
return offers
}
function combine (prefixCombinations = [], count) {
const combinations = []
for (let c = 0; c <= count; c++) {
if (prefixCombinations.length) {
for (const comb of prefixCombinations) {
combinations.push(comb.concat(c))
}
} else {
combinations.push([c])
}
}
return combinations
}