From 0a3fe365d02c25fec748be9bb1847cc16120361a Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 22 Dec 2020 13:04:55 -0600 Subject: [PATCH] Make a ton of VS-recommended changes. Sure, why not. --- Program.cs | 2 +- Q03.cs | 2 +- Q04.cs | 10 +++++----- Q06.cs | 8 ++++---- Q07.cs | 8 ++++---- Q08.cs | 4 ++-- Q09.cs | 2 +- Q10.cs | 8 +++++--- Q11.cs | 4 ++-- Q12.cs | 4 ++-- Q13.cs | 4 +++- Q14.cs | 10 ++++++---- Q15.cs | 2 +- Q18.cs | 15 +++++---------- Q19.cs | 20 +++++++++++--------- Q21.cs | 4 ++-- Q22.cs | 2 +- 17 files changed, 56 insertions(+), 53 deletions(-) diff --git a/Program.cs b/Program.cs index 0165408..8bc120b 100644 --- a/Program.cs +++ b/Program.cs @@ -2,7 +2,7 @@ { class Program { - static void Main(string[] args) + static void Main() { var start = System.DateTime.Now; Q01.Go(); diff --git a/Q03.cs b/Q03.cs index 260b8e7..9488115 100644 --- a/Q03.cs +++ b/Q03.cs @@ -25,7 +25,7 @@ namespace _2020 list.Add(new List()); for (int i = 0; i < line.Length; i++) { - list[list.Count - 1].Add(line[i] != '.'); + list[^1].Add(line[i] != '.'); } } diff --git a/Q04.cs b/Q04.cs index 31d153d..621e43e 100644 --- a/Q04.cs +++ b/Q04.cs @@ -7,11 +7,11 @@ namespace _2020 { class Q04 { - static List> Passports = new List>(); + static readonly List> Passports = new List>(); - static Regex PassportRegex = new Regex(@"(?[a-z]{3}):(?\S+)", RegexOptions.Compiled); - static Regex HTMLColor = new Regex("^#[0-9a-f]{6}$", RegexOptions.Compiled); - static Regex PassportID = new Regex("^[0-9]{9}$", RegexOptions.Compiled); + static readonly Regex PassportRegex = new Regex(@"(?[a-z]{3}):(?\S+)", RegexOptions.Compiled); + static readonly Regex HTMLColor = new Regex("^#[0-9a-f]{6}$", RegexOptions.Compiled); + static readonly Regex PassportID = new Regex("^[0-9]{9}$", RegexOptions.Compiled); public static void Go() { @@ -35,7 +35,7 @@ namespace _2020 var matches = PassportRegex.Matches(line); for (int i = 0; i < matches.Count; i++) { - Passports[Passports.Count - 1].Add(matches[i].Groups["key"].Value, matches[i].Groups["val"].Value); + Passports[^1].Add(matches[i].Groups["key"].Value, matches[i].Groups["val"].Value); } } } diff --git a/Q06.cs b/Q06.cs index f9825f5..a96e334 100644 --- a/Q06.cs +++ b/Q06.cs @@ -6,12 +6,12 @@ namespace _2020 { class Q06 { - class group + class Group { public int numInGroup; public Dictionary answers; } - static List list = new List(); + static readonly List list = new List(); public static void Go() { @@ -25,12 +25,12 @@ namespace _2020 using var fs = new FileStream("06input.txt", FileMode.Open); using var sr = new StreamReader(fs); string line; - group entry = null; + Group entry = null; while ((line = sr.ReadLine()) != null) { if (line.Length == 0 || entry == null) { - entry = new group() + entry = new Group() { numInGroup = 0, answers = new Dictionary(), diff --git a/Q07.cs b/Q07.cs index e3b885e..6b601de 100644 --- a/Q07.cs +++ b/Q07.cs @@ -14,9 +14,9 @@ namespace _2020 public List> ValidContents = new List>(); } - static Regex bagRegex = new Regex(@"(?: contain |, )?(?[0-9]+ )?(?.+?) bags?", RegexOptions.Compiled); + static readonly Regex bagRegex = new Regex(@"(?: contain |, )?(?[0-9]+ )?(?.+?) bags?", RegexOptions.Compiled); - static List list = new List(); + static readonly List list = new List(); public static void Go() { @@ -82,11 +82,11 @@ namespace _2020 static void Part1() { - Func, BagType, List> bagAgg = (accum, bagType) => + static List bagAgg(List accum, BagType bagType) { accum.Add(bagType.BagName); return accum; - }; + } var directContain = GetBagsCanContain(new BagType() { BagName = "shiny gold" }); var totalContain = directContain.Aggregate(new List(), bagAgg); diff --git a/Q08.cs b/Q08.cs index 743b97c..ee58dc7 100644 --- a/Q08.cs +++ b/Q08.cs @@ -7,7 +7,7 @@ namespace _2020 { class Q08 { - static List> instructions = new List>(); + static readonly List> instructions = new List>(); public static void Go() { @@ -21,7 +21,7 @@ namespace _2020 foreach (var line in File.ReadAllLines("08input.txt")) { var parts = line.Split(' '); - if (parts.Count() != 2) + if (parts.Length != 2) { throw new Exception("malformed input"); } diff --git a/Q09.cs b/Q09.cs index 8c27c81..b5b681d 100644 --- a/Q09.cs +++ b/Q09.cs @@ -6,7 +6,7 @@ namespace _2020 { class Q09 { - static List list = new List(); + static readonly List list = new List(); const int chunkLength = 25; diff --git a/Q10.cs b/Q10.cs index 3eb02f4..275dec2 100644 --- a/Q10.cs +++ b/Q10.cs @@ -6,7 +6,7 @@ namespace _2020 { class Q10 { - static List list = new List(); + static readonly List list = new List(); static List sortedList = null; public static void Go() @@ -51,8 +51,10 @@ namespace _2020 static void Part2() { - var pathsToIndices = new List(); - pathsToIndices.Add(1); + var pathsToIndices = new List + { + 1 + }; for (int i = 1; i < sortedList.Count; i++) { long pathLen = 0; diff --git a/Q11.cs b/Q11.cs index 545c849..8ea8f6a 100644 --- a/Q11.cs +++ b/Q11.cs @@ -100,7 +100,7 @@ namespace _2020 { int numOccupied = 0; - Func checkSeat = (checkRow, checkCol) => + bool checkSeat(int checkRow, int checkCol) { if (seatList[checkRow, checkCol] == SeatState.Occupied) { @@ -113,7 +113,7 @@ namespace _2020 } return false; - }; + } // ul for (int checkRow = row - 1, checkCol = col - 1; checkRow >= 0 && checkCol >= 0; checkRow--, checkCol--) diff --git a/Q12.cs b/Q12.cs index 93e6a52..2f134b6 100644 --- a/Q12.cs +++ b/Q12.cs @@ -18,7 +18,7 @@ namespace _2020 static Vec2 waypointPosition = new Vec2() { x = 10, y = 1 }; static int facingDir = 90; - static List> instructions = new List>(); + static readonly List> instructions = new List>(); public static void Go() { @@ -119,7 +119,7 @@ namespace _2020 } else if (dir == 'L') { - facingDir = (facingDir - amount); + facingDir -= amount; while (facingDir < 0) { facingDir = 360 + facingDir; diff --git a/Q13.cs b/Q13.cs index 402ece0..b26bfd3 100644 --- a/Q13.cs +++ b/Q13.cs @@ -10,7 +10,7 @@ namespace _2020 class Q13 { static int leaveTime; - static List busses = new List(); + static readonly List busses = new List(); public static void Go() { @@ -116,7 +116,9 @@ namespace _2020 } // this is how i initially solved it. took ~30mins. i'm not proud of it, but the "real" answer apparently required specialized knowledge i did not have. +#pragma warning disable IDE0051 // Remove unused private members static void Part2BruteForce() +#pragma warning restore IDE0051 // Remove unused private members { int root = busses.First(x => x > 0); var sorted = busses.OrderBy(x => x); diff --git a/Q14.cs b/Q14.cs index e1d21a0..6f84e8e 100644 --- a/Q14.cs +++ b/Q14.cs @@ -21,7 +21,7 @@ namespace _2020 public List mask = new List(); public List writeInstructions = new List(); } - static List instructions = new List(); + static readonly List instructions = new List(); public static void Go() { @@ -64,9 +64,11 @@ namespace _2020 { var inst = instructions[^1]; - var writeInst = new Instruction.WriteInstruction(); - writeInst.idx = Convert.ToUInt64(line["mem[".Length..line.IndexOf("]")]); - writeInst.value = Convert.ToUInt64(line[(line.IndexOf('=') + 1)..]); + var writeInst = new Instruction.WriteInstruction + { + idx = Convert.ToUInt64(line["mem[".Length..line.IndexOf("]")]), + value = Convert.ToUInt64(line[(line.IndexOf('=') + 1)..]) + }; inst.writeInstructions.Add(writeInst); } diff --git a/Q15.cs b/Q15.cs index baecdd6..154f1ff 100644 --- a/Q15.cs +++ b/Q15.cs @@ -6,7 +6,7 @@ namespace _2020 { class Q15 { - static List list = new List(); + static readonly List list = new List(); public static void Go() { diff --git a/Q18.cs b/Q18.cs index 608d159..3d51b52 100644 --- a/Q18.cs +++ b/Q18.cs @@ -30,17 +30,12 @@ namespace _2020 static long DoOp(long left, long right, char op) { - switch (op) + return op switch { - case '+': - return left + right; - - case '*': - return left * right; - - default: - throw new Exception("unrecognized op"); - } + '+' => left + right, + '*' => left * right, + _ => throw new Exception("unrecognized op"), + }; } static void Part1() diff --git a/Q19.cs b/Q19.cs index 28b8d49..464997b 100644 --- a/Q19.cs +++ b/Q19.cs @@ -7,8 +7,8 @@ namespace _2020 { class Q19 { - static Dictionary rules = new Dictionary(); - static List messages = new List(); + static readonly Dictionary rules = new Dictionary(); + static readonly List messages = new List(); [DebuggerDisplay("ch={ch} | num rules={ruleIdxs?.Count}")] class Rule @@ -141,10 +141,11 @@ namespace _2020 static void Part2() { - var rulesUpdated = new Dictionary(rules); - rulesUpdated[8] = new Rule() + var rulesUpdated = new Dictionary(rules) { - ruleIdxs = new List>() + [8] = new Rule() + { + ruleIdxs = new List>() { new List() { @@ -156,10 +157,10 @@ namespace _2020 8, }, }, - }; - rulesUpdated[11] = new Rule() - { - ruleIdxs = new List>() + }, + [11] = new Rule() + { + ruleIdxs = new List>() { new List() { @@ -173,6 +174,7 @@ namespace _2020 31, }, }, + } }; int numMatched = 0; diff --git a/Q21.cs b/Q21.cs index 02a032d..66032c2 100644 --- a/Q21.cs +++ b/Q21.cs @@ -8,8 +8,8 @@ namespace _2020 { class Q21 { - static List, List>> list = new List, List>>(); - static Dictionary ingredientCounts = new Dictionary(); + static readonly List, List>> list = new List, List>>(); + static readonly Dictionary ingredientCounts = new Dictionary(); static IEnumerable allAllergens = new List(); static IEnumerable allIngredients = new List(); diff --git a/Q22.cs b/Q22.cs index d87abaf..a758810 100644 --- a/Q22.cs +++ b/Q22.cs @@ -7,7 +7,7 @@ namespace _2020 { class Q22 { - static Dictionary> list = new Dictionary>(); + static readonly Dictionary> list = new Dictionary>(); public static void Go() {