Automatic code cleanup

This commit is contained in:
2023-12-02 12:39:10 -05:00
parent 70de8d15b1
commit 26d876e178
36 changed files with 132 additions and 293 deletions

View File

@@ -1,13 +1,9 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2015.Day10; namespace AdventOfCode.Problems.AOC2015.Day10;
[ProblemInfo(2015, 10, "Evles Look, Elves Say")] [ProblemInfo(2015, 10, "Evles Look, Elves Say")]
internal class LookAndSay : Problem<int, int> internal class LookAndSay : Problem<int, int>
{ {
@@ -59,4 +55,4 @@ internal class LookAndSay : Problem<int, int>
input = next; input = next;
} }
} }

View File

@@ -31,6 +31,7 @@ public class NiceList : Problem<int, int>
} }
} }
} }
private static bool IsNice2(string value) private static bool IsNice2(string value)
{ {
var pairs = new Dictionary<string, List<int>>(); var pairs = new Dictionary<string, List<int>>();

View File

@@ -1,11 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace AdventOfCode.Problems.AOC2019.Day1 namespace AdventOfCode.Problems.AOC2019.Day1
{ {
[ProblemInfo(2019, 1, "The Tyranny of the Rocket Equation")] [ProblemInfo(2019, 1, "The Tyranny of the Rocket Equation")]
@@ -27,7 +21,7 @@ namespace AdventOfCode.Problems.AOC2019.Day1
return curCost + GetFuelCost(curCost); return curCost + GetFuelCost(curCost);
} }
public static int GetCost(int mass) => mass/ 3 - 2; public static int GetCost(int mass) => mass / 3 - 2;
public override void LoadInput() public override void LoadInput()
{ {
@@ -41,8 +35,7 @@ namespace AdventOfCode.Problems.AOC2019.Day1
public override void CalculatePart2() public override void CalculatePart2()
{ {
Part2 = GetFuelRequirement(_input); Part2 = GetFuelRequirement(_input);
} }
} }
} }

View File

@@ -1,13 +1,6 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
namespace AdventOfCode.Problems.AOC2019.Day3 namespace AdventOfCode.Problems.AOC2019.Day3
{ {
@@ -157,7 +150,7 @@ namespace AdventOfCode.Problems.AOC2019.Day3
return false; return false;
} }
} }
else //Both else //Both
{ {
if (min.X != other.min.X) if (min.X != other.min.X)
{ {
@@ -227,7 +220,6 @@ namespace AdventOfCode.Problems.AOC2019.Day3
public static int SolveWires(string[] wires) public static int SolveWires(string[] wires)
{ {
var (wireSegmentsA, wireSegmentsB) = CreateWirePair(wires); var (wireSegmentsA, wireSegmentsB) = CreateWirePair(wires);
int shortestWire = int.MaxValue; int shortestWire = int.MaxValue;
@@ -271,7 +263,6 @@ namespace AdventOfCode.Problems.AOC2019.Day3
var wireA = wires[0].Split(','); var wireA = wires[0].Split(',');
var wireSegmentsA = CreateWire(wireA); var wireSegmentsA = CreateWire(wireA);
var wireB = wires[1].Split(','); var wireB = wires[1].Split(',');
var wireSegmentsB = CreateWire(wireB); var wireSegmentsB = CreateWire(wireB);
@@ -325,4 +316,4 @@ namespace AdventOfCode.Problems.AOC2019.Day3
Part2 = SolveWires(_inputLines); Part2 = SolveWires(_inputLines);
} }
} }
} }

View File

@@ -1,11 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace AdventOfCode.Problems.AOC2019.Day4 namespace AdventOfCode.Problems.AOC2019.Day4
{ {
[ProblemInfo(2019, 4, "Secure Container")] [ProblemInfo(2019, 4, "Secure Container")]
@@ -30,7 +24,6 @@ namespace AdventOfCode.Problems.AOC2019.Day4
public static bool HasDoubles(int[] password) public static bool HasDoubles(int[] password)
{ {
bool foundDouble = false; bool foundDouble = false;
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
@@ -53,7 +46,6 @@ namespace AdventOfCode.Problems.AOC2019.Day4
foundDouble = true; foundDouble = true;
} }
return foundDouble; return foundDouble;
} }
public static bool IsAssending(int[] password) public static bool IsAssending(int[] password)
@@ -68,8 +60,6 @@ namespace AdventOfCode.Problems.AOC2019.Day4
return true; return true;
} }
public static int CountPasswordsPart1(int lower, int upper) public static int CountPasswordsPart1(int lower, int upper)
{ {
int passwordCount = 0; int passwordCount = 0;
@@ -87,6 +77,7 @@ namespace AdventOfCode.Problems.AOC2019.Day4
} }
return passwordCount; return passwordCount;
} }
public static int CountPasswordsPart2(int lower, int upper) public static int CountPasswordsPart2(int lower, int upper)
{ {
int passwordCount = 0; int passwordCount = 0;
@@ -139,7 +130,6 @@ namespace AdventOfCode.Problems.AOC2019.Day4
} }
} }
public override void LoadInput() public override void LoadInput()
{ {
} }
@@ -154,4 +144,4 @@ namespace AdventOfCode.Problems.AOC2019.Day4
Part2 = CountPasswordsPart2(147981, 691423); Part2 = CountPasswordsPart2(147981, 691423);
} }
} }
} }

