Initial Commit

This commit is contained in:
Jose Caban
2025-11-30 20:28:10 -05:00
commit e9ac699e67
209 changed files with 39737 additions and 0 deletions

156
2023/Day03CSharp/Program.cs Normal file
View File

@@ -0,0 +1,156 @@
using AdventCommon;
using System.ComponentModel.DataAnnotations;
using System.Numerics;
var p1exampleSln = Part01("example-input.txt");
var p1puzzleSln = Part01("puzzle-input.txt");
Console.WriteLine("Part01 Example Sum: " + p1exampleSln);
Console.WriteLine("Part01 Puzzle Sum: " + p1puzzleSln);
var p2exampleSln = Part02("example-input.txt");
var p2puzzleSln = Part02("puzzle-input.txt");
Console.WriteLine("Part02 Example Sum: " + p2exampleSln);
Console.WriteLine("Part02 Puzzle Sum: " + p2puzzleSln);
int Part01(string fileName)
{
var input = new Day03Input(fileName);
int sum = 0;
foreach (var num in input.Numbers)
{
bool foundAdjacent = false;
foreach (var s in input.Symbols)
{
if (Day03Input.Part.IsAdjacent(num, s))
{
foundAdjacent = true;
break;
}
}
if (foundAdjacent)
{
sum += num.Value;
}
}
return sum;
}
int Part02(string fileName)
{
var input = new Day03Input(fileName);
int sum = 0;
foreach (var symbol in input.Symbols.Where(x => x.S == '*'))
{
List<Day03Input.Number> adjacentNums = new List<Day03Input.Number>();
foreach (var num in input.Numbers)
{
if (Day03Input.Part.IsAdjacent(num, symbol))
{
adjacentNums.Add(num);
}
}
if (adjacentNums.Count == 2)
{
sum += adjacentNums[0].Value * adjacentNums[1].Value;
}
}
return sum;
}
class Day03Input : PuzzleInput
{
public abstract class Part
{
public Part(int x, int y, int length)
{
Coords = new Vector2(x, y);
Length = length;
}
public Vector2 Coords { get; set; }
public float X { get { return Coords.X; } }
public float Y { get { return Coords.Y; } }
public int Length { get; private set; }
public static bool IsAdjacent(Part left, Part right)
{
for (int i = 0; i < left.Length; i++)
{
for (int j = 0; j < right.Length; j++)
{
var l = new Vector2(left.X + i, left.Y);
var r = new Vector2(right.X + j, right.Y);
if (Vector2.Subtract(l, r).Length() < 2)
return true;
}
}
return false;
}
}
public class Number : Part
{
public Number(int x, int y, int value, int length) : base(x,y,length)
{
Value = value;
}
public int Value { get; private set; }
}
public class Symbol : Part
{
public Symbol(int x, int y, char s) : base(x,y,1)
{
S = s;
}
public char S { get; private set; }
}
public Day03Input(string fileName) : base(fileName)
{
for (int j = 0; j < Lines.Count; j++)
{
for (int i = 0; i < Lines[j].Length; i++)
{
if (Char.IsDigit(Lines[j][i]))
{
string num = "";
for (int sub = i; sub < Lines[j].Length && Char.IsDigit(Lines[j][sub]); sub++)
{
num += Lines[j][sub];
}
int val = Int32.Parse(num);
var n = new Number(i, j, val, num.Length);
Numbers.Add(n);
// Skip remaining characters in the number
for (int upper = i + n.Length - 1; i < upper; i++) ;
}
else if (!Char.IsLetterOrDigit(Lines[j][i]) && Lines[j][i] != '.')
{
var v = new Symbol(i, j, Lines[j][i]);
Symbols.Add(v);
}
}
}
}
public List<Number> Numbers { get; set; } = new List<Number>();
public List<Symbol> Symbols { get; set; } = new List<Symbol>();
}