-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
144 lines (128 loc) · 5.48 KB
/
Program.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BoardCutter
{
class Program
{
public const double kerf = 0.125;
static void Main(string[] args)
{
var cuts = new List<Cut> {
new Cut { Number = 13, Length = 27.5 },
new Cut { Number = 6, Length = 129 },
new Cut { Number = 3, Length = 105 },
new Cut { Number = 8, Length = 15 },
new Cut { Number = 2, Length = 6*12 },
new Cut { Number = 2, Length = 42 },
new Cut { Number = 7, Length = 42 },
new Cut { Number = 3, Length = 38.5 },
new Cut { Number = 3, Length =32.3 }
};
//Current price list for 2x4's at local menards on 11/14/12
var stockList = new List<Board> {
new Board { Length = 20*12, Price = 7.43 },
new Board { Length = 18*12, Price = 6.65 },
new Board { Length = 16*12, Price = 5.44 },
new Board { Length = 14*12, Price = 4.89 },
new Board { Length = 12*12, Price = 4.11 },
new Board { Length = 10*12, Price = 2.99 },
new Board { Length = 116.625, Price = 3.49 },
new Board { Length = 104.625, Price = 2.93 },
new Board { Length = 92.625, Price = 2.39 },
new Board { Length = 7*12, Price = 2.05 },
};
//Calculate based on always picking the smallest stock stock big enough for the cut when new stock is needed
SmallestStock(ExpandCuts(cuts).OrderByDescending(x => x).ToList(), stockList);
Console.WriteLine("");
//Calculate based on always picking the largest stock when a new stock is needed
LargestStock(ExpandCuts(cuts).OrderByDescending(x => x).ToList(), stockList);
Console.Read();
}
private static void SmallestStock(IList<double> cuts, List<Board> stockList)
{
var stockUsed = new List<Board>();
var scraps = new List<double>();
while (cuts.Any())
{
//switch to from list to queue
var longestCut = cuts.OrderByDescending(x => x).First();
cuts.Remove(longestCut);
if (scraps.Any(x => x > longestCut))
{
scraps = scraps.CutFromScrap(longestCut);
}
else
{
var newStock = stockList.Where(s => s.Length > longestCut).OrderBy(s => s.PricePerUnit).First();
//Remove Saw Width
stockUsed.Add(newStock);
scraps.Add(newStock.Length - longestCut);
}
}
Console.WriteLine("Number of boards used: {0}", stockUsed.Count());
Console.WriteLine("Total Cost: {0}", stockUsed.Select(s => s.Price).Sum());
OutputPurchaseList(stockUsed);
OutputWaste(scraps, stockUsed.Select(s => s.Length).Sum());
}
private static void LargestStock(IList<double> cuts, IList<Board> stockList)
{
var longest = stockList.OrderByDescending(x => x.Length).First();
var longestBoard = longest.Length;
var boardCost = longest.Price;
var scraps = new List<double>();
var stockUsed = new List<Board>();
while (cuts.Any())
{
var longestCut = cuts.OrderByDescending(x => x).First();
cuts.Remove(longestCut);
if (scraps.Any(x => x > longestCut))
{
scraps = scraps.CutFromScrap(longestCut);
}
else
{
stockUsed.Add(longest);
scraps.Add(longestBoard - longestCut - kerf);
}
}
Console.WriteLine("Total number of boards used: {0}", stockUsed.Count());
Console.WriteLine("Total Cost: {0}", stockUsed.Count() * boardCost);
OutputWaste(scraps, stockUsed.Count() * longestBoard);
}
private static void OutputPurchaseList(List<Board> stockUsed)
{
//Output Purchase List
var purchaseList = stockUsed.GroupBy(s => s.Length).Select(s => String.Format("{0} | {1}", s.Key / 12, s.Count()));
Console.WriteLine("Purchase List: ", purchaseList);
purchaseList.ToList().ForEach(i => Console.WriteLine("\t {0}", i));
}
private static void OutputWaste(List<double> scraps, double totalStock)
{
double waste = 0;
Console.Write("Remainders: ");
foreach (var s in scraps)
{
waste += s;
Console.Write("{0}, ", s);
}
Console.Write("\n");
Console.WriteLine("Wasted Length: {0}", waste/12);
Console.WriteLine("Utilization Percent: {0}", (1 - (waste / totalStock)) * 100);
}
private static IEnumerable<double> ExpandCuts(IList<Cut> cuts)
{
foreach (var cut in cuts)
{
for (int i = 0; i < cut.Number; i++)
{
yield return cut.Length;
}
}
}
}
}