View File

@@ -1,14 +1,8 @@
using AdventOfCode.Day_5; using AdventOfCode.Day_5;
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2019.Day5; namespace AdventOfCode.Problems.AOC2019.Day5;
[ProblemInfo(2019, 5, "Sunny with a Chance of Asteroids")] [ProblemInfo(2019, 5, "Sunny with a Chance of Asteroids")]
internal class ChanceOfAsteroids : Problem<int, int> internal class ChanceOfAsteroids : Problem<int, int>
{ {
@@ -33,4 +27,4 @@ internal class ChanceOfAsteroids : Problem<int, int>
{ {
_baseInput = InputParsing.ParseIntCsv(GetInputFile("input.csv")); _baseInput = InputParsing.ParseIntCsv(GetInputFile("input.csv"));
} }
} }

View File

@@ -1,9 +1,4 @@
using System; namespace AdventOfCode.Day_5
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace AdventOfCode.Day_5
{ {
public class IntCodeV2 public class IntCodeV2
{ {
@@ -19,7 +14,6 @@ namespace AdventOfCode.Day_5
this.paramCount = paramCount; this.paramCount = paramCount;
this.action = action; this.action = action;
} }
} }
public bool IsHalted { get; private set; } public bool IsHalted { get; private set; }
@@ -167,10 +161,11 @@ namespace AdventOfCode.Day_5
{ {
var opModes = new int[3]; var opModes = new int[3];
var arr = instruction.ToIntArray(); var arr = instruction.ToIntArray();
switch(arr.Length) switch (arr.Length)
{ {
case 1: case 1:
return (opModes, arr[0]); return (opModes, arr[0]);
case 2: case 2:
return (opModes, (arr[^2] * 10) + arr[^1]); return (opModes, (arr[^2] * 10) + arr[^1]);
} }
@@ -234,7 +229,5 @@ namespace AdventOfCode.Day_5
_instructionPointer = 0; _instructionPointer = 0;
return this; return this;
} }
} }
} }

View File

@@ -1,12 +1,7 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2019.Day6; namespace AdventOfCode.Problems.AOC2019.Day6;
[ProblemInfo(2019, 6, "Universal Orbit Map")] [ProblemInfo(2019, 6, "Universal Orbit Map")]
internal class UniversalOrbits : Problem<int, int> internal class UniversalOrbits : Problem<int, int>
{ {
@@ -14,7 +9,7 @@ internal class UniversalOrbits : Problem<int, int>
public override void CalculatePart1() public override void CalculatePart1()
{ {
if(_map == null) if (_map == null)
return; return;
Part1 = _map.CalculateOrbits(); Part1 = _map.CalculateOrbits();
} }
@@ -31,7 +26,6 @@ internal class UniversalOrbits : Problem<int, int>
HashSet<string> pathYOU = new(pathToYOU.Select(o => o.Name)); HashSet<string> pathYOU = new(pathToYOU.Select(o => o.Name));
for (int i = 0; i < pathToSAN.Count; i++) for (int i = 0; i < pathToSAN.Count; i++)
{ {
if (pathYOU.Contains(pathToSAN[i].Name)) if (pathYOU.Contains(pathToSAN[i].Name))
@@ -50,11 +44,10 @@ internal class UniversalOrbits : Problem<int, int>
} }
} }
Part2 = dist - 2; Part2 = dist - 2;
} }
public override void LoadInput() public override void LoadInput()
{ {
_map = new OrbitMap(ReadInputLines()); _map = new OrbitMap(ReadInputLines());
} }
} }

View File

