コード例 #1
0
ファイル: Day08.cs プロジェクト: muztanger/aoc2020
            public int Exec2()
            {
                Reset();

                var mem = new DefaultDictionary <int, int>();

                while (mIndex != mProgram.Count)
                {
                    mem[mIndex]++;
                    if (mem[mIndex] >= 10)
                    {
                        throw new TimeoutException();
                    }

                    var instruction = mProgram[mIndex];
                    switch (instruction.Op)
                    {
                    case Operation.Nop: Nop(); break;

                    case Operation.Acc: Acc(instruction.Value); break;

                    case Operation.Jmp: Jmp(instruction.Value); break;

                    default: throw new NotImplementedException();
                    }
                }
                return(mAccumulator);
            }
コード例 #2
0
        /// <summary>
        /// Copies the given <paramref name="source"/> enumerable to a <see cref="DefaultDictionary{TKey,TValue}"/>
        /// </summary>
        /// <typeparam name="TSource">The type of the items in the source enumerable.</typeparam>
        /// <typeparam name="TKey">The type of the key of the resulting Dictionary.</typeparam>
        /// <typeparam name="TValue">The type of the value of the resulting Dictionary.</typeparam>
        /// <param name="source">The source enumerable.</param>
        /// <param name="keySelector">A function to generate the key from a given item.</param>
        /// <param name="valueSelector">A function to generate the value from a given item.</param>
        /// <param name="defaultFactory">The default factory to be used in the Dictionary.</param>
        /// <returns>A DefaultDictionary containing the items of the given enumerable, transformed by the given selector functions.</returns>
        /// <exception cref="ArgumentNullException">If any of the parameters are null.</exception>
        public static IDictionary <TKey, TValue> ToDefaultDictionary <TSource, TKey, TValue>(this IEnumerable <TSource> source,
                                                                                             Func <TSource, TKey> keySelector, Func <TSource, TValue> valueSelector, Func <TKey, TValue> defaultFactory)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (keySelector == null)
            {
                throw new ArgumentNullException(nameof(keySelector));
            }
            if (valueSelector == null)
            {
                throw new ArgumentNullException(nameof(valueSelector));
            }
            if (defaultFactory == null)
            {
                throw new ArgumentNullException(nameof(defaultFactory));
            }

            var dict = new DefaultDictionary <TKey, TValue>(defaultFactory);

            foreach (var item in source)
            {
                dict.Add(keySelector(item), valueSelector(item));
            }
            return(dict);
        }
コード例 #3
0
ファイル: Day10.cs プロジェクト: muztanger/aoc2020
        private static int DiffCount(IOrderedEnumerable <int> jolts)
        {
            var counts = new DefaultDictionary <int, int>();
            var last   = 0;

            foreach (var x in jolts)
            {
                int diff = x - last;
                counts[diff]++;
                last = x;
            }
            counts[3]++;
            var result = counts[1] * counts[3];

            return(result);
        }
コード例 #4
0
ファイル: Day20.cs プロジェクト: muztanger/aoc2020
        private static Dictionary <int, int> CountNeighbours(KeyValuePair <int, Tile>[] parsed)
        {
            var nCount = new DefaultDictionary <int, int>();

            for (int i = 0; i < parsed.Length - 1; i++)
            {
                var t1 = parsed[i];
                for (int j = i + 1; j < parsed.Length; j++)
                {
                    var t2 = parsed[j];
                    if (t1.Value.Neighbour(t2.Value) && t2.Value.Neighbour(t1.Value))
                    {
                        nCount[t1.Key]++;
                        nCount[t2.Key]++;
                    }
                }
            }
            return(nCount.Inner);
        }