using System; using System.Collections.Generic; using System.IO; using System.Text; using static System.Console; namespace _2018_04 { class Program { static void Main(string[] args) { string[] splitter = new string[] { "[", "]" }; string[] resTab = File.ReadAllLines(@"D:\AdventOfCode\data\2018_04_data.txt"); Array.Sort(resTab); Dictionary gdList = new Dictionary(); GardDay gd = null; //[1518-11-01 00:00] Guard #10 begins shift //[1518-11-01 00:05] falls asleep //[1518-11-01 00:25] wakes up //[1518-11-01 00:30] falls asleep //[1518-11-01 00:55] wakes up foreach (var str in resTab) { if (str.Contains("#")) { if (gd != null) { WriteLine(gd.ToString()); if (gdList.ContainsKey(gd.GardNr)) { for (int vekt = 0; vekt < gdList[gd.GardNr].MinTabell.Length; vekt++) { gdList[gd.GardNr].MinTabell[vekt] += gd.MinTabell[vekt]; } gdList[gd.GardNr].SleepMinutes += gd.SleepMinutes; } else { gd.fillMinTable(); gdList.Add(gd.GardNr, gd); } } gd = new GardDay(str); // WriteLine($"Gard day = {gd.ToString()}"); } else { gd.fillNext(str); } } foreach( int key in gdList.Keys) { } //gdList.Add(gd.GardNr, gd); } } public class GardDay { string[] splitter = new string[] { "[", "]" }; public int GardNr { get; set; } public Dictionary TimeStamp { get; set; } public int[] MinTabell { get; set; } public int SleepMinutes { get; set; } public void fillMinTable() { int saveMin = -1; foreach (KeyValuePair pair in TimeStamp) { if (pair.Value == "sleep") { saveMin = pair.Key.Minute; } else { if (saveMin > -1) { for (int i = saveMin; i < pair.Key.Minute; i++) { MinTabell[i]++; SleepMinutes++; } } } } } public GardDay(string inRow) { MinTabell = new int[60]; SleepMinutes = 0; string outStr = ""; fillNext(inRow, out outStr); var xwords = outStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var ystr in xwords) { if (ystr.Contains("#")) { GardNr = int.Parse(ystr.Substring(ystr.IndexOf("#") + 1)); } } } public void fillNext(string inRow) { string tmpOut = null; fillNext(inRow, out tmpOut); } public void fillNext(string inRow, out string outStr) { string[] xrow = inRow.Split(splitter, StringSplitOptions.RemoveEmptyEntries); outStr = xrow[1]; DateTime ts = DateTime.Parse(xrow[0]); if (TimeStamp == null) { TimeStamp = new Dictionary(); } string tmpStep = "sleep"; if (outStr.Contains("Guard") || outStr.Contains("wakes")) { tmpStep = "awake"; } TimeStamp.Add(ts, tmpStep); } public override string ToString() { StringBuilder outTxt = new StringBuilder(); outTxt.Append($"gardnr {GardNr}"); foreach (var key in TimeStamp.Keys) { outTxt.Append($" Time : {key}, {TimeStamp[key]}"); } return outTxt.ToString(); } } }