-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
80 lines (62 loc) · 2.54 KB
/
main.ts
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
import { Decol, ListLayout, optimize } from "./src/Optimization.js"
export type InputData = {
useMinimalOrder : boolean,
optimizeBySquare : boolean,
decoli : { id : string, square : string }[],
layouts : {
id : string,
minOrder : number,
decoli : { decolId : string, count : number }[],
}[],
required : { decolId : string, count : number }[]
}
export type Result = {
error : string,
result : { layoutId : string, count : number }[]
}
export const optimizeDecolsOrder = function (input : string) : string {
const inputData : InputData = JSON.parse(input)
const decoli : Map<string, Decol> = new Map(inputData.decoli.map(decolData => {
return [
decolData.id,
Decol.new({ square : Number(String(decolData.square).replace(/,/g, '.') || 0) })
]
}))
const layouts : Map<string, ListLayout> = new Map(inputData.layouts.map(layoutData => {
const layoutDecolsData : Map<Decol, number> = new Map(layoutData.decoli.map(layoutDecolData => {
return [ decoli.get(layoutDecolData.decolId), layoutDecolData.count ]
}))
return [
layoutData.id,
ListLayout.new({
name : layoutData.id,
minOrderCount : layoutData.minOrder,
layout : layoutDecolsData
})
]
}))
const requiredDecols : Map<Decol, number> = new Map(inputData.required.map(requiredData => {
return [ decoli.get(requiredData.decolId), requiredData.count ]
}))
const result = optimize(decoli, layouts, requiredDecols, inputData.useMinimalOrder, inputData.optimizeBySquare)
const outerResult : Result = {
error : result.error,
result : Array.from(result.result.entries()).map(([ variable, count ]) => {
return {
layoutId : variable.name,
count : Math.ceil(count)
}
})
}
return JSON.stringify(outerResult)
}
export const sanityChecker = (a, b) => a + b
const inputFile = process.argv[ 2 ]
const outputFile = process.argv[ 3 ]
console.log("Starting optimization, input data: " + inputFile)
const fs = require('fs')
// empty the output in case something will go wrong, so that it won't show stale results
fs.writeFileSync(outputFile, '')
const input = fs.readFileSync(inputFile)
fs.writeFileSync(outputFile, optimizeDecolsOrder(input))
console.log("Optimization completed, output data: " + inputFile)