public void RecordHistory(string text, bool isInPrivate) { if (!isInPrivate) { var record = new Spoken { UtcNowTicks = DateTime.UtcNow.Ticks, TickCount = Environment.TickCount, IsInPrivate = isInPrivate, Text = text }; XmlFragmentHelper.WriteLog(HistoryFilePath, record); } }
private void UpdateSpoken() { if (Speaking != null) { Spoken.Add(Speaking); } Speaking = null; lblCurrentSpeaker.Text = Translations.NobodySpeaking; SetLabelWithTooltip(lblSpoken, Translations.PeopleThatHaveSpoken, Spoken); }
private static int Solution(string[] input, int noOfIterations) { string[] inputs = input[0].Split(","); int newLastNo; Dictionary <int, Spoken> gamePlay = new(); Spoken lastGo = new(0, 0, false); for (int turnNo = 1; turnNo <= inputs.Length; turnNo++) { lastGo = new Spoken(int.Parse(inputs[turnNo - 1]), turnNo, true); gamePlay.Add(lastGo.No, lastGo); } for (int turnNo = inputs.Length + 1; turnNo <= noOfIterations; turnNo++) { if (lastGo.FirstTime) { newLastNo = 0; gamePlay[lastGo.No] = lastGo with { FirstTime = false }; } else { newLastNo = turnNo - 1 - gamePlay[lastGo.No].TurnNo; gamePlay[lastGo.No] = lastGo with { TurnNo = turnNo - 1 }; } lastGo = new(newLastNo, turnNo, !gamePlay.ContainsKey(newLastNo)); } return(lastGo.No); } }
static void ProcessText(TextReader reader) { ReadToFirstChapterHeader(reader); var endOfText = false; var paragraph = new StringBuilder(); var blankCount = 0; for (var line = reader.ReadLine(); !endOfText && line != null; line = reader.ReadLine()) { if (string.IsNullOrWhiteSpace(line)) { if (paragraph.Length != 0) { var text = paragraph.ToString(); for (var startReference = text.IndexOf('*'); 0 <= startReference; startReference = text.IndexOf('*')) { var endReference = startReference; do { endReference++; }while (endReference < text.Length && text[endReference - 1] != ' '); Console.WriteLine(text.Substring(startReference, endReference - startReference)); Console.WriteLine(" " + text.Substring(startReference)); text = text.Substring(0, startReference) + text.Substring(endReference); } var record = new Spoken { UtcNowTicks = DateTime.UtcNow.Ticks, TickCount = Environment.TickCount, Text = text }; records.Add(record); paragraph.Clear(); } blankCount++; } else { switch (blankCount) { case 0: if (paragraph.Length != 0) { paragraph.Append(' '); } paragraph.Append(line); break; case 1: if (line.StartsWith("*")) { Debug.Assert(true); } else if (line.StartsWith("BOOK")) { Debug.Assert(true); } else { Debug.Assert(paragraph.Length == 0); paragraph.Append(line); } break; case 2: if (line.StartsWith("*")) { Debug.Assert(true); } else { Debug.Assert(paragraph.Length == 0); paragraph.Append(line); } break; case 4: Debug.Assert(line.StartsWith("CHAPTER")); break; case 5: endOfText = true; break; default: Debug.Assert(false); break; } blankCount = 0; } } }
public static void Day15() { Console.WriteLine("Day15: "); var input = File.ReadAllText("Aoc2020\\Day15\\test.txt").Split(',').Select(int.Parse).ToArray(); // part 1 int lastNum = PlaySpokenGame2(2020); Console.WriteLine(lastNum); // part 2 Stopwatch sw = Stopwatch.StartNew(); lastNum = PlaySpokenGame2(30000000); sw.Stop(); Console.WriteLine($"{lastNum} in {sw.ElapsedMilliseconds}ms"); int PlaySpokenGame(int rounds) { Spoken[] lastSpoken = new Spoken[rounds]; int i, lastNum = -1; for (i = 0; i < input.Length; i++) { lastNum = input[i]; lastSpoken[lastNum].time = i + 1; } for (; i < rounds; i++) { if (lastSpoken[lastNum].time == 0) { lastNum = input[i % input.Length]; } else { if (lastSpoken[lastNum].prev_time == 0) { lastNum = 0; } else { lastNum = lastSpoken[lastNum].time - lastSpoken[lastNum].prev_time; } } lastSpoken[lastNum].prev_time = lastSpoken[lastNum].time; lastSpoken[lastNum].time = i + 1; } return(lastNum); } int PlaySpokenGame2(int rounds) { int[] lastSpoken = new int[rounds]; int i, prev = -1, next = -1; for (i = 0; i < input.Length; i++) { prev = input[i]; lastSpoken[prev] = i + 1; } next = 0; for (; i < rounds; i++) { if (lastSpoken[prev] == 0) { next = 0; } else { next = i + 1 - lastSpoken[prev]; lastSpoken[prev] = i + 1; } } return(next); } }