@@ -1,11 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace AdventOfCode.Problems.AOC2019.Day8 namespace AdventOfCode.Problems.AOC2019.Day8
{ {
[ProblemInfo(2019, 8, "Space Image Format")] [ProblemInfo(2019, 8, "Space Image Format")]
@@ -18,7 +12,6 @@ namespace AdventOfCode.Problems.AOC2019.Day8
var imageData = File.ReadAllText("Day8/input.txt").Replace("\n", "").Select(c => int.Parse(c.ToString())).ToArray(); var imageData = File.ReadAllText("Day8/input.txt").Replace("\n", "").Select(c => int.Parse(c.ToString())).ToArray();
Console.WriteLine(Checksum(imageData, 25, 6)); Console.WriteLine(Checksum(imageData, 25, 6));
} }
public static void RenderImage(int[] data, int h, int w) public static void RenderImage(int[] data, int h, int w)
@@ -28,11 +21,9 @@ namespace AdventOfCode.Problems.AOC2019.Day8
for (int l = 0; l < layerCount; l++) for (int l = 0; l < layerCount; l++)
{ {
} }
} }
public static int Checksum(int[] data, int h, int w) public static int Checksum(int[] data, int h, int w)
{ {
var imgSize = h * w; var imgSize = h * w;
@@ -54,9 +45,11 @@ namespace AdventOfCode.Problems.AOC2019.Day8
case 0: case 0:
zeroCount[l]++; zeroCount[l]++;
break; break;
case 1: case 1:
oneCount[l]++; oneCount[l]++;
break; break;
case 2: case 2:
twoCount[l]++; twoCount[l]++;
break; break;
@@ -70,7 +63,6 @@ namespace AdventOfCode.Problems.AOC2019.Day8
} }
return oneCount[smallestLayer] * twoCount[smallestLayer]; return oneCount[smallestLayer] * twoCount[smallestLayer];
} }
public override void LoadInput() public override void LoadInput()
@@ -88,4 +80,4 @@ namespace AdventOfCode.Problems.AOC2019.Day8
Part2 = null; Part2 = null;
} }
} }
} }

View File

@@ -1,10 +1,4 @@
using System; namespace AdventOfCode
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace AdventOfCode
{ {
public static class InputParsing public static class InputParsing
{ {
@@ -34,4 +28,4 @@ namespace AdventOfCode
return intArr; return intArr;
} }
} }
} }

View File

@@ -1,9 +1,6 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System.Collections; using System.Collections;
using System.Numerics;
using System.Text;
namespace AdventOfCode.Problems.AOC2021.Day11; namespace AdventOfCode.Problems.AOC2021.Day11;
@@ -76,7 +73,6 @@ public class DumboOctopus : Problem<int, int>, IEnumerable<byte>
{ {
Increment(ref data); Increment(ref data);
Flash(ref data, ref flashes); Flash(ref data, ref flashes);
} }
private static void Increment(ref byte[] data) private static void Increment(ref byte[] data)

View File

@@ -1,12 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2021.Day3; namespace AdventOfCode.Problems.AOC2021.Day3;
[ProblemInfo(2021, 3, "Binary Diagnostic")] [ProblemInfo(2021, 3, "Binary Diagnostic")]
@@ -75,4 +68,4 @@ public class BinaryDiagnostic : Problem<int, int>
Part2 = oxygenLevel * carbonLevel; Part2 = oxygenLevel * carbonLevel;
} }
} }

View File

@@ -1,11 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2021.Day6; namespace AdventOfCode.Problems.AOC2021.Day6;
[ProblemInfo(2021, 6, "Lanternfish")] [ProblemInfo(2021, 6, "Lanternfish")]
@@ -18,11 +12,11 @@ internal class LanternFish : Problem<long, long>
var input = ReadInputLines(); var input = ReadInputLines();
Data = input.First().Split(",").Select(v => int.Parse(v)).ToArray(); Data = input.First().Split(",").Select(v => int.Parse(v)).ToArray();
} }
public override void CalculatePart1() public override void CalculatePart1()
{ {
var lifetimes = PrepareLifetimes(); var lifetimes = PrepareLifetimes();
Part1 = Simulate(lifetimes, 80); Part1 = Simulate(lifetimes, 80);
} }
public override void CalculatePart2() public override void CalculatePart2()
@@ -53,8 +47,4 @@ internal class LanternFish : Problem<long, long>
return lifetimes.Sum(); return lifetimes.Sum();
} }
}
}

View File

