public override string?CalculateSolutionPartTwo() { IDynamicIndexable2D <NodeState> nodes = _initialNodeStates.AsIndexable().ToDynamicIndexable(); Position position = (nodes.Length0 / 2, nodes.Length1 / 2); Direction direction = Direction.North; int count = 0; for (int i = 0; i < 10_000_000; i++) { direction = nodes.GetValueOrDefault(position.X, position.Y) switch { NodeState.Clean => direction.ToLeft(), NodeState.Weakened => direction, NodeState.Infected => direction.ToRight(), NodeState.Flagged => direction.ToReverse(), _ => throw new InvalidOperationException() }; nodes[position.X, position.Y] = (NodeState)(((int)nodes.GetValueOrDefault(position.X, position.Y) + 1) % s_nodeStateCount); if (nodes[position.X, position.Y] == NodeState.Infected) { count++; } position += direction; } return(SolutionPartTwo = count.ToString()); } }
public static string ToString <T>(this IDynamicIndexable2D <T> source, Func <T, char> charSelector, bool screenMode = false) { var builder = new StringBuilder(source.Length0 * (source.Length1 + Environment.NewLine.Length)); int?row = null; foreach (Position pos in source.EnumeratePositions(screenMode)) { if (row.HasValue && (screenMode ? pos.Y : pos.X) != row.Value) { builder.Append(Environment.NewLine); } row = screenMode ? pos.Y : pos.X; builder.Append(charSelector(source[pos])); } return(builder.ToString()); }
public DynamicIndexable2DEnumerator(IDynamicIndexable2D <TValue> values) { _values = values; Reset(); }
public static IEnumerable <Position> EnumeratePositions <T>(this IDynamicIndexable2D <T> source) { return(source.EnumeratePositions(source.Start0, source.Length0, source.Start1, source.Length1)); }
public static IEnumerable <Position> EnumeratePositions <T>(this IDynamicIndexable2D <T> source, Func <T, bool> predicate, Position?after = null, Position?before = null, bool screenMode = false) { return(source.EnumeratePositions(source.Start0, source.Length0, source.Start1, source.Length1, predicate, after, before, screenMode: screenMode)); }
public static IEnumerable <Position> EnumeratePositions <T>(this IDynamicIndexable2D <T> source, bool screenMode = false) { return(source.EnumeratePositions(source.Start0, source.Length0, source.Start1, source.Length1, screenMode: screenMode)); }