private static long Run(HashSet <MultiDimensionalPosition> initialState) { return(Produce.Iterate(initialState, 6, current => { var adjacentCounts = new DictionaryWithDefault <MultiDimensionalPosition, int>(0); // Look at all active cells and all cells adjacent to active cells var cells = new HashSet <MultiDimensionalPosition>(); foreach (var position in current) { cells.Add(position); foreach (var pos2 in position.Adjacents()) { cells.Add(pos2); adjacentCounts[pos2] += 1; } } var next = cells.Where(position => { var active = current.Contains(position); return (active && adjacentCounts[position].IsInRange(2, 3)) || (!active && adjacentCounts[position] == 3); }).ToHashSet(); return next; }) .Count); }
private static CircularList <int> Run(CircularList <int> initialState, int iterations) { var nodeMap = initialState.Walk().ToDictionary(it => it.Value, it => it); var numberOfCups = nodeMap.Count; Produce.Iterate(initialState, iterations, state => { var pickedUp = state.Next.Extract(3); var destination = Modthing(state.Value - 1, numberOfCups); while (pickedUp.Value == destination || pickedUp.Next.Value == destination || pickedUp.Next.Next.Value == destination) { destination = Modthing(destination - 1, numberOfCups); } var d = nodeMap[destination]; d.Insert(pickedUp); return(state.Next); }); return(nodeMap[1]); }