@@ -1,7 +1,7 @@
using AdventOfCode.Runner; using AdventOfCode.Runner.Attributes;
using AdventOfCode.Runner.Attributes;
namespace AdventOfCode.Problems.AOC2022.Day0; namespace AdventOfCode.Problems.AOC2022.Day0;
[ProblemInfo(2022, 0, "Fancy Test")] [ProblemInfo(2022, 0, "Fancy Test")]
public class TestProblem : Problem public class TestProblem : Problem
{ {
@@ -20,7 +20,6 @@ public class TestProblem : Problem
Thread.Sleep(1000); Thread.Sleep(1000);
} }
public override void PrintPart1() public override void PrintPart1()
{ {
Console.WriteLine("Result"); Console.WriteLine("Result");

View File

@@ -1,13 +1,7 @@
using AdventOfCode.Runner; using AdventOfCode.Runner.Attributes;
using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day1; namespace AdventOfCode.Problems.AOC2022.Day1;
[ProblemInfo(2022, 1, "Calorie Counting")] [ProblemInfo(2022, 1, "Calorie Counting")]
internal class CalorieCounting : Problem internal class CalorieCounting : Problem
{ {
@@ -16,7 +10,8 @@ internal class CalorieCounting : Problem
private (int calories, int elf)? _mostestElf; private (int calories, int elf)? _mostestElf;
private IEnumerable<(int sum, int idx)>? _mostestElves; private IEnumerable<(int sum, int idx)>? _mostestElves;
public CalorieCounting() { public CalorieCounting()
{
FlaresFood = new List<List<int>> FlaresFood = new List<List<int>>
{ {
new List<int>() new List<int>()
@@ -24,12 +19,12 @@ internal class CalorieCounting : Problem
} }
public override void LoadInput() public override void LoadInput()
{ {
var lines = File.ReadAllLines(GetInputFile("input.txt")); var lines = File.ReadAllLines(GetInputFile("input.txt"));
var c = 0; var c = 0;
foreach (var calorie in lines) foreach (var calorie in lines)
{ {
if(string.IsNullOrWhiteSpace(calorie)) if (string.IsNullOrWhiteSpace(calorie))
{ {
FlaresFood.Add(new List<int>()); FlaresFood.Add(new List<int>());
c++; c++;
@@ -37,22 +32,23 @@ internal class CalorieCounting : Problem
} }
FlaresFood[c].Add(int.Parse(calorie)); FlaresFood[c].Add(int.Parse(calorie));
} }
} }
public override void CalculatePart1()
{ public override void CalculatePart1()
_mostestElf = FlaresFood {
_mostestElf = FlaresFood
.Select((x, idx) => (sum: x.Sum(), idx)) .Select((x, idx) => (sum: x.Sum(), idx))
.MaxBy(x => x.sum); .MaxBy(x => x.sum);
Part1 = _mostestElf.Value.ToString(); Part1 = _mostestElf.Value.ToString();
} }
public override void CalculatePart2() public override void CalculatePart2()
{ {
_mostestElves = FlaresFood _mostestElves = FlaresFood
.Select((x, idx) => (sum: x.Sum(), idx)) .Select((x, idx) => (sum: x.Sum(), idx))
.OrderByDescending(e => e.sum) .OrderByDescending(e => e.sum)
.Take(3); .Take(3);
Part2 = _mostestElves.Sum(e => e.sum).ToString(); Part2 = _mostestElves.Sum(e => e.sum).ToString();
} }
} }

View File

@@ -1,10 +1,5 @@
using System; namespace AdventOfCode.Problems.AOC2022.Day10;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day10;
internal class CathodeCPU internal class CathodeCPU
{ {
public enum Instruction public enum Instruction
@@ -38,9 +33,9 @@ internal class CathodeCPU
return result; return result;
} }
public void ExecuteCode((Instruction ins, int value)[] code, Action<int, int> processor) public void ExecuteCode((Instruction ins, int value)[] code, Action<int, int> processor)
{ {
while (_programCounter < code.Length) while (_programCounter < code.Length)
{ {
var (ins, value) = code[_programCounter]; var (ins, value) = code[_programCounter];
@@ -52,23 +47,20 @@ internal class CathodeCPU
case { ins: Instruction.NoOp }: case { ins: Instruction.NoOp }:
_programCounter++; _programCounter++;
break; break;
case { ins: Instruction.AddX, _pending : -1 }:
case { ins: Instruction.AddX, _pending: -1 }:
_pending = 1; _pending = 1;
break; break;
case { ins: Instruction.AddX, _pending: 0 }: case { ins: Instruction.AddX, _pending: 0 }:
X += value; X += value;
_programCounter++; _programCounter++;
break; break;
} }
if (_pending >= 0)
if(_pending >= 0)
_pending--; _pending--;
_cycleNumber++; _cycleNumber++;
} }
} }
}
}

View File

@@ -25,11 +25,10 @@ internal class CathodeRayTube : Problem<int, string>
if (cycle > output.Length) if (cycle > output.Length)
return; return;
var pos = signal % 40; var pos = signal % 40;
var head = (cycle % 40); var head = (cycle % 40);
var sprite = Math.Abs(pos - head); var sprite = Math.Abs(pos - head);
if(sprite <= 1) if (sprite <= 1)
output[cycle] = '█'; output[cycle] = '█';
}); });

View File

@@ -38,7 +38,7 @@ internal class Monkey
{ {
InspectionCount++; InspectionCount++;
value = Operate(value); value = Operate(value);
if(worryOffset != 0) if (worryOffset != 0)
value /= worryOffset; value /= worryOffset;
return value; return value;
} }
@@ -68,6 +68,6 @@ internal class Monkey
public override string ToString() public override string ToString()
{ {
return $"{MonkeyNumber}: ({InspectionCount}) [{string.Join(", ",Items)}]"; return $"{MonkeyNumber}: ({InspectionCount}) [{string.Join(", ", Items)}]";
} }
} }

View File

