Skip to content

Commit

Permalink
A bit more code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
elpollouk committed Dec 12, 2023
1 parent a1eacd0 commit 596ea3e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 79 deletions.
11 changes: 3 additions & 8 deletions Advent2023/Day12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ namespace Advent2023
{
public class Day12
{
class CheckGroup
class CheckGroup(string groups)
{
readonly int[] counts;

public CheckGroup(string groups)
{
counts = groups.Split(',').Select(int.Parse).ToArray();
}
readonly int[] counts = groups.Split(',').Select(int.Parse).ToArray();

public bool IsValid(int group, int checkSize)
{
Expand All @@ -31,7 +26,7 @@ public bool HasCompleted(int group)

class CheckContext(string input, string groups)
{
readonly CheckGroup checks = new CheckGroup(groups);
readonly CheckGroup checks = new(groups);
readonly char[] currentState = [.. input];
readonly Dictionary<(int, int, int), long> cache = [];

Expand Down
142 changes: 71 additions & 71 deletions Utils/Generators.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
using System;
using System.Collections.Generic;

namespace Utils
{
public static class Generators
{
public static Func<int> Cycler(int range)
{
var current = 0;
return () => current++ % range;
}

public static Func<T> CreateCycler<T>(this IEnumerable<T> collection)
{
var enumerator = collection.GetEnumerator();
return () =>
{
if (!enumerator.MoveNext())
{
enumerator.Reset();
enumerator.MoveNext();
}
return enumerator.Current;
};
}

public static IEnumerable<T> Cycle<T>(this IEnumerable<T> range)
{
while (true)
foreach (var v in range)
yield return v;
}

public static Func<T> Reader<T>(this IList<T> collection)
{
var current = 0;
return () => collection[current++];
}

public static Func<char> Reader(this string text)
{
var current = 0;
return () => text[current++];
}

public static Func<T> Reader<T>(params T[] collection)
{
var current = 0;
return () => collection[current++];
}

public static IEnumerable<(int x, int y)> Rectangle(int width, int height)
{
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
yield return (x, y);
using System;
using System.Collections.Generic;

namespace Utils
{
public static class Generators
{
public static Func<int> Cycler(int range)
{
var current = 0;
return () => current++ % range;
}

public static Func<T> CreateCycler<T>(this IEnumerable<T> collection)
{
var enumerator = collection.GetEnumerator();
return () =>
{
if (!enumerator.MoveNext())
{
enumerator.Reset();
enumerator.MoveNext();
}
return enumerator.Current;
};
}

public static IEnumerable<T> Cycle<T>(this IEnumerable<T> range)
{
while (true)
foreach (var v in range)
yield return v;
}

public static Func<T> Reader<T>(this IList<T> collection)
{
var current = 0;
return () => collection[current++];
}

public static Func<char> Reader(this string text)
{
var current = 0;
return () => text[current++];
}

public static IEnumerable<(int x, int y)> Rectangle((int x, int y) from, (int x, int y) to)
{
for (var y = from.y; y < to.y; y++)
for (var x = from.x; x < to.x; x++)
yield return (x, y);
}

public static IEnumerable<(int x, int y)> Rectangle(this Array array) => Rectangle(array.GetLength(0), array.GetLength(1));

public static Func<T> Reader<T>(params T[] collection)
{
var current = 0;
return () => collection[current++];
}

public static IEnumerable<(int x, int y)> Rectangle(int width, int height)
{
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
yield return (x, y);
}

public static IEnumerable<(int x, int y)> Rectangle((int x, int y) from, (int x, int y) to)
{
for (var y = from.y; y < to.y; y++)
for (var x = from.x; x < to.x; x++)
yield return (x, y);
}

public static IEnumerable<(int x, int y)> Rectangle(this Array array) => Rectangle(array.GetLength(0), array.GetLength(1));

public static IEnumerable<((int x, int y) pos, T item)> Iterate<T>(this T[,] array)
{
foreach (var pos in array.Rectangle())
{
foreach (var pos in array.Rectangle())
yield return (pos, array[pos.x, pos.y]);
}
}
}
}
}
}

0 comments on commit 596ea3e

Please sign in to comment.