-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pattern.cs
89 lines (84 loc) · 2.46 KB
/
Pattern.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CSP
{
public class Pattern
{
public int count= 1;
public String patternId;
public List<int> cuts = new List<int>() ;
public int stockSize;
public int stockLeft;
//Construct
public Pattern(int stockSize)
{
this.patternId = "P-size:"+stockSize;
this.stockSize = stockSize;
this.stockLeft = stockSize;
}
//Overrides
public override bool Equals(object? obj){
return obj is Pattern pattern &&
cuts.SequenceEqual(pattern.cuts);
}
public override int GetHashCode(){
return HashCode.Combine(patternId, cuts, stockLeft);
}
public override string ToString()
{
string planDesc = '{'+patternId+" lorem} ( ";
foreach (var cut in cuts)
{
// planDesc += '{'+cut.id+','+cut.size+'}';
planDesc += cut+" ";
}
planDesc += $") waste:{stockLeft} | X{count}";
return planDesc;
}
//Methods
public void addCut(int cut){
if (cut<=stockLeft)
{
cuts.Add(cut);
stockLeft-=cut;
}
else
{
throw new ArgumentOutOfRangeException();
}
}
public int getWaste(){
return stockLeft * count;
}
//Class Methods (utils)
public static List<Pattern> joinSimilarPatterns(List<Pattern> patterns){
List<Pattern> pats = new List<Pattern>();
foreach (Pattern pat in patterns)
{
if (!pats.Any()){
pats.Add(pat);
}
else{
bool found = false;
foreach (Pattern newpat in pats)
{
if (pat.Equals(newpat))
{
found = true;
newpat.count++;
}
}
if(!found) pats.Add(pat);
}
}
return pats;
}
public static int getTotalWaste(List<Pattern> patterns){
int total = 0;
patterns.ForEach(pat => total += pat.getWaste());
return total;
}
}
}