Example #1
0
        private int Part1(int[] banks)
        {
            int cycles = 0;
            HashSet <BanksState> statesSeen   = new HashSet <BanksState>();
            BanksState           initialState = new BanksState(banks);

            statesSeen.Add(initialState);

            while (true)
            {
                DoRedistribution(banks);
                cycles++;

                BanksState currentState = new BanksState(banks);
                if (statesSeen.Contains(currentState))
                {
                    return(cycles);
                }

                statesSeen.Add(currentState);
            }
        }
Example #2
0
        private int Part2(int[] banks)
        {
            int cycles = 0;
            Dictionary <BanksState, int> statesSeen = new Dictionary <BanksState, int>();
            BanksState initialState = new BanksState(banks);

            statesSeen.Add(initialState, 0);

            while (true)
            {
                DoRedistribution(banks);
                cycles++;

                BanksState currentState = new BanksState(banks);
                if (statesSeen.Keys.Contains(currentState))
                {
                    return(cycles - statesSeen[currentState]);
                }

                statesSeen.Add(currentState, cycles);
            }
        }
Example #3
0
        public override bool Equals(object obj)
        {
            // All of this safety checking is overkill for this simple program, but it's
            // good practice (and habit!), so I included it.
            if (!(obj is BanksState))
            {
                return(false);
            }
            BanksState other = (BanksState)obj;

            if (other.Values.Length != this.Values.Length)
            {
                return(false);
            }

            for (int i = 0; i < this.Values.Length; i++)
            {
                if (this.Values[i] != other.Values[i])
                {
                    return(false);
                }
            }
            return(true);
        }