@@ -1,13 +1,7 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day11; namespace AdventOfCode.Problems.AOC2022.Day11;
[ProblemInfo(2022, 11, "Monkey in the Middle")] [ProblemInfo(2022, 11, "Monkey in the Middle")]
internal class MonkeyInTheMiddle : Problem<int, int> internal class MonkeyInTheMiddle : Problem<int, int>
{ {
@@ -37,7 +31,6 @@ internal class MonkeyInTheMiddle : Problem<int, int>
var lines = ReadInputLines("test.txt").Chunk(7); var lines = ReadInputLines("test.txt").Chunk(7);
_monkeysPart1 = lines.Select(ln => new Monkey(ln)).ToArray(); _monkeysPart1 = lines.Select(ln => new Monkey(ln)).ToArray();
_monkeysPart2 = lines.Select(ln => new Monkey(ln)).ToArray(); _monkeysPart2 = lines.Select(ln => new Monkey(ln)).ToArray();
} }
private static void Simulate(Monkey[] monkeys, int rounds, uint worry = 3) private static void Simulate(Monkey[] monkeys, int rounds, uint worry = 3)
@@ -54,8 +47,8 @@ internal class MonkeyInTheMiddle : Problem<int, int>
SimulateTurn(monkey, monkeys, worry); SimulateTurn(monkey, monkeys, worry);
} }
private static void SimulateTurn(Monkey monkey, Monkey[] monkeys, uint worry = 3)
private static void SimulateTurn(Monkey monkey, Monkey[] monkeys, uint worry = 3) { {
for (int i = 0; i < monkey.Items.Count; i++) for (int i = 0; i < monkey.Items.Count; i++)
{ {
var item = monkey.Inspect(monkey.Items[i], worry); var item = monkey.Inspect(monkey.Items[i], worry);
@@ -64,4 +57,4 @@ internal class MonkeyInTheMiddle : Problem<int, int>
} }
monkey.Items.Clear(); monkey.Items.Clear();
} }
} }

View File

@@ -1,12 +1,7 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day12; namespace AdventOfCode.Problems.AOC2022.Day12;
[ProblemInfo(2022, 12, "Hill Climbing Algorithm")] [ProblemInfo(2022, 12, "Hill Climbing Algorithm")]
internal class HillClimbing : Problem internal class HillClimbing : Problem
{ {
@@ -24,4 +19,4 @@ internal class HillClimbing : Problem
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }

View File

@@ -1,12 +1,7 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day13; namespace AdventOfCode.Problems.AOC2022.Day13;
[ProblemInfo(2022, 13, "Distress Signal")] [ProblemInfo(2022, 13, "Distress Signal")]
internal class DistressSignal : Problem internal class DistressSignal : Problem
{ {
@@ -24,4 +19,4 @@ internal class DistressSignal : Problem
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }

View File

@@ -1,12 +1,7 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day14; namespace AdventOfCode.Problems.AOC2022.Day14;
[ProblemInfo(2022, 14, "Regolith Reservoir")] [ProblemInfo(2022, 14, "Regolith Reservoir")]
internal class RegolithReservoir : Problem internal class RegolithReservoir : Problem
{ {
@@ -24,4 +19,4 @@ internal class RegolithReservoir : Problem
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }

View File

@@ -1,5 +1,4 @@
using AdventOfCode.Runner; using AdventOfCode.Runner.Attributes;
using AdventOfCode.Runner.Attributes;
namespace AdventOfCode.Problems.AOC2022.Day2; namespace AdventOfCode.Problems.AOC2022.Day2;
@@ -74,7 +73,8 @@ internal class RockPaperScissors : Problem
}; };
} }
private static int GetResponseValue(char move, char result) { private static int GetResponseValue(char move, char result)
{
var p = result switch var p = result switch
{ {
'X' => (GetMoveValue(move) + 2) % 3, //Lose 'X' => (GetMoveValue(move) + 2) % 3, //Lose
@@ -85,7 +85,6 @@ internal class RockPaperScissors : Problem
return p == 0 ? 3 : p; return p == 0 ? 3 : p;
} }
public override void CalculatePart2() public override void CalculatePart2()
{ {
var score = 0; var score = 0;

View File

@@ -1,19 +1,14 @@
using AdventOfCode.Runner; using AdventOfCode.Runner.Attributes;
using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day3; namespace AdventOfCode.Problems.AOC2022.Day3;
[ProblemInfo(2022, 3, "Rucksack Reorganization")] [ProblemInfo(2022, 3, "Rucksack Reorganization")]
internal class RucksackReorganization : Problem internal class RucksackReorganization : Problem
{ {
private string[] _sacks; private string[] _sacks;
public RucksackReorganization() { public RucksackReorganization()
{
_sacks = Array.Empty<string>(); _sacks = Array.Empty<string>();
} }
@@ -21,6 +16,7 @@ internal class RucksackReorganization : Problem
{ {
_sacks = ReadInputLines("input.txt"); _sacks = ReadInputLines("input.txt");
} }
public override void CalculatePart1() public override void CalculatePart1()
{ {
var total = 0; var total = 0;
@@ -55,5 +51,4 @@ internal class RucksackReorganization : Problem
} }
Part2 = total.ToString(); Part2 = total.ToString();
} }
}
}

View File

@@ -1,13 +1,7 @@
using AdventOfCode.Runner; using AdventOfCode.Runner.Attributes;
using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day4; namespace AdventOfCode.Problems.AOC2022.Day4;
[ProblemInfo(2022, 4, "Camp Cleanup")] [ProblemInfo(2022, 4, "Camp Cleanup")]
internal class CampCleanup : Problem<int, int> internal class CampCleanup : Problem<int, int>
{ {
@@ -19,7 +13,7 @@ internal class CampCleanup : Problem<int, int>
foreach (var line in lines) foreach (var line in lines)
{ {
var (a, b) = line.Split(',') var (a, b) = line.Split(',')
.Select(range => .Select(range =>
range.Split('-') range.Split('-')
.Select(v => int.Parse(v)) .Select(v => int.Parse(v))
.Chunk(2) .Chunk(2)
@@ -32,7 +26,6 @@ internal class CampCleanup : Problem<int, int>
} }
} }
public override void CalculatePart1() public override void CalculatePart1()
{ {
var total = 0; var total = 0;
@@ -50,10 +43,9 @@ internal class CampCleanup : Problem<int, int>
{ {
if (a.OverlapsWith(b)) if (a.OverlapsWith(b))
Part2++; Part2++;
} }
} }
record Range(int A, int B) record Range(int A, int B)
{ {
public bool Contains(Range other) public bool Contains(Range other)
@@ -66,6 +58,6 @@ internal class CampCleanup : Problem<int, int>
return (B >= other.A && A <= other.A) || (A <= other.B && B >= other.B) || (A >= other.A && B <= other.B); return (B >= other.A && A <= other.A) || (A <= other.B && B >= other.B) || (A >= other.A && B <= other.B);
} }
public static implicit operator Range((int a, int b) value) => new (value.a, value.b); public static implicit operator Range((int a, int b) value) => new(value.a, value.b);
} }
} }

