Пример #1
0
 private static long Part2(params long[] buses)
 {
     return(buses
            .Select((id, index) => new { id, index })
            .Where(bus => bus.id != -1)
            .Skip(1)
            .Aggregate((value: buses[0], increment: buses[0]), (state, bus) => (
                           value: Produce.Forever(state.value, current => current + state.increment)
                           .First(current => (current + bus.index).IsMultipleOf(bus.id)),
                           increment: state.increment * bus.id
                           ))
            .value);
 }
Пример #2
0
        internal static long PlayCombat(Day22Input initialState)
        {
            var result = Produce.Forever(initialState,
                                         state =>
            {
                var z         = state.Player1.Zip(state.Player2).ToList();
                var nextState = z.Aggregate((player1: state.Player1.Skip(z.Count).ToList(), player2: state.Player2.Skip(z.Count).ToList()), (accumulator, current) =>
                {
                    current.First.Should().NotBe(current.Second);
                    if (current.First > current.Second)
                    {
                        accumulator.player1.AddRange(new [] { current.First, current.Second });
                    }
                    else
                    {
                        accumulator.player2.AddRange(new[] { current.Second, current.First });
                    }

                    return(accumulator);
                });