From 596ea3e8cde0491adf3d8609dd238ac44d87867c Mon Sep 17 00:00:00 2001 From: Adrian O'Grady Date: Tue, 12 Dec 2023 17:32:28 +0000 Subject: [PATCH] A bit more code clean-up --- Advent2023/Day12.cs | 11 +--- Utils/Generators.cs | 142 ++++++++++++++++++++++---------------------- 2 files changed, 74 insertions(+), 79 deletions(-) diff --git a/Advent2023/Day12.cs b/Advent2023/Day12.cs index 9851620..49b27b3 100644 --- a/Advent2023/Day12.cs +++ b/Advent2023/Day12.cs @@ -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) { @@ -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 = []; diff --git a/Utils/Generators.cs b/Utils/Generators.cs index f79713a..3282616 100644 --- a/Utils/Generators.cs +++ b/Utils/Generators.cs @@ -1,75 +1,75 @@ -using System; -using System.Collections.Generic; - -namespace Utils -{ - public static class Generators - { - public static Func Cycler(int range) - { - var current = 0; - return () => current++ % range; - } - - public static Func CreateCycler(this IEnumerable collection) - { - var enumerator = collection.GetEnumerator(); - return () => - { - if (!enumerator.MoveNext()) - { - enumerator.Reset(); - enumerator.MoveNext(); - } - return enumerator.Current; - }; - } - - public static IEnumerable Cycle(this IEnumerable range) - { - while (true) - foreach (var v in range) - yield return v; - } - - public static Func Reader(this IList collection) - { - var current = 0; - return () => collection[current++]; - } - - public static Func Reader(this string text) - { - var current = 0; - return () => text[current++]; - } - - public static Func Reader(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 Cycler(int range) + { + var current = 0; + return () => current++ % range; + } + + public static Func CreateCycler(this IEnumerable collection) + { + var enumerator = collection.GetEnumerator(); + return () => + { + if (!enumerator.MoveNext()) + { + enumerator.Reset(); + enumerator.MoveNext(); + } + return enumerator.Current; + }; + } + + public static IEnumerable Cycle(this IEnumerable range) + { + while (true) + foreach (var v in range) + yield return v; + } + + public static Func Reader(this IList collection) + { + var current = 0; + return () => collection[current++]; + } + + public static Func 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 Reader(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(this T[,] array) - { - foreach (var pos in array.Rectangle()) + { + foreach (var pos in array.Rectangle()) yield return (pos, array[pos.x, pos.y]); - } - } -} + } + } +}