AdventOfCode/AdventOfCode/Common/AOCExtensions.cs
Alexander Sigler 6ca37a1868 Add 2024 days
2025-05-05 10:47:22 -07:00

58 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Common
{
public static class AOCExtensions
{
public static int ToInt(this string str)
{
try
{
return Convert.ToInt32(str);
} catch (Exception e)
{
return 0;
throw e;
}
}
public static string ReplaceMultipleSpaces(this string str)
{
while (str.Contains(" ")) str = str.Replace(" ", " ");
return str;
}
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
public static void PrintSquareArray<T>(this T[,] arr)
{
for (int x = 0; x < arr.GetLength(0); x++)
{
for (int y = 0; y < arr.GetLength(1); y++)
{
Console.Write(arr[x, y] + " ");
}
Console.WriteLine();
}
}
public static bool IsWithinBoard<T>(this T[,] board, int newX, int newY)
{
if (newX < 0 || newY < 0
|| newX >= board.GetLength(0) || newY >= board.GetLength(1))
return false;
return true;
}
}
}