View File

@@ -3,7 +3,6 @@
using Superpower; using Superpower;
using Superpower.Parsers; using Superpower.Parsers;
using System.Diagnostics;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace AdventOfCode.Problems.AOC2022.Day5; namespace AdventOfCode.Problems.AOC2022.Day5;
@@ -13,15 +12,16 @@ internal partial class SupplyStacks : Problem
{ {
private List<char>[] _stacksPart1 = Array.Empty<List<char>>(); private List<char>[] _stacksPart1 = Array.Empty<List<char>>();
private List<char>[] _stacksPart2 = Array.Empty<List<char>>(); private List<char>[] _stacksPart2 = Array.Empty<List<char>>();
private readonly TextParser<(int, int, int)> _moveParser = from move in Character.Letter.Or(Character.WhiteSpace).Many()
from stack in Character.Digit.Many()
from frm in Character.Letter.Or(Character.WhiteSpace).Many()
from source in Character.Digit.Many()
from to in Character.Letter.Or(Character.WhiteSpace).Many()
from dst in Character.Digit.Many()
select (int.Parse(stack), int.Parse(source), int.Parse(dst));
private List<(int stack, int from, int to)> _moves = new (); private readonly TextParser<(int, int, int)> _moveParser = from move in Character.Letter.Or(Character.WhiteSpace).Many()
from stack in Character.Digit.Many()
from frm in Character.Letter.Or(Character.WhiteSpace).Many()
from source in Character.Digit.Many()
from to in Character.Letter.Or(Character.WhiteSpace).Many()
from dst in Character.Digit.Many()
select (int.Parse(stack), int.Parse(source), int.Parse(dst));
private List<(int stack, int from, int to)> _moves = new();
public override void CalculatePart1() public override void CalculatePart1()
{ {
@@ -34,12 +34,12 @@ internal partial class SupplyStacks : Problem
private static void PerformBasicMove(List<char>[] data, (int stack, int from, int to) move) private static void PerformBasicMove(List<char>[] data, (int stack, int from, int to) move)
{ {
var from = data[move.from-1]; var from = data[move.from - 1];
var to = data[move.to-1]; var to = data[move.to - 1];
for (int i = 0; i < move.stack; i++) for (int i = 0; i < move.stack; i++)
{ {
var item = from[^1]; var item = from[^1];
from.RemoveAt(from.Count-1); from.RemoveAt(from.Count - 1);
to.Add(item); to.Add(item);
} }
} }

View File

@@ -1,11 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day6; namespace AdventOfCode.Problems.AOC2022.Day6;
[ProblemInfo(2022, 6, "Tuning Trouble")] [ProblemInfo(2022, 6, "Tuning Trouble")]
@@ -38,4 +32,4 @@ internal class TuningTrouble : Problem<int, int>
{ {
_input = ReadInputText(); _input = ReadInputText();
} }
} }

View File

@@ -1,8 +1,6 @@
using System.Collections; namespace AdventOfCode.Problems.AOC2022.Day7;
namespace AdventOfCode.Problems.AOC2022.Day7; internal class DirectoryNode
internal class DirectoryNode
{ {
public DirectoryNode Parent { get; set; } public DirectoryNode Parent { get; set; }
public string Name { get; set; } public string Name { get; set; }
@@ -40,7 +38,7 @@ internal class DirectoryNode
{ {
if (path == "/") if (path == "/")
return this; return this;
if(string.IsNullOrWhiteSpace(path)) if (string.IsNullOrWhiteSpace(path))
return this; return this;
var segments = path.Split("/").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); var segments = path.Split("/").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
if (segments.Length == 0) if (segments.Length == 0)
@@ -91,19 +89,19 @@ internal class DirectoryNode
var child = Children.FirstOrDefault(c => c.Name == segments[0]); var child = Children.FirstOrDefault(c => c.Name == segments[0]);
if(child == null) if (child == null)
return null; return null;
else else
if(segments.Length == 1) if (segments.Length == 1)
return child; return child;
else else
return child.GetDirectory(segments[1..]); return child.GetDirectory(segments[1..]);
} }
public List<DirectoryNode> Where(Func<DirectoryNode, bool> filter) public List<DirectoryNode> Where(Func<DirectoryNode, bool> filter)
{ {
var result = new List<DirectoryNode>(); var result = new List<DirectoryNode>();
if(filter(this)) if (filter(this))
result.Add(this); result.Add(this);
result.AddRange(Children.SelectMany(c => c.Where(filter))); result.AddRange(Children.SelectMany(c => c.Where(filter)));
return result; return result;

View File

@@ -3,7 +3,7 @@
namespace AdventOfCode.Problems.AOC2022.Day7; namespace AdventOfCode.Problems.AOC2022.Day7;
[ProblemInfo(2022, 7, "No Space Left On Device")] [ProblemInfo(2022, 7, "No Space Left On Device")]
internal class NoSpace : Problem<int,int> internal class NoSpace : Problem<int, int>
{ {
private DirectoryNode _dirTree = new DirectoryNode("/"); private DirectoryNode _dirTree = new DirectoryNode("/");
@@ -37,7 +37,8 @@ internal class NoSpace : Problem<int,int>
ParseCommand(line[2..], ref curDir); ParseCommand(line[2..], ref curDir);
dir = $"/{string.Join("/", curDir.Reverse())}"; dir = $"/{string.Join("/", curDir.Reverse())}";
_dirTree.AddDirectory(dir); _dirTree.AddDirectory(dir);
}else }
else
ReadDirectory(line, _dirTree.GetDirectory(dir)!); ReadDirectory(line, _dirTree.GetDirectory(dir)!);
} }
} }

View File

@@ -68,7 +68,6 @@ public class RopeSimulator
else else
_visited.Add(tail, 1); _visited.Add(tail, 1);
} }
} }
private void FollowHead() private void FollowHead()
@@ -129,12 +128,12 @@ public class RopeSimulator
Draw('s', (0, 0), lowerX, upperY); Draw('s', (0, 0), lowerX, upperY);
Draw('H', _head, lowerX, upperY); Draw('H', _head, lowerX, upperY);
for (int i = _segments.Length -1; i >= 0; i--) for (int i = _segments.Length - 1; i >= 0; i--)
{ {
if(segment == i) if (segment == i)
Console.ForegroundColor= ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
else else
Console.ForegroundColor= ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
Draw((i + 1).ToString()[0], _segments[i], lowerX, upperY); Draw((i + 1).ToString()[0], _segments[i], lowerX, upperY);
} }
} }

