Make a ton of VS-recommended changes. Sure, why not.

This commit is contained in:
2020-12-22 13:04:55 -06:00
parent f420af35cc
commit 0a3fe365d0
17 changed files with 56 additions and 53 deletions

View File

@ -2,7 +2,7 @@
{ {
class Program class Program
{ {
static void Main(string[] args) static void Main()
{ {
var start = System.DateTime.Now; var start = System.DateTime.Now;
Q01.Go(); Q01.Go();

2
Q03.cs
View File

@ -25,7 +25,7 @@ namespace _2020
list.Add(new List<bool>()); list.Add(new List<bool>());
for (int i = 0; i < line.Length; i++) for (int i = 0; i < line.Length; i++)
{ {
list[list.Count - 1].Add(line[i] != '.'); list[^1].Add(line[i] != '.');
} }
} }

10
Q04.cs
View File

@ -7,11 +7,11 @@ namespace _2020
{ {
class Q04 class Q04
{ {
static List<Dictionary<string, string>> Passports = new List<Dictionary<string, string>>(); static readonly List<Dictionary<string, string>> Passports = new List<Dictionary<string, string>>();
static Regex PassportRegex = new Regex(@"(?<key>[a-z]{3}):(?<val>\S+)", RegexOptions.Compiled); static readonly Regex PassportRegex = new Regex(@"(?<key>[a-z]{3}):(?<val>\S+)", RegexOptions.Compiled);
static Regex HTMLColor = new Regex("^#[0-9a-f]{6}$", RegexOptions.Compiled); static readonly Regex HTMLColor = new Regex("^#[0-9a-f]{6}$", RegexOptions.Compiled);
static Regex PassportID = new Regex("^[0-9]{9}$", RegexOptions.Compiled); static readonly Regex PassportID = new Regex("^[0-9]{9}$", RegexOptions.Compiled);
public static void Go() public static void Go()
{ {
@ -35,7 +35,7 @@ namespace _2020
var matches = PassportRegex.Matches(line); var matches = PassportRegex.Matches(line);
for (int i = 0; i < matches.Count; i++) 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);
} }
} }
} }

8
Q06.cs
View File

@ -6,12 +6,12 @@ namespace _2020
{ {
class Q06 class Q06
{ {
class group class Group
{ {
public int numInGroup; public int numInGroup;
public Dictionary<char, int> answers; public Dictionary<char, int> answers;
} }
static List<group> list = new List<group>(); static readonly List<Group> list = new List<Group>();
public static void Go() public static void Go()
{ {
@ -25,12 +25,12 @@ namespace _2020
using var fs = new FileStream("06input.txt", FileMode.Open); using var fs = new FileStream("06input.txt", FileMode.Open);
using var sr = new StreamReader(fs); using var sr = new StreamReader(fs);
string line; string line;
group entry = null; Group entry = null;
while ((line = sr.ReadLine()) != null) while ((line = sr.ReadLine()) != null)
{ {
if (line.Length == 0 || entry == null) if (line.Length == 0 || entry == null)
{ {
entry = new group() entry = new Group()
{ {
numInGroup = 0, numInGroup = 0,
answers = new Dictionary<char, int>(), answers = new Dictionary<char, int>(),

8
Q07.cs
View File

@ -14,9 +14,9 @@ namespace _2020
public List<Tuple<string, int>> ValidContents = new List<Tuple<string, int>>(); public List<Tuple<string, int>> ValidContents = new List<Tuple<string, int>>();
} }
static Regex bagRegex = new Regex(@"(?: contain |, )?(?<numBags>[0-9]+ )?(?<bagType>.+?) bags?", RegexOptions.Compiled); static readonly Regex bagRegex = new Regex(@"(?: contain |, )?(?<numBags>[0-9]+ )?(?<bagType>.+?) bags?", RegexOptions.Compiled);
static List<BagType> list = new List<BagType>(); static readonly List<BagType> list = new List<BagType>();
public static void Go() public static void Go()
{ {
@ -82,11 +82,11 @@ namespace _2020
static void Part1() static void Part1()
{ {
Func<List<string>, BagType, List<string>> bagAgg = (accum, bagType) => static List<string> bagAgg(List<string> accum, BagType bagType)
{ {
accum.Add(bagType.BagName); accum.Add(bagType.BagName);
return accum; return accum;
}; }
var directContain = GetBagsCanContain(new BagType() { BagName = "shiny gold" }); var directContain = GetBagsCanContain(new BagType() { BagName = "shiny gold" });
var totalContain = directContain.Aggregate(new List<string>(), bagAgg); var totalContain = directContain.Aggregate(new List<string>(), bagAgg);

4
Q08.cs
View File

@ -7,7 +7,7 @@ namespace _2020
{ {
class Q08 class Q08
{ {
static List<Tuple<string, int>> instructions = new List<Tuple<string, int>>(); static readonly List<Tuple<string, int>> instructions = new List<Tuple<string, int>>();
public static void Go() public static void Go()
{ {
@ -21,7 +21,7 @@ namespace _2020
foreach (var line in File.ReadAllLines("08input.txt")) foreach (var line in File.ReadAllLines("08input.txt"))
{ {
var parts = line.Split(' '); var parts = line.Split(' ');
if (parts.Count() != 2) if (parts.Length != 2)
{ {
throw new Exception("malformed input"); throw new Exception("malformed input");
} }

2
Q09.cs
View File

@ -6,7 +6,7 @@ namespace _2020
{ {
class Q09 class Q09
{ {
static List<long> list = new List<long>(); static readonly List<long> list = new List<long>();
const int chunkLength = 25; const int chunkLength = 25;

8
Q10.cs
View File

@ -6,7 +6,7 @@ namespace _2020
{ {
class Q10 class Q10
{ {
static List<long> list = new List<long>(); static readonly List<long> list = new List<long>();
static List<long> sortedList = null; static List<long> sortedList = null;
public static void Go() public static void Go()
@ -51,8 +51,10 @@ namespace _2020
static void Part2() static void Part2()
{ {
var pathsToIndices = new List<long>(); var pathsToIndices = new List<long>
pathsToIndices.Add(1); {
1
};
for (int i = 1; i < sortedList.Count; i++) for (int i = 1; i < sortedList.Count; i++)
{ {
long pathLen = 0; long pathLen = 0;

4
Q11.cs
View File

@ -100,7 +100,7 @@ namespace _2020
{ {
int numOccupied = 0; int numOccupied = 0;
Func<int, int, bool> checkSeat = (checkRow, checkCol) => bool checkSeat(int checkRow, int checkCol)
{ {
if (seatList[checkRow, checkCol] == SeatState.Occupied) if (seatList[checkRow, checkCol] == SeatState.Occupied)
{ {
@ -113,7 +113,7 @@ namespace _2020
} }
return false; return false;
}; }
// ul // ul
for (int checkRow = row - 1, checkCol = col - 1; checkRow >= 0 && checkCol >= 0; checkRow--, checkCol--) for (int checkRow = row - 1, checkCol = col - 1; checkRow >= 0 && checkCol >= 0; checkRow--, checkCol--)

4
Q12.cs
View File

@ -18,7 +18,7 @@ namespace _2020
static Vec2 waypointPosition = new Vec2() { x = 10, y = 1 }; static Vec2 waypointPosition = new Vec2() { x = 10, y = 1 };
static int facingDir = 90; static int facingDir = 90;
static List<Tuple<char, int>> instructions = new List<Tuple<char, int>>(); static readonly List<Tuple<char, int>> instructions = new List<Tuple<char, int>>();
public static void Go() public static void Go()
{ {
@ -119,7 +119,7 @@ namespace _2020
} }
else if (dir == 'L') else if (dir == 'L')
{ {
facingDir = (facingDir - amount); facingDir -= amount;
while (facingDir < 0) while (facingDir < 0)
{ {
facingDir = 360 + facingDir; facingDir = 360 + facingDir;

4
Q13.cs
View File

@ -10,7 +10,7 @@ namespace _2020
class Q13 class Q13
{ {
static int leaveTime; static int leaveTime;
static List<int> busses = new List<int>(); static readonly List<int> busses = new List<int>();
public static void Go() 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. // 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() static void Part2BruteForce()
#pragma warning restore IDE0051 // Remove unused private members
{ {
int root = busses.First(x => x > 0); int root = busses.First(x => x > 0);
var sorted = busses.OrderBy(x => x); var sorted = busses.OrderBy(x => x);

10
Q14.cs
View File

@ -21,7 +21,7 @@ namespace _2020
public List<byte> mask = new List<byte>(); public List<byte> mask = new List<byte>();
public List<WriteInstruction> writeInstructions = new List<WriteInstruction>(); public List<WriteInstruction> writeInstructions = new List<WriteInstruction>();
} }
static List<Instruction> instructions = new List<Instruction>(); static readonly List<Instruction> instructions = new List<Instruction>();
public static void Go() public static void Go()
{ {
@ -64,9 +64,11 @@ namespace _2020
{ {
var inst = instructions[^1]; var inst = instructions[^1];
var writeInst = new Instruction.WriteInstruction(); var writeInst = new Instruction.WriteInstruction
writeInst.idx = Convert.ToUInt64(line["mem[".Length..line.IndexOf("]")]); {
writeInst.value = Convert.ToUInt64(line[(line.IndexOf('=') + 1)..]); idx = Convert.ToUInt64(line["mem[".Length..line.IndexOf("]")]),
value = Convert.ToUInt64(line[(line.IndexOf('=') + 1)..])
};
inst.writeInstructions.Add(writeInst); inst.writeInstructions.Add(writeInst);
} }

2
Q15.cs
View File

@ -6,7 +6,7 @@ namespace _2020
{ {
class Q15 class Q15
{ {
static List<int> list = new List<int>(); static readonly List<int> list = new List<int>();
public static void Go() public static void Go()
{ {

15
Q18.cs
View File

@ -30,17 +30,12 @@ namespace _2020
static long DoOp(long left, long right, char op) static long DoOp(long left, long right, char op)
{ {
switch (op) return op switch
{ {
case '+': '+' => left + right,
return left + right; '*' => left * right,
_ => throw new Exception("unrecognized op"),
case '*': };
return left * right;
default:
throw new Exception("unrecognized op");
}
} }
static void Part1() static void Part1()

20
Q19.cs
View File

@ -7,8 +7,8 @@ namespace _2020
{ {
class Q19 class Q19
{ {
static Dictionary<int, Rule> rules = new Dictionary<int, Rule>(); static readonly Dictionary<int, Rule> rules = new Dictionary<int, Rule>();
static List<string> messages = new List<string>(); static readonly List<string> messages = new List<string>();
[DebuggerDisplay("ch={ch} | num rules={ruleIdxs?.Count}")] [DebuggerDisplay("ch={ch} | num rules={ruleIdxs?.Count}")]
class Rule class Rule
@ -141,10 +141,11 @@ namespace _2020
static void Part2() static void Part2()
{ {
var rulesUpdated = new Dictionary<int, Rule>(rules); var rulesUpdated = new Dictionary<int, Rule>(rules)
rulesUpdated[8] = new Rule()
{ {
ruleIdxs = new List<List<int>>() [8] = new Rule()
{
ruleIdxs = new List<List<int>>()
{ {
new List<int>() new List<int>()
{ {
@ -156,10 +157,10 @@ namespace _2020
8, 8,
}, },
}, },
}; },
rulesUpdated[11] = new Rule() [11] = new Rule()
{ {
ruleIdxs = new List<List<int>>() ruleIdxs = new List<List<int>>()
{ {
new List<int>() new List<int>()
{ {
@ -173,6 +174,7 @@ namespace _2020
31, 31,
}, },
}, },
}
}; };
int numMatched = 0; int numMatched = 0;

4
Q21.cs
View File

@ -8,8 +8,8 @@ namespace _2020
{ {
class Q21 class Q21
{ {
static List<Tuple<List<string>, List<string>>> list = new List<Tuple<List<string>, List<string>>>(); static readonly List<Tuple<List<string>, List<string>>> list = new List<Tuple<List<string>, List<string>>>();
static Dictionary<string, int> ingredientCounts = new Dictionary<string, int>(); static readonly Dictionary<string, int> ingredientCounts = new Dictionary<string, int>();
static IEnumerable<string> allAllergens = new List<string>(); static IEnumerable<string> allAllergens = new List<string>();
static IEnumerable<string> allIngredients = new List<string>(); static IEnumerable<string> allIngredients = new List<string>();

2
Q22.cs
View File

@ -7,7 +7,7 @@ namespace _2020
{ {
class Q22 class Q22
{ {
static Dictionary<int, List<int>> list = new Dictionary<int, List<int>>(); static readonly Dictionary<int, List<int>> list = new Dictionary<int, List<int>>();
public static void Go() public static void Go()
{ {