-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2.js
127 lines (121 loc) · 4.62 KB
/
task2.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
console.clear()
const axios = require('axios')
const data = {
token: "30d09cb3-a614-4bd9-99aa-0071fad81680",
map_url: "https://datsanta.dats.team/json/map/a8e01288-28f8-45ee-9db4-f74fc4ff02c8.json",
mapID: "a8e01288-28f8-45ee-9db4-f74fc4ff02c8",
route_url: "https://datsanta.dats.team/api/round2"
}
const info = require('./a8e01288-28f8-45ee-9db4-f74fc4ff02c8.json')
const shuffle = (array) => {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
const getTotalPrice = (children) => {
let total = 0
for (let i = 0; i < children.length; i++) {
total+=children[i].gift.price
}
return total
}
const bubbleSortByPrice = (array ) => {
for (let i = 0, endI = array.length - 1; i < endI; i++) {
for (let j = 0, endJ = endI - i; j < endJ; j++) {
if (array[j].price > array[j + 1].price) {
let swaparray = array[j]
array[j] = array[j + 1]
array[j + 1] = swaparray
}
}
}
return array;
}
const main = () => {
console.time('main')
// gender 0 is female and gender 1 is male but -1 is unimegasex
const gift_type = [
{type: "constructors", gender: 1, age_min: 3, age_max: -1},
{type: "dolls", gender: 0, age_min: 0, age_max: -1},
{type: "radio_controlled_toys", gender: 1, age_min: 3, age_max: -1},
{type: "toy_vehicles", gender: 1, age_min: 1, age_max: -1},
{type: "board_games", gender: -1, age_min: 5, age_max: -1},
{type: "outdoor_games", gender: -1, age_min: 1, age_max: -1},
{type: "playground", gender: -1, age_min: 2, age_max: -1},
{type: "soft_toys", gender: -1, age_min: 0, age_max: -1},
{type: "computer_games", gender: 1, age_min: 6, age_max: -1},
{type: "sweets", gender: -1, age_min: 0, age_max: -1},
{type: "books", gender: -1, age_min: 3, age_max: -1},
{type: "pet", gender: -1, age_min: 0, age_max: -1},
{type: "clothes", gender: 0, age_min: 0, age_max: -1},
]
for (let i = 0; i < info.gifts.length; i++) {
for (let j = 0; j < gift_type.length; j++) {
if(gift_type[j].type == info.gifts[i].type){
info.gifts[i].gender = gift_type[j].gender
info.gifts[i].age_min = gift_type[j].age_min
info.gifts[i].age_max = gift_type[j].age_max == -1 ? 999999999 : gift_type[j].age_max
}
}
}
let currentGifts = bubbleSortByPrice(info.gifts).reverse()
currentGifts = shuffle(info.gifts)
for (let i = 0; i < info.children.length; i++) {
info.children[i].gender = info.children[i].gender == "female" ? 0 : 1
info.children[i].gift = currentGifts.filter(gift => (gift.gender == info.children[i].gender || gift.gender == -1) && (info.children[i].age >= gift.age_min && info.children[i].age <= gift.age_max))[0]
currentGifts = currentGifts.filter(gift => gift.id != info.children[i].gift.id)
}
console.log(getTotalPrice(info.children))
while(getTotalPrice(info.children) > 100000){
currentGifts = bubbleSortByPrice(info.gifts).reverse()
currentGifts = shuffle(info.gifts)
for (let i = 0; i < info.children.length; i++) {
info.children[i].gender = info.children[i].gender == "female" ? 0 : 1
info.children[i].gift = currentGifts.filter(gift => (gift.gender == info.children[i].gender || gift.gender == -1) && (info.children[i].age >= gift.age_min && info.children[i].age <= gift.age_max))[0]
currentGifts = currentGifts.filter(gift => gift.id != info.children[i].gift.id)
}
console.log(getTotalPrice(info.children))
}
const presentingGifts = []
for (let i = 0; i < info.children.length; i++) {
presentingGifts.push({
giftID: info.children[i].gift.id,
childID: info.children[i].id
})
}
console.log(presentingGifts[0])
console.log(info.children.length,info.gifts.length,presentingGifts.length)
axios({
method: "post",
url: String(data.route_url),
headers: {
"Accept": "application/json",
"X-API-Key": data.token,
"Content-Type": "application/json"
},
data: JSON.stringify({
"mapID": data.mapID,
"presentingGifts": presentingGifts
})
}).then(res=>{
console.log(res.data)
}).catch(err=>{
console.log(err)
})
const fs = require("fs")
const path = require("path")
fs.appendFile(`${new Date().getTime()}.json`, JSON.stringify(presentingGifts), (err2) => {
if (err2) console.log(err2)
console.log("saved")
})
console.timeEnd('main')
}
main()