View File

@@ -1,14 +1,5 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using Superpower.Parsers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2023.Day1; namespace AdventOfCode.Problems.AOC2023.Day1;
[ProblemInfo(2023, 1, "Trebuchet!?")] [ProblemInfo(2023, 1, "Trebuchet!?")]
@@ -20,6 +11,7 @@ public partial class Trebuchet : Problem<int, int>
{ {
_inputData = ReadInputLines(); _inputData = ReadInputLines();
} }
public override void CalculatePart1() public override void CalculatePart1()
{ {
Part1 = _inputData.Select(GetCalibrationValues) Part1 = _inputData.Select(GetCalibrationValues)
@@ -48,7 +40,7 @@ public partial class Trebuchet : Problem<int, int>
return (left, right); return (left, right);
} }
private readonly (string word, int value)[] _numberWords = new [] private readonly (string word, int value)[] _numberWords = new[]
{ {
("one", 1), ("one", 1),
("two", 2), ("two", 2),
@@ -78,13 +70,14 @@ public partial class Trebuchet : Problem<int, int>
{ {
left = word; left = word;
break; break;
}else if(line[i] - '0' >= 10) }
else if (line[i] - '0' >= 10)
continue; continue;
left = line[i] - '0'; left = line[i] - '0';
break; break;
} }
for (int i = line.Length - 1; i >= 0; i--) for (int i = line.Length - 1; i >= 0; i--)
{ {
var word = _numberWords.FirstOrDefault(v => line[..(i + 1)].EndsWith(v.word, StringComparison.Ordinal), (word: "", value: -1)).value; var word = _numberWords.FirstOrDefault(v => line[..(i + 1)].EndsWith(v.word, StringComparison.Ordinal), (word: "", value: -1)).value;
@@ -101,4 +94,4 @@ public partial class Trebuchet : Problem<int, int>
} }
return (left, right); return (left, right);
} }
} }

View File

@@ -1,12 +1,7 @@
using AdventOfCode.Runner.Attributes; using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2023.Day2; namespace AdventOfCode.Problems.AOC2023.Day2;
[ProblemInfo(2023, 2, "Cube Conundrum")] [ProblemInfo(2023, 2, "Cube Conundrum")]
internal class CubeConundrum : Problem<int, int> internal class CubeConundrum : Problem<int, int>
{ {
@@ -33,4 +28,4 @@ internal class CubeConundrum : Problem<int, int>
return _gameInfo.Where(g => !g.Rounds.Any(r => !r.IsPossible(constraints))) return _gameInfo.Where(g => !g.Rounds.Any(r => !r.IsPossible(constraints)))
.Select(r => r.Id); .Select(r => r.Id);
} }
} }

View File

@@ -1,12 +1,4 @@
using System; namespace AdventOfCode.Problems.AOC2023.Day2;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2023.Day2;
internal class CubeGame internal class CubeGame
{ {
@@ -25,12 +17,12 @@ internal class CubeGame
Rounds[i] = CubeRound.ParseRound(roundsData[i]); Rounds[i] = CubeRound.ParseRound(roundsData[i]);
} }
public CubeRound GetMinimalConstraints() public CubeRound GetMinimalConstraints()
{ {
var (r, g, b) = (0, 0, 0); var (r, g, b) = (0, 0, 0);
foreach (var round in Rounds) foreach (var round in Rounds)
{ {
if(round.Red > r) if (round.Red > r)
r = round.Red; r = round.Red;
if (round.Green > g) if (round.Green > g)
g = round.Green; g = round.Green;
@@ -54,15 +46,15 @@ internal record struct CubeRound(int Red, int Green, int Blue)
var count = int.Parse(info[0]); var count = int.Parse(info[0]);
switch (info[1]) switch (info[1])
{ {
case "red": case ['r', ..]:
r = count; r = count;
break; break;
case "green": case ['g', ..]:
g = count; g = count;
break; break;
case "blue": case ['b', ..]:
b = count; b = count;
break; break;
} }
@@ -72,7 +64,7 @@ internal record struct CubeRound(int Red, int Green, int Blue)
public readonly bool IsPossible(CubeRound constraints) public readonly bool IsPossible(CubeRound constraints)
{ {
if(Red > constraints.Red) if (Red > constraints.Red)
return false; return false;
if (Green > constraints.Green) if (Green > constraints.Green)
return false; return false;
@@ -86,4 +78,4 @@ internal record struct CubeRound(int Red, int Green, int Blue)
{ {
return Red * Green * Blue; return Red * Green * Blue;
} }
}; };

View File

@@ -1,5 +1,4 @@
 global using AdventOfCode.Runner;
global using AdventOfCode.Runner;
namespace AdventOfCode; namespace AdventOfCode;

View File

@@ -25,8 +25,8 @@ public class AOCRunner
_years = new List<int>(); _years = new List<int>();
_selectedYear = DateTime.Now.Year; _selectedYear = DateTime.Now.Year;
FindProblemClasses(); FindProblemClasses();
if(!_loadedProblems.ContainsKey(_selectedYear)) if (!_loadedProblems.ContainsKey(_selectedYear))
_selectedYear = _loadedProblems.Keys.First(); _selectedYear = _loadedProblems.Keys.First();
InitSizing(); InitSizing();
@@ -291,7 +291,7 @@ public class AOCRunner
Console.Write(" Problems "); Console.Write(" Problems ");
RenderProblemList(); RenderProblemList();
} }
else else
RenderProblemResults(); RenderProblemResults();
} }

View File

@@ -1,9 +1,11 @@
namespace AdventOfCode.Runner.Attributes; namespace AdventOfCode.Runner.Attributes;
public class ProblemInfoAttribute : Attribute public class ProblemInfoAttribute : Attribute
{ {
public int Day { get; init; } public int Day { get; init; }
public int Year { get; init; } public int Year { get; init; }
public string Name { get; init; } public string Name { get; init; }
public ProblemInfoAttribute(int year, int day, string name) public ProblemInfoAttribute(int year, int day, string name)
{ {
Year = year; Year = year;