From 97d7d0141d80e2cab7447e4e8d96eb004abbb206 Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 11 Dec 2020 09:36:28 -0600 Subject: [PATCH] Convert to a logger so I can see results in terminal or IDE --- Program.cs | 11 ++++++++++- Q01.cs | 5 ++--- Q02.cs | 5 ++--- Q03.cs | 5 ++--- Q04.cs | 5 ++--- Q05.cs | 5 ++--- Q06.cs | 8 +++----- Q07.cs | 5 ++--- Q08.cs | 7 +++---- Q09.cs | 13 ++++++------- Q10.cs | 13 ++++++------- Q11.cs | 13 ++++++------- 12 files changed, 46 insertions(+), 49 deletions(-) diff --git a/Program.cs b/Program.cs index 7f47890..c54995c 100644 --- a/Program.cs +++ b/Program.cs @@ -16,7 +16,16 @@ Q09.Go(); Q10.Go(); Q11.Go(); - System.Diagnostics.Debug.WriteLine($"Total time={(System.DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Total time={(System.DateTime.Now - start).TotalMilliseconds}ms"); + } + } + + class Util + { + internal static void Log(string msg) + { + System.Diagnostics.Debug.WriteLine(msg); + System.Console.WriteLine(msg); } } } diff --git a/Q01.cs b/Q01.cs index 1a220c0..7d4ee16 100644 --- a/Q01.cs +++ b/Q01.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Diagnostics; using System.IO; namespace _2020 @@ -39,7 +38,7 @@ namespace _2020 { if (numList[i] + numList[j] == 2020) { - Debug.WriteLine($"Q01Part1: idx {i} + idx {j} = 2020. mult = {numList[i] * numList[j]}"); + Util.Log($"Q01Part1: idx {i} + idx {j} = 2020. mult = {numList[i] * numList[j]}"); return; } } @@ -56,7 +55,7 @@ namespace _2020 { if (numList[i] + numList[j] + numList[k] == 2020) { - Debug.WriteLine($"Q01Part2: idx {i} + idx {j} + idx {k} = 2020. mult = {numList[i] * numList[j] * numList[k]}"); + Util.Log($"Q01Part2: idx {i} + idx {j} + idx {k} = 2020. mult = {numList[i] * numList[j] * numList[k]}"); return; } } diff --git a/Q02.cs b/Q02.cs index d9b9b6d..4ec09bd 100644 --- a/Q02.cs +++ b/Q02.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Text.RegularExpressions; @@ -70,7 +69,7 @@ namespace _2020 } } - Debug.WriteLine($"Q02Part1: numValid={numValid}"); + Util.Log($"Q02Part1: numValid={numValid}"); } static void Part2(List list) @@ -95,7 +94,7 @@ namespace _2020 } } - Debug.WriteLine($"Q02Part2: numValid={numValid}"); + Util.Log($"Q02Part2: numValid={numValid}"); } } } diff --git a/Q03.cs b/Q03.cs index 4f86a22..260b8e7 100644 --- a/Q03.cs +++ b/Q03.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Diagnostics; using System.IO; namespace _2020 @@ -60,7 +59,7 @@ namespace _2020 static void Part1() { - Debug.WriteLine($"Q03Part1: num trees hit = {GetNumTrees(1, 3)}"); + Util.Log($"Q03Part1: num trees hit = {GetNumTrees(1, 3)}"); } static void Part2() @@ -79,7 +78,7 @@ namespace _2020 { multSum *= trees; } - Debug.WriteLine($"Q03Part2: num trees hit, multiplied = {multSum}"); + Util.Log($"Q03Part2: num trees hit, multiplied = {multSum}"); } } } diff --git a/Q04.cs b/Q04.cs index 2c03e80..31d153d 100644 --- a/Q04.cs +++ b/Q04.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Text.RegularExpressions; @@ -66,7 +65,7 @@ namespace _2020 } } - Debug.WriteLine($"Q04Part1: num valid={numValid}"); + Util.Log($"Q04Part1: num valid={numValid}"); } static void Part2() @@ -139,7 +138,7 @@ namespace _2020 } } - Debug.WriteLine($"Q04Part2: num valid={numValid}"); + Util.Log($"Q04Part2: num valid={numValid}"); } } } diff --git a/Q05.cs b/Q05.cs index 9694109..9478986 100644 --- a/Q05.cs +++ b/Q05.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; namespace _2020 @@ -98,7 +97,7 @@ namespace _2020 } } - Debug.WriteLine($"Q05Part1: highest id={highestId}"); + Util.Log($"Q05Part1: highest id={highestId}"); } static void Part2() @@ -126,7 +125,7 @@ namespace _2020 throw new Exception("Found multiple seats"); } - Debug.WriteLine($"Q05Part2: your seat={myId}"); + Util.Log($"Q05Part2: your seat={myId}"); foundMine = true; } diff --git a/Q06.cs b/Q06.cs index a1a85e7..f9825f5 100644 --- a/Q06.cs +++ b/Q06.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; using System.Linq; @@ -63,13 +61,13 @@ namespace _2020 static void Part1() { var sum = list.Sum(x => x.answers.Keys.Count); - Debug.WriteLine($"Q06Part1: sum={sum}"); + Util.Log($"Q06Part1: sum={sum}"); } static void Part2() { var sum = list.Sum(x => x.answers.Sum(y => y.Value == x.numInGroup ? 1 : 0)); - Debug.WriteLine($"Q06Part2: sum={sum}"); + Util.Log($"Q06Part2: sum={sum}"); } } } diff --git a/Q07.cs b/Q07.cs index 03b6bf4..e3b885e 100644 --- a/Q07.cs +++ b/Q07.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -100,7 +99,7 @@ namespace _2020 } var result = totalContain.Distinct(); - Debug.WriteLine($"Q07Part1: total contain={result.Count()}"); + Util.Log($"Q07Part1: total contain={result.Count()}"); } static int GetNumBagsContainedIn(BagType bag) @@ -122,7 +121,7 @@ namespace _2020 int bagsNeeded = GetNumBagsContainedIn(shinyGoldBag); - Debug.WriteLine($"Q07Part2: bags needed={bagsNeeded}"); + Util.Log($"Q07Part2: bags needed={bagsNeeded}"); } } } diff --git a/Q08.cs b/Q08.cs index d150145..743b97c 100644 --- a/Q08.cs +++ b/Q08.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; @@ -82,7 +81,7 @@ namespace _2020 throw new Exception("Exited normally - shouldn't have"); } - Debug.WriteLine($"Q08Part1: accum value={result.Item1}"); + Util.Log($"Q08Part1: accum value={result.Item1}"); } static void Part2() @@ -120,7 +119,7 @@ namespace _2020 throw new Exception("nothing worked"); } - Debug.WriteLine($"changing {instructionToChange} to {instructionToReplace} didn't work - flipping"); + Util.Log($"changing {instructionToChange} to {instructionToReplace} didn't work - flipping"); var tmp = instructionToChange; instructionToChange = instructionToReplace; instructionToReplace = tmp; @@ -144,7 +143,7 @@ namespace _2020 } } - Debug.WriteLine($"Q08Part2: accum value={accum}"); + Util.Log($"Q08Part2: accum value={accum}"); } } } diff --git a/Q09.cs b/Q09.cs index 94afa7c..8c27c81 100644 --- a/Q09.cs +++ b/Q09.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; namespace _2020 @@ -15,15 +14,15 @@ namespace _2020 { var start = DateTime.Now; MakeList(); - Debug.WriteLine($"Q09 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Q09 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); var p1start = DateTime.Now; Part1(); - Debug.WriteLine($"Q09 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); + Util.Log($"Q09 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); var p2start = DateTime.Now; Part2(); - Debug.WriteLine($"Q09 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); + Util.Log($"Q09 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); - Debug.WriteLine($"Q09 took {(DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Q09 took {(DateTime.Now - start).TotalMilliseconds}ms"); } static void MakeList() @@ -73,7 +72,7 @@ namespace _2020 if (idx >= 0) { - Debug.WriteLine($"Q09Part1: found invalid number={list[idx]}"); + Util.Log($"Q09Part1: found invalid number={list[idx]}"); } else { @@ -127,7 +126,7 @@ namespace _2020 } } - Debug.WriteLine($"Q09Part2: encryption weakness={smallestInRange + largestInRange}"); + Util.Log($"Q09Part2: encryption weakness={smallestInRange + largestInRange}"); } } } diff --git a/Q10.cs b/Q10.cs index 66f2729..3eb02f4 100644 --- a/Q10.cs +++ b/Q10.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; namespace _2020 @@ -14,15 +13,15 @@ namespace _2020 { var start = DateTime.Now; MakeList(); - Debug.WriteLine($"Q10 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Q10 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); var p1start = DateTime.Now; Part1(); - Debug.WriteLine($"Q10 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); + Util.Log($"Q10 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); var p2start = DateTime.Now; Part2(); - Debug.WriteLine($"Q10 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); + Util.Log($"Q10 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); - Debug.WriteLine($"Q10 took {(DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Q10 took {(DateTime.Now - start).TotalMilliseconds}ms"); } static void MakeList() @@ -47,7 +46,7 @@ namespace _2020 diffs[sortedList[i] - sortedList[i - 1] - 1]++; } - Debug.WriteLine($"Q10Part1: device joltage={sortedList[^1]}, diffs by 1={diffs[0]}, diffs by 3={diffs[2]}, multiplied={diffs[0] * diffs[2]}"); + Util.Log($"Q10Part1: device joltage={sortedList[^1]}, diffs by 1={diffs[0]}, diffs by 3={diffs[2]}, multiplied={diffs[0] * diffs[2]}"); } static void Part2() @@ -72,7 +71,7 @@ namespace _2020 pathsToIndices.Add(pathLen); } - Debug.WriteLine($"Q10Part2: combinations={pathsToIndices[^1]}"); + Util.Log($"Q10Part2: combinations={pathsToIndices[^1]}"); } } } diff --git a/Q11.cs b/Q11.cs index 8e93e3e..545c849 100644 --- a/Q11.cs +++ b/Q11.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -23,15 +22,15 @@ namespace _2020 { var start = DateTime.Now; MakeList(); - Debug.WriteLine($"Q11 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Q11 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); var p1start = DateTime.Now; Part1(); - Debug.WriteLine($"Q11 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); + Util.Log($"Q11 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); var p2start = DateTime.Now; Part2(); - Debug.WriteLine($"Q11 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); + Util.Log($"Q11 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); - Debug.WriteLine($"Q11 took {(DateTime.Now - start).TotalMilliseconds}ms"); + Util.Log($"Q11 took {(DateTime.Now - start).TotalMilliseconds}ms"); } static void MakeList() @@ -241,7 +240,7 @@ namespace _2020 Array.Copy(nextState, currState, numRows * numCols); } - Debug.WriteLine($"Q11Part1: stabilized after {rounds - 1} rounds, total occupied={GetNumOccupied(currState)}"); + Util.Log($"Q11Part1: stabilized after {rounds - 1} rounds, total occupied={GetNumOccupied(currState)}"); } static void Part2() @@ -278,7 +277,7 @@ namespace _2020 Array.Copy(nextState, currState, numRows * numCols); } - Debug.WriteLine($"Q11Part2: stabilized after {rounds - 1} rounds, total occupied={GetNumOccupied(currState)}"); + Util.Log($"Q11Part2: stabilized after {rounds - 1} rounds, total occupied={GetNumOccupied(currState)}"); } } }