my advent of code solutions

apply style fixes

* trailing comma
* prefer concrete collection type
* collection expression

+110 -98
+1 -1
Solutions/2015/Day04.cs
··· 18 18 while (true) 19 19 { 20 20 var hash = MD5.HashData(Encoding.ASCII.GetBytes(_key + counter)); 21 - if (BitConverter.ToString(hash).Replace("-", "").StartsWith("00000")) 21 + if (Convert.ToHexString(hash).StartsWith("00000")) 22 22 return counter; 23 23 counter++; 24 24 }
+2 -1
Solutions/2015/Day07.cs
··· 29 29 case 3: 30 30 if (ushort.TryParse(split[0], out var val)) 31 31 { 32 - _actions.Add(destination, () => val); 32 + var result = val; 33 + _actions.Add(destination, () => result); 33 34 _wires.Add(destination, val); 34 35 } 35 36 else
+1 -1
Solutions/2015/Day18.cs
··· 32 32 lights[i][j + 1], 33 33 lights[i + 1][j - 1], 34 34 lights[i + 1][j], 35 - lights[i + 1][j + 1] 35 + lights[i + 1][j + 1], 36 36 }.Count(n => n); 37 37 38 38 nextGrid[i][j] = lights[i][j] ? activeNeighbors is 2 or 3 : activeNeighbors == 3;
+3 -3
Solutions/2015/Day21.cs
··· 28 28 new(Name: "Shortsword", Cost: 10, Damage: 5), 29 29 new(Name: "Warhammer", Cost: 25, Damage: 6), 30 30 new(Name: "Longsword", Cost: 40, Damage: 7), 31 - new(Name: "Greataxe", Cost: 74, Damage: 8) 31 + new(Name: "Greataxe", Cost: 74, Damage: 8), 32 32 }; 33 33 34 34 var armor = new Item[] ··· 38 38 new(Name: "Chainmail", Cost: 31, Armor: 2), 39 39 new(Name: "Splintmail", Cost: 53, Armor: 3), 40 40 new(Name: "Bandedmail", Cost: 75, Armor: 4), 41 - new(Name: "Platemail", Cost: 102, Armor: 5) 41 + new(Name: "Platemail", Cost: 102, Armor: 5), 42 42 }; 43 43 44 44 var rings = new Item[] ··· 49 49 new(Name: "Damage +3", Cost: 100, Damage: 3, Armor: 0), 50 50 new(Name: "Defense +1", Cost: 20, Damage: 0, Armor: 1), 51 51 new(Name: "Defense +2", Cost: 40, Damage: 0, Armor: 2), 52 - new(Name: "Defense +3", Cost: 80, Damage: 0, Armor: 3) 52 + new(Name: "Defense +3", Cost: 80, Damage: 0, Armor: 3), 53 53 }; 54 54 55 55 _combinations =
+13 -6
Solutions/2015/Day22.cs
··· 13 13 new("Drain", Mana: 73, Damage: 2, Heal: 2), 14 14 new("Shield", Mana: 113, Armor: 7, Duration: 6), 15 15 new("Poison", Mana: 173, Damage: 3, Duration: 6), 16 - new("Recharge", Mana: 229, ManaCharge: 101, Duration: 5) 16 + new("Recharge", Mana: 229, ManaCharge: 101, Duration: 5), 17 17 ]; 18 18 19 19 private Dictionary<string, int> _boss = new(); ··· 29 29 int ManaCharge = 0 30 30 ); 31 31 32 - private struct GameState(bool hardMode = false, int roundNumber = 0, int totalManaSpent = 0, int playerHealth = 50, 33 - int playerMana = 500, int bossHealth = 0, int bossDamage = 0, Dictionary<Spell, int>? activeSpells = null) 32 + private struct GameState( 33 + bool hardMode = false, 34 + int roundNumber = 0, 35 + int totalManaSpent = 0, 36 + int playerHealth = 50, 37 + int playerMana = 500, 38 + int bossHealth = 0, 39 + int bossDamage = 0, 40 + Dictionary<Spell, int>? activeSpells = null) 34 41 { 35 42 public GameResult DoTurn(Spell spell) 36 43 { ··· 63 70 private void ProcessActiveSpells() 64 71 { 65 72 if (activeSpells is null) return; 66 - 73 + 67 74 activeSpells.Keys.ForEach(ProcessSpell); 68 75 foreach (var (spell, duration) in activeSpells.ToList()) 69 76 { ··· 84 91 { 85 92 Win, 86 93 Loss, 87 - Continue 94 + Continue, 88 95 } 89 96 90 97 private GameState ProcessStates(GameState initialState) ··· 105 112 _boss = Input.ToDictionary(k => k.Split(": ")[0], v => int.Parse(v.Split(": ")[1])); 106 113 107 114 public override object Part1() => ""; 108 - // ProcessStates(new(bossHealth: _boss["Hit Points"], bossDamage: _boss["Damage"])); 115 + // ProcessStates(new(bossHealth: _boss["Hit Points"], bossDamage: _boss["Damage"])); 109 116 110 117 public override object Part2() => ""; 111 118 }
+1 -1
Solutions/2015/Day23.cs
··· 10 10 Dictionary<char, int> registers = new() 11 11 { 12 12 ['a'] = initialA, 13 - ['b'] = initialB 13 + ['b'] = initialB, 14 14 }; 15 15 16 16 var input = Input.ToList();
+1 -1
Solutions/2016/Day01.cs
··· 34 34 Direction.South => (coord.x, coord.y - 1), 35 35 Direction.East => (coord.x + 1, coord.y), 36 36 Direction.West => (coord.x - 1, coord.y), 37 - _ => (0, 0) 37 + _ => (0, 0), 38 38 }; 39 39 40 40 public override object Part1()
+2 -2
Solutions/2016/Day02.cs
··· 18 18 'D' => c with { y = c.y == 2 ? c.y : c.y + 1 }, 19 19 'L' => c with { x = c.x == 0 ? c.x : c.x - 1 }, 20 20 'R' => c with { x = c.x == 2 ? c.x : c.x + 1 }, 21 - _ => throw new ArgumentException("invalid direction", nameof(instruction)) 21 + _ => throw new ArgumentException("invalid direction", nameof(instruction)), 22 22 }); 23 23 24 24 answer.Add(1 + location.x + location.y * 3); ··· 48 48 'D' => c with { y = c.y == 4 || keyPad[c.y + 1, c.x] == '\0' ? c.y : c.y + 1 }, 49 49 'L' => c with { x = c.x == 0 || keyPad[c.y, c.x - 1] == '\0' ? c.x : c.x - 1 }, 50 50 'R' => c with { x = c.x == 4 || keyPad[c.y, c.x + 1] == '\0' ? c.x : c.x + 1 }, 51 - _ => throw new ArgumentException("invalid direction", nameof(instruction)) 51 + _ => throw new ArgumentException("invalid direction", nameof(instruction)), 52 52 }); 53 53 54 54 answer.Add(keyPad[location.y, location.x]);
+4 -4
Solutions/2016/Day07.cs
··· 31 31 private static bool SupportsSsl(string input) 32 32 { 33 33 foreach (var ip in BracketsRegex().Split(input)) 34 - foreach (var aba in CheckAba(ip)) 35 - foreach (var m in InsideBracketsRegex().Matches(input).Cast<Match>()) 36 - if (m.Value.Contains($"{aba[1]}{aba[0]}{aba[1]}")) 37 - return true; 34 + foreach (var aba in CheckAba(ip)) 35 + foreach (var m in InsideBracketsRegex().Matches(input).Cast<Match>()) 36 + if (m.Value.Contains($"{aba[1]}{aba[0]}{aba[1]}")) 37 + return true; 38 38 39 39 return false; 40 40 }
+1 -1
Solutions/2019/Day02.cs
··· 18 18 { 19 19 1 => v[v[i + 1]] + v[v[i + 2]], 20 20 2 => v[v[i + 1]] * v[v[i + 2]], 21 - _ => throw new ArgumentOutOfRangeException(nameof(verb)) 21 + _ => throw new ArgumentOutOfRangeException(nameof(verb)), 22 22 }; 23 23 24 24 return v[0];
+1 -1
Solutions/2019/Day05.cs
··· 8 8 public override void ProcessInput() => 9 9 _tape = Input.First().Split(',').Select(int.Parse); 10 10 11 - private void RunIntCode(IList<int> v, int input) 11 + private void RunIntCode(List<int> v, int input) 12 12 { 13 13 var i = 0; 14 14 while (i < v.Count && v[i] != 99)
+1 -1
Solutions/2019/Day10.cs
··· 19 19 foreach (var asteroid in _asteroids!) 20 20 { 21 21 var canSee = _asteroids 22 - .Except(new[] { asteroid }) 22 + .Except([asteroid]) 23 23 .Select(a => (x: a.x - asteroid.x, y: a.y - asteroid.y)) 24 24 .GroupBy(a => Math.Atan2(a.y, a.x)) 25 25 .Count();
+2 -2
Solutions/2019/Day11.cs
··· 11 11 Up, 12 12 Down, 13 13 Left, 14 - Right 14 + Right, 15 15 } 16 16 17 17 public override void ProcessInput() => ··· 46 46 Direction.Down => direction == 0 ? Direction.Right : Direction.Left, 47 47 Direction.Left => direction == 0 ? Direction.Down : Direction.Up, 48 48 Direction.Right => direction == 0 ? Direction.Up : Direction.Down, 49 - _ => _heading 49 + _ => _heading, 50 50 }; 51 51 52 52 Move();
+1 -1
Solutions/2019/Day13.cs
··· 24 24 2 => "#", 25 25 3 => "_", 26 26 4 => ".", 27 - _ => value 27 + _ => value, 28 28 }); 29 29 } 30 30 }
+1 -1
Solutions/2019/Day14.cs
··· 92 92 return new() 93 93 { 94 94 Quantity = int.Parse(spl[0]), 95 - Name = spl[1] 95 + Name = spl[1], 96 96 }; 97 97 } 98 98 }
+2 -2
Solutions/2019/Day15.cs
··· 49 49 currentLocation = _vm.Result switch 50 50 { 51 51 Location.Empty or Location.System => Location.GetLocation(currentLocation.Neighbor(direction)), 52 - _ => throw new($"Unknown or unexpected response for previous room: {_vm.Result}") 52 + _ => throw new($"Unknown or unexpected response for previous room: {_vm.Result}"), 53 53 }; 54 54 } 55 55 else ··· 161 161 Wall => "\u2587", 162 162 Empty => X == 0 && Y == 0 ? "S" : " ", 163 163 System => "O", 164 - _ => "?" 164 + _ => "?", 165 165 }; 166 166 167 167 public bool UpdateDistanceToOxygenSystem()
+1 -1
Solutions/2019/IntCodeVM.cs
··· 56 56 0 => MemGet(param), 57 57 1 => param, 58 58 2 => MemGet(_relativeBase + param), 59 - _ => throw new("invalid parameter mode") 59 + _ => throw new("invalid parameter mode"), 60 60 }; 61 61 } 62 62
+1 -1
Solutions/2020/Day10.cs
··· 12 12 { 13 13 var parsed = Input.Select(int.Parse).ToArray(); 14 14 // add socket and device to the list 15 - _adapters = parsed.Concat(new[] { 0, parsed.Max() + 3 }).OrderBy(i => i).ToArray(); 15 + _adapters = parsed.Concat([0, parsed.Max() + 3]).OrderBy(i => i).ToArray(); 16 16 _memo = new long[_adapters.Length]; 17 17 } 18 18
+4 -4
Solutions/2020/Day11.cs
··· 74 74 { 75 75 'L' when CountAdjacent(y, x) == 0 => '#', 76 76 '#' when CountAdjacent(y, x) >= 4 => 'L', 77 - _ => Grid[y][x] 77 + _ => Grid[y][x], 78 78 }; 79 79 80 80 // next.PrintBoard(); ··· 88 88 new[] 89 89 { 90 90 At(y - 1, x - 1), At(y - 1, x + 0), At(y - 1, x + 1), At(y + 0, x - 1), At(y + 0, x + 1), 91 - At(y + 1, x - 1), At(y + 1, x + 0), At(y + 1, x + 1) 91 + At(y + 1, x - 1), At(y + 1, x + 0), At(y + 1, x + 1), 92 92 }.Count(c => c == '#'); 93 93 94 94 public LifeGame StepPart2() ··· 100 100 { 101 101 'L' when CanSee(y, x) == 0 => '#', 102 102 '#' when CanSee(y, x) >= 5 => 'L', 103 - _ => Grid[y][x] 103 + _ => Grid[y][x], 104 104 }; 105 105 106 106 // next.PrintBoard(); ··· 111 111 new[] 112 112 { 113 113 TraceRay(y, x, -1, -1), TraceRay(y, x, -1, +0), TraceRay(y, x, -1, +1), TraceRay(y, x, +0, -1), 114 - TraceRay(y, x, +0, +1), TraceRay(y, x, +1, -1), TraceRay(y, x, +1, +0), TraceRay(y, x, +1, +1) 114 + TraceRay(y, x, +0, +1), TraceRay(y, x, +1, -1), TraceRay(y, x, +1, +0), TraceRay(y, x, +1, +1), 115 115 }.Count(c => c == '#'); 116 116 117 117 private char TraceRay(int y, int x, int dy, int dx)
+4 -4
Solutions/2020/Day17.cs
··· 33 33 _plane4[(x, y, 0, 0)] = input[y][x]; 34 34 } 35 35 36 - private static int Neighbors(IReadOnlyDictionary<(int x, int y, int z), char> plane, int x, int y, int z) 36 + private static int Neighbors(Dictionary<(int x, int y, int z), char> plane, int x, int y, int z) 37 37 { 38 38 var neighbors = 0; 39 39 ··· 49 49 } 50 50 51 51 private static Dictionary<(int x, int y, int z), char> Iterate( 52 - IReadOnlyDictionary<(int x, int y, int z), char> prev) 52 + Dictionary<(int x, int y, int z), char> prev) 53 53 { 54 54 var next = new Dictionary<(int x, int y, int z), char>(); 55 55 ··· 67 67 return next; 68 68 } 69 69 70 - private static int Neighbors4(IReadOnlyDictionary<(int x, int y, int z, int w), char> plane, int x, int y, 70 + private static int Neighbors4(Dictionary<(int x, int y, int z, int w), char> plane, int x, int y, 71 71 int z, int w) 72 72 { 73 73 var neighbors = 0; ··· 85 85 } 86 86 87 87 private static Dictionary<(int x, int y, int z, int w), char> Iterate4( 88 - IReadOnlyDictionary<(int x, int y, int z, int w), char> prev) 88 + Dictionary<(int x, int y, int z, int w), char> prev) 89 89 { 90 90 var next = new Dictionary<(int x, int y, int z, int w), char>(); 91 91
+1 -1
Solutions/2020/Day19.cs
··· 30 30 var sub = string.Join("|", _rules![key].Select(test => test.Length switch 31 31 { 32 32 1 => test[0][0] == '"' ? test[0].Trim('"') : MakeRegexExpression(test[0]), 33 - _ => string.Join(string.Empty, test.Select(MakeRegexExpression)) 33 + _ => string.Join(string.Empty, test.Select(MakeRegexExpression)), 34 34 })); 35 35 _stack.Pop(); 36 36 return _rules[key].Length > 1 ? $"({sub})" : sub;
+1 -1
Solutions/2020/Day20.cs
··· 239 239 (piece._topSide.Value, new(piece._topSide.Value.Reverse().ToArray())), 240 240 (piece.RightSide.Value, new(piece.RightSide.Value.Reverse().ToArray())), 241 241 (piece.BottomSide.Value, new(piece.BottomSide.Value.Reverse().ToArray())), 242 - (piece._leftSide.Value, new(piece._leftSide.Value.Reverse().ToArray())) 242 + (piece._leftSide.Value, new(piece._leftSide.Value.Reverse().ToArray())), 243 243 ]; 244 244 245 245 private static HashSet<string> CalculateAllSidesWithFlipped(PuzzlePiece piece) =>
+1 -1
Solutions/2020/Day22.cs
··· 83 83 bool recursive) => 84 84 Play(new(enumerable1), new(enumerable2), recursive); 85 85 86 - private static int CalculateScore(IReadOnlyCollection<int> deck) => 86 + private static int CalculateScore(Queue<int> deck) => 87 87 deck.Reverse().Zip(Enumerable.Range(1, deck.Count), (a, b) => a * b).Sum(); 88 88 89 89 public override object Part1()
+1 -1
Solutions/2020/Day24.cs
··· 12 12 { "se", (0, 1, -1) }, 13 13 { "sw", (-1, 1, 0) }, 14 14 { "nw", (0, -1, 1) }, 15 - { "ne", (1, -1, 0) } 15 + { "ne", (1, -1, 0) }, 16 16 }; 17 17 18 18 private Dictionary<(int q, int r, int s), Tile>? _tiles;
+1 -1
Solutions/2021/Day04.cs
··· 75 75 76 76 private int At(int x, int y) => x * _size + y; 77 77 78 - private bool HasWin(HashSet<int> c, IReadOnlyList<int> b) 78 + private bool HasWin(HashSet<int> c, List<int> b) 79 79 { 80 80 for (var y = 0; y < _size; y++) 81 81 {
+1 -1
Solutions/2021/Day08.cs
··· 22 22 "1010010" => 7, 23 23 "1111111" => 8, 24 24 "1111011" => 9, 25 - _ => -1 25 + _ => -1, 26 26 }; 27 27 28 28 private static int Decode(string line)
+3 -3
Solutions/2021/Day10.cs
··· 10 10 {'(', ')'}, 11 11 {'[', ']'}, 12 12 {'{', '}'}, 13 - {'<', '>'} 13 + {'<', '>'}, 14 14 }; 15 15 16 16 private static readonly Dictionary<char, long> Scores = new() ··· 18 18 { ')', 3 }, 19 19 { ']', 57 }, 20 20 { '}', 1197 }, 21 - { '>', 25137 } 21 + { '>', 25137 }, 22 22 }; 23 23 24 24 private static readonly Dictionary<char, long> ScoresPart2 = new() ··· 26 26 { '(', 1 }, 27 27 { '[', 2 }, 28 28 { '{', 3 }, 29 - { '<', 4 } 29 + { '<', 4 }, 30 30 }; 31 31 32 32 private readonly List<long> _scores2 = [];
+18 -17
Solutions/2021/Day11.cs
··· 18 18 19 19 // increment all octopuses 20 20 for (var row = 0; row < _octopusField.Length; row++) 21 - for (var col = 0; col < _octopusField[row].Length; col++) 22 - _octopusField[row][col]++; 21 + for (var col = 0; col < _octopusField[row].Length; col++) 22 + _octopusField[row][col]++; 23 23 24 24 // flash any that exceeded 10 25 25 for (var row = 0; row < _octopusField.Length; row++) 26 - for (var col = 0; col < _octopusField[row].Length; col++) 27 - if (_octopusField[row][col] == 10) 28 - FlashAt(row, col); 26 + for (var col = 0; col < _octopusField[row].Length; col++) 27 + if (_octopusField[row][col] == 10) 28 + FlashAt(row, col); 29 29 30 30 var done = true; 31 31 for (var row = 0; row < _octopusField.Length; row++) 32 - for (var col = 0; col < _octopusField[row].Length; col++) 33 - if (_octopusField[row][col] == -1) 34 - _octopusField[row][col] = 0; 35 - else 36 - done = false; 32 + for (var col = 0; col < _octopusField[row].Length; col++) 33 + if (_octopusField[row][col] == -1) 34 + _octopusField[row][col] = 0; 35 + else 36 + done = false; 37 37 38 38 if (_totalTurns == 100) _flashesAfter100 = _flashTally; 39 39 if (done) break; ··· 45 45 _flashTally++; 46 46 _octopusField![r][c] = -1; 47 47 foreach (var rr in new[] { -1, 0, 1 }.Select(dr => dr + r)) 48 - foreach (var cc in new[] { -1, 0, 1 }.Select(dc => dc + c)) 49 - if (0 <= rr && rr < _octopusField.Length && 0 <= cc && cc < _octopusField[0].Length && _octopusField[rr][cc] != -1) 50 - { 51 - _octopusField[rr][cc]++; 52 - if (_octopusField[rr][cc] >= 10) 53 - FlashAt(rr, cc); 54 - } 48 + foreach (var cc in new[] { -1, 0, 1 }.Select(dc => dc + c)) 49 + if (0 <= rr && rr < _octopusField.Length && 0 <= cc && cc < _octopusField[0].Length && 50 + _octopusField[rr][cc] != -1) 51 + { 52 + _octopusField[rr][cc]++; 53 + if (_octopusField[rr][cc] >= 10) 54 + FlashAt(rr, cc); 55 + } 55 56 } 56 57 57 58 public override object Part1() => _flashesAfter100;
+1 -1
Solutions/2021/Day15.cs
··· 51 51 } 52 52 } 53 53 54 - private static int DijkstraCost(IReadOnlyDictionary<(int x, int y), Node> grid, Node target) 54 + private static int DijkstraCost(Dictionary<(int x, int y), Node> grid, Node target) 55 55 { 56 56 var searchQueue = new PriorityQueue<Node, int>(); 57 57 grid[(0, 0)].Distance = 0;
+1 -1
Solutions/2021/Day16.cs
··· 77 77 5 => Packets[0].Eval > Packets[1].Eval ? 1 : 0, 78 78 6 => Packets[0].Eval < Packets[1].Eval ? 1 : 0, 79 79 7 => Packets[0].Eval == Packets[1].Eval ? 1 : 0, 80 - _ => throw new ArgumentException("invalid packet type", nameof(TypeId)) 80 + _ => throw new ArgumentException("invalid packet type", nameof(TypeId)), 81 81 }; 82 82 } 83 83
+2 -2
Solutions/2021/Day19.cs
··· 38 38 (-1, 0, 0) => (pt.Y, -pt.X, pt.Z), 39 39 (0, 0, 1) => (pt.Y, pt.Z, pt.X), 40 40 (0, 0, -1) => (pt.Y, -pt.Z, -pt.X), 41 - _ => throw new("Invalid up vector") 41 + _ => throw new("Invalid up vector"), 42 42 }; 43 43 44 44 return rotation switch ··· 47 47 1 => (reoriented.Z, reoriented.Y, -reoriented.X), 48 48 2 => (-reoriented.X, reoriented.Y, -reoriented.Z), 49 49 3 => (-reoriented.Z, reoriented.Y, reoriented.X), 50 - _ => throw new("Invalid rotation") 50 + _ => throw new("Invalid rotation"), 51 51 }; 52 52 } 53 53
+2 -2
Solutions/2021/Day23.cs
··· 72 72 return possible; 73 73 }, 74 74 Distance = tuple => tuple.distance, 75 - Cell = (_, tuple) => tuple.state 75 + Cell = (_, tuple) => tuple.state, 76 76 }; 77 77 78 78 public override object Part1() ··· 108 108 1 => B, 109 109 2 => C, 110 110 3 => D, 111 - _ => null 111 + _ => null, 112 112 }; 113 113 114 114 public static State New(State from, char[] hall, int i, string hole) =>
+11 -11
Solutions/2021/Day25.cs
··· 10 10 public override void ProcessInput() => 11 11 _cucumbers = Input.Select(l => l.ToCharArray()).ToArray(); 12 12 13 - private static char[][]? DoStep(IReadOnlyList<char[]> arr) 13 + private static char[][]? DoStep(char[][] arr) 14 14 { 15 15 var moved = false; 16 - var h = arr.Count; 16 + var h = arr.Length; 17 17 var w = arr[0].Length; 18 18 var result = new char[h][]; 19 19 for (var i = 0; i < h; i++) ··· 36 36 result2[i] = new char[w]; 37 37 38 38 for (var i = 0; i < h; i++) 39 - for (var j = 0; j < w; j++) 40 - { 41 - if (result2[i][j] == 0) result2[i][j] = result[i][j]; 42 - if (result[i][j] != 'v') continue; 43 - if (result[(i + 1) % h][j] != '.') continue; 44 - result2[i][j] = '.'; 45 - result2[(i + 1) % h][j] = 'v'; 46 - moved = true; 47 - } 39 + for (var j = 0; j < w; j++) 40 + { 41 + if (result2[i][j] == 0) result2[i][j] = result[i][j]; 42 + if (result[i][j] != 'v') continue; 43 + if (result[(i + 1) % h][j] != '.') continue; 44 + result2[i][j] = '.'; 45 + result2[(i + 1) % h][j] = 'v'; 46 + moved = true; 47 + } 48 48 49 49 return moved ? result2 : null; 50 50 }
+1 -1
Solutions/2022/Day11.cs
··· 75 75 { 76 76 '*' => i => i * amount, 77 77 '+' => i => i + amount, 78 - _ => throw new ArgumentOutOfRangeException(line, "invalid operation") 78 + _ => throw new ArgumentOutOfRangeException(line, "invalid operation"), 79 79 }; 80 80 else m.Operation = i => i * i; 81 81 }
+5 -4
Solutions/2022/Day15.cs
··· 18 18 public override object Part1() 19 19 { 20 20 var targetRow = UseTestInput ? 10 : 2_000_000; 21 - 21 + 22 22 var taken = _sensors! 23 23 .Where(t => t.ClosestBeaconPosition.y == targetRow) 24 24 .Select(t => t.ClosestBeaconPosition.x); ··· 33 33 { 34 34 var size = UseTestInput ? 20 : 4_000_000; 35 35 var limit = new SensorRange(0, size); 36 - 36 + 37 37 for (var y = 0; y <= size; y++) 38 38 { 39 - var covered = _sensors!.Select(s => s.GetSlice(y)); 39 + var y1 = y; 40 + var covered = _sensors!.Select(s => s.GetSlice(y1)); 40 41 var gap = FindGap(covered, limit); 41 42 if (gap is { } x) 42 43 return (x * 4_000_000L) + y; 43 44 } 44 - 45 + 45 46 return 0; 46 47 } 47 48
+10 -9
Solutions/2023/Day07.cs
··· 3 3 public class Day07() : Day(2023, 7, "Camel Cards") 4 4 { 5 5 private List<(Hand hand, long bid)> _hands = []; 6 - public const string OrderedCards = "23456789TJQKA"; 6 + private const string OrderedCards = "23456789TJQKA"; 7 7 8 8 public override void ProcessInput() => 9 9 _hands = Input ··· 22 22 23 23 public override object Part2() => 24 24 _hands 25 - .OrderBy(d => d.hand, new HandComparer(rank => rank == OrderedCards.IndexOf('J') ? 0 : rank + 1, useWilds: true)) 25 + .OrderBy(d => d.hand, 26 + new HandComparer(rank => rank == OrderedCards.IndexOf('J') ? 0 : rank + 1, useWilds: true)) 26 27 .Select((hand, i) => hand.bid * (i + 1)) 27 28 .Sum(); 28 29 29 - public class HandComparer(Func<int, int> rankFunc, bool useWilds = false) : IComparer<Hand> 30 + private class HandComparer(Func<int, int> rankFunc, bool useWilds = false) : IComparer<Hand> 30 31 { 31 32 public int Compare(Hand? x, Hand? y) 32 33 { ··· 48 49 [Flags] 49 50 public enum HandType 50 51 { 51 - HighCard, OnePair, TwoPair, ThreeOfAKind, FullHouse, FourOfAKind, FiveOfAKind 52 + HighCard, OnePair, TwoPair, ThreeOfAKind, FullHouse, FourOfAKind, FiveOfAKind, 52 53 } 53 54 54 55 public class Hand(string value) ··· 86 87 { 87 88 2 => HandType.FiveOfAKind, 88 89 1 => HandType.FourOfAKind, 89 - _ => pairs > 0 ? HandType.FullHouse : HandType.ThreeOfAKind 90 + _ => pairs > 0 ? HandType.FullHouse : HandType.ThreeOfAKind, 90 91 }, 91 92 2 => pairs switch 92 93 { ··· 95 96 { 96 97 1 => HandType.ThreeOfAKind, 97 98 2 => HandType.FourOfAKind, 98 - _ => wildCount > 2 ? HandType.FiveOfAKind : HandType.OnePair 99 + _ => wildCount > 2 ? HandType.FiveOfAKind : HandType.OnePair, 99 100 }, 100 - _ => HandType.HighCard 101 + _ => HandType.HighCard, 101 102 }, 102 103 _ => wildCount switch 103 104 { ··· 106 107 3 => HandType.FourOfAKind, 107 108 4 => HandType.FiveOfAKind, 108 109 5 => HandType.FiveOfAKind, 109 - _ => HandType.HighCard 110 - } 110 + _ => HandType.HighCard, 111 + }, 111 112 }; 112 113 } 113 114 }
+1 -1
Solutions/Extensions.cs
··· 116 116 { 117 117 var array = list as T[] ?? list.ToArray(); 118 118 return array.Length == 1 119 - ? new[] { array } 119 + ? [array] 120 120 : array.SelectMany(t => Permute(array.Where(x => !x!.Equals(t))), (v, p) => p.Prepend(v)); 121 121 } 122 122
+2 -1
Tests/Test2024.cs
··· 1 1 using Solutions._2024; 2 + 2 3 namespace Tests; 3 4 4 5 [TestClass] ··· 11 12 [DataRow(typeof(Day04), "2573", "1850")] 12 13 public void CheckAllDays(Type dayType, string part1, string part2) => 13 14 Common.CheckDay(dayType, part1, part2); 14 - 15 + 15 16 [DataTestMethod] 16 17 [DataRow(typeof(Day01), "11", "31")] 17 18 [DataRow(typeof(Day02), "2", "4")]