static void ReadSupplyOrInventory(string filename, ref SortedDictionary <DateTime, ProductCounter> list) { list.Clear(); StreamReader reader = new StreamReader(filename); reader.ReadLine(); while (!reader.EndOfStream) { var str = reader.ReadLine(); var spl = str.Split(','); if (spl.Length == 3) { DateTime date = DateTime.Parse(spl[0]); list[date] = new ProductCounter(Int32.Parse(spl[1]), Int32.Parse(spl[2])); } } reader.Close(); }
static void Parse(string name, ref Dictionary <DateTime, ProductCounter> state, ref Dictionary <DateTime, ProductCounter> stolen, ref Dictionary <DateTime, ProductCounter> sell) { var supply = new SortedDictionary <DateTime, ProductCounter>(); var inventory = new SortedDictionary <DateTime, ProductCounter>(); ReadSupplyOrInventory(string.Format("input\\{0}-supply.csv", name), ref supply); ReadSupplyOrInventory(string.Format("input\\{0}-inventory.csv", name), ref inventory); StreamReader reader = new StreamReader(string.Format("input\\{0}-sell.csv", name)); reader.ReadLine(); sell.Clear(); while (!reader.EndOfStream) { var str = reader.ReadLine(); var spl = str.Split(','); if (spl.Length == 2) { var date = DateTime.Parse(spl[0]); var spl1 = spl[1].Split('-'); if (spl1.Length == 8) { var sprod = spl1[2]; ProductCounter toadd = new ProductCounter(); if (sprod == "ap") { toadd.appleCount = 1; } else if (sprod == "pe") { toadd.penCount = 1; } if (sell.ContainsKey(date)) { var prodCount = sell[date]; prodCount.AddCount(toadd); sell[date] = prodCount; } else { sell[date] = toadd; } } } } reader.Close(); var firstInvDate = inventory.First().Key; inventory[firstInvDate.Subtract(new TimeSpan(DateTime.DaysInMonth(firstInvDate.Year, firstInvDate.Month), 0, 0, 0))] = new ProductCounter(); state.Clear(); stolen.Clear(); bool islast = false; foreach (var pair in inventory) { if (islast) { break; } ProductCounter counter = pair.Value; for (int i = 1; i <= 31; i++) { DateTime curDate = pair.Key.Add(new TimeSpan(i, 0, 0, 0)); if (sell.ContainsKey(curDate)) { counter.SubstractCount(sell[curDate]); } if (supply.ContainsKey(curDate)) { counter.AddCount(supply[curDate]); } state[curDate] = counter; DateTime nextDate = pair.Key.Add(new TimeSpan(i + 1, 0, 0, 0)); if (nextDate.Month != curDate.Month) { if (inventory.Last().Key == curDate) { islast = true; } if (inventory.ContainsKey(curDate)) { state[curDate] = inventory[curDate]; var data = inventory[curDate]; counter.SubstractCount(data); stolen[curDate] = counter; } break; } } } }
public void SubstractCount(ProductCounter prod) { this.appleCount -= prod.appleCount; this.penCount -= prod.penCount; }
public void AddCount(ProductCounter prod) { this.appleCount += prod.appleCount; this.penCount += prod.penCount; }