예제 #1
0
        public override object Puzzle2(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            string group    = "";
            bool   newGroup = true;
            int    sum      = 0;

            foreach (string line in lines)
            {
                if (!string.IsNullOrEmpty(line))
                {
                    if (newGroup)
                    {
                        group    = line;
                        newGroup = false;
                    }
                    else
                    {
                        group = new String(group.Intersect(line).ToArray());
                    }
                }
                else
                {
                    sum     += group.Distinct().Count();
                    group    = "";
                    newGroup = true;
                }
            }

            // Add last group.
            sum += group.Distinct().Count();

            return(sum);
        }
예제 #2
0
        public override object Puzzle2(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            int[] busses = lines[1].Split(',').Select(x => x.Contains("x") ? 0 : int.Parse(x)).ToArray();

            long step     = 0;
            long stepSize = busses[0];

            for (int offset = 1; offset < busses.Length; ++offset)
            {
                if (busses[offset] == 0)
                {
                    continue;
                }

                step += stepSize;

                if ((step + offset) % busses[offset] != 0)
                {
                    --offset;
                    continue;
                }

                stepSize = stepSize * busses[offset];
            }

            return(step);
        }
예제 #3
0
        public override object Puzzle2(AOCInput input)
        {
            string[] lines   = input.GetInputLines();
            int[]    numbers = new int[lines.Length];

            for (int i = 0; i < lines.Length; i++)
            {
                numbers[i] = int.Parse(lines[i]);
            }

            for (int i = 0; i < numbers.Length; ++i)
            {
                for (int j = 0; j < numbers.Length; ++j)
                {
                    for (int k = 0; k < numbers.Length; ++k)
                    {
                        if (i == j || i == k || j == k)
                        {
                            continue;
                        }

                        if (numbers[i] + numbers[j] + numbers[k] == 2020)
                        {
                            return(numbers[i] * numbers[j] * numbers[k]);
                        }
                    }
                }
            }

            return(null);
        }
예제 #4
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            string group = "";

            int sum = 0;

            foreach (string line in lines)
            {
                if (!string.IsNullOrEmpty(line))
                {
                    group += line;
                }
                else
                {
                    sum  += group.Distinct().Count();
                    group = "";
                }
            }

            // Add last group.
            sum += group.Distinct().Count();

            return(sum);
        }
예제 #5
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines    = input.GetInputLines();
            int[]    joltages = lines.Select(int.Parse).OrderBy(x => x).ToArray();

            int device = joltages[joltages.Length - 1] + 3;

            int curJoltage = 0;

            int diff1 = 0;
            int diff2 = 0;
            int diff3 = 1;

            for (int i = 0; i < joltages.Length; ++i)
            {
                if (joltages[i] - curJoltage == 1)
                {
                    diff1++;
                }
                else if (joltages[i] - curJoltage == 2)
                {
                    diff2++;
                }
                else if (joltages[i] - curJoltage == 3)
                {
                    diff3++;
                }

                curJoltage = joltages[i];
            }

            return(diff1 * diff3);
        }
예제 #6
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            int answer = 0;

            for (int i = 0; i < lines.Length; ++i)
            {
                string[] split = lines[i].Split(new char[] { '-', ' ', ':' });

                int    min       = int.Parse(split[0]);
                int    max       = int.Parse(split[1]);
                char   character = split[2][0];
                string password  = split[4];

                int count = 0;

                for (int j = 0; j < password.Length; ++j)
                {
                    if (password[j] == character)
                    {
                        count++;
                    }
                }

                if (count >= min && count <= max)
                {
                    answer++;
                }
            }

            return(answer);
        }
예제 #7
0
        public override object Puzzle2(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            int answer = 0;

            for (int i = 0; i < lines.Length; ++i)
            {
                string[] split = lines[i].Split(new char[] { '-', ' ', ':' });

                int    first     = int.Parse(split[0]);
                int    second    = int.Parse(split[1]);
                char   character = split[2][0];
                string password  = split[4];

                bool hasFirst  = password[first - 1] == character;
                bool hasSecond = password[second - 1] == character;

                if (hasFirst ^ hasSecond)
                {
                    answer++;
                }
            }

            return(answer);
        }
예제 #8
0
        public override object Puzzle2(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            int[] patternX = { 1, 3, 5, 7, 1 };
            int[] patternY = { 1, 1, 1, 1, 2 };

            long product = 1;

            for (int i = 0; i < patternX.Length; ++i)
            {
                int posX = 0;
                int posY = 0;

                int trees = 0;

                while (posY < lines.Length)
                {
                    if (lines[posY][(posX % lines[posY].Length)] == '#')
                    {
                        trees++;
                    }

                    posX += patternX[i];
                    posY += patternY[i];
                }

                product *= trees;
            }

            return(product);
        }
예제 #9
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            string[] map = input.GetInputLines();

            for (int y1 = 0; y1 < map.Length; ++y1)
            {
                for (int x1 = 0; x1 < map[y1].Length; ++x1)
                {
                    // If astroid, we might need to place station here.
                    if (map[y1][x1] == '#')
                    {
                        for (int y2 = 0; y2 < map.Length; ++y2)
                        {
                            for (int x2 = 0; x2 < map[y2].Length; ++x2)
                            {
                                // If same astroid, do nothing.
                                if (y1 == y2 && x1 == x2)
                                {
                                    continue;
                                }

                                int xDist = x1 - x2;
                                int yDist = y1 - y2;
                            }
                        }
                    }
                }
            }

            return(null);
        }
예제 #10
0
        public override object Puzzle2(AOCInput input)
        {
            string[] rules = input.GetInputLines();

            Dictionary <string, Dictionary <string, int> > bags = new Dictionary <string, Dictionary <string, int> >();

            foreach (string rule in rules)
            {
                string[] splitRule = rule.Split(new string[] { "bags contain", "bags", "bag", ",", "." }, StringSplitOptions.RemoveEmptyEntries);

                Dictionary <string, int> contains = new Dictionary <string, int>();


                if (!rule.Contains("no other bags."))
                {
                    for (int i = 1; i < splitRule.Length; ++i)
                    {
                        int    amount = int.Parse(splitRule[i].Trim().Split(' ')[0]);
                        string name   = splitRule[i].Trim().Remove(0, 1 + amount.ToString().Length);

                        contains.Add(name, amount);
                    }
                }

                bags.Add(splitRule[0].Trim(), contains);
            }

            return(Count(bags, "shiny gold"));
        }
예제 #11
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            int earliest = int.Parse(lines[0]);

            int[] busses = lines[1].Split(',').Where(x => !x.Contains("x")).Select(int.Parse).ToArray();

            // Search at lest for the minimum id;
            int attempts = earliest / busses.Min() + 1;

            int closestDistance = int.MaxValue;
            int timeToWait      = 0;
            int busID           = 0;

            for (int i = 0; i < attempts; ++i)
            {
                for (int j = 0; j < busses.Length; ++j)
                {
                    int distance = Math.Abs(earliest - busses[j] * i);

                    if (distance < closestDistance && busses[j] * i > earliest)
                    {
                        closestDistance = distance;
                        timeToWait      = (busses[j] * i) - earliest;
                        busID           = busses[j];
                    }
                }
            }

            return(timeToWait * busID);
        }
예제 #12
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            // Get input.
            string[] lines = input.GetInputLines().Select(x => x.Trim()).ToArray();
            Dictionary <string, Orbit> orbits = GetOrbits(lines);

            // Get target and start orbit.
            Orbit start  = orbits["YOU"].lower;
            Orbit target = orbits["SAN"].lower;

            List <Orbit> visited = new List <Orbit>();

            start.dist = 0;
            visited.Add(start);

            bool found = false;

            while (!found)
            {
                Orbit        cur        = visited.OrderBy(x => x.dist).First();
                List <Orbit> neighbours = cur.GetOrbits();

                foreach (Orbit neighbour in neighbours)
                {
                    if (neighbour == null)
                    {
                        continue;
                    }

                    if (neighbour.dist > cur.dist + 1)
                    {
                        neighbour.dist = cur.dist + 1;
                        neighbour.path = cur;
                        visited.Add(neighbour);

                        if (neighbour == target)
                        {
                            found = true;
                        }
                    }
                }
                visited.Remove(cur);
            }

            // Reconstruct path found, keep track of steps required.
            Orbit path      = target;
            int   transfers = 0;

            while (path != null)
            {
                path = path.path;

                if (path != null)
                {
                    transfers++;
                }
            }

            return(transfers);
        }
예제 #13
0
        public override object Puzzle2(AOCInput input)
        {
            string[] lines    = input.GetInputLines();
            int[]    joltages = lines.Select(int.Parse).OrderBy(x => x).ToArray();

            int device = joltages[joltages.Length - 1] + 3;

            _cachedArrangements = new Dictionary <int, long>();

            return(CalculateArrangements(0, joltages, 0) + 1);
        }
예제 #14
0
        public override object Puzzle2(AOCInput input)
        {
            string[] boardingTickets = input.GetInputLines();

            List <int> ids = new List <int>();

            foreach (string ticket in boardingTickets)
            {
                int rowMin    = 0;
                int rowMax    = 127;
                int columnMin = 0;
                int columnMax = 8;

                for (int i = 0; i < ticket.Length; ++i)
                {
                    if (ticket[i] == 'F')
                    {
                        rowMax -= ((rowMax + 1) - rowMin) / 2;
                    }
                    else if (ticket[i] == 'B')
                    {
                        rowMin += ((rowMax + 1) - rowMin) / 2;
                    }
                    else if (ticket[i] == 'L')
                    {
                        columnMax -= ((columnMax + 1) - columnMin) / 2;
                    }
                    else if (ticket[i] == 'R')
                    {
                        columnMin += ((columnMax + 1) - columnMin) / 2;
                    }
                }

                int id = rowMin * 8 + columnMin;
                ids.Add(id);
            }

            foreach (int id in ids)
            {
                if (ids.Contains(id - 2) && !ids.Contains(id - 1))
                {
                    return(id - 1);
                }
            }

            return(-1);
        }
예제 #15
0
        public override object Puzzle2(AOCInput input)
        {
            const int preambleLength = 25;

            string[] lines   = input.GetInputLines();
            long[]   numbers = lines.Select(long.Parse).ToArray();

            long[] preamble = new long[preambleLength];

            long answer = 0;

            for (int i = 0; i < numbers.Length - 1; ++i)
            {
                preamble[i % preambleLength] = numbers[i];

                if (i >= preambleLength)
                {
                    if (!CheckRule(preamble, numbers[i + 1]))
                    {
                        answer = numbers[i + 1];
                    }
                }
            }

            int size = 2;

            while (true)
            {
                for (int i = 0; i < numbers.Length - size; ++i)
                {
                    long sum = 0;
                    for (int j = 0; j < size; ++j)
                    {
                        sum += numbers[i + j];
                    }

                    if (sum == answer)
                    {
                        return(numbers.Skip(i).Take(size).Min() + numbers.Skip(i).Take(size).Max());
                    }
                }

                size++;
            }

            return(null);
        }
예제 #16
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            // Parse input.
            string[] lines = input.GetInputLines();

            List <Point> line1 = GetLine(lines[0].Split(','));
            List <Point> line2 = GetLine(lines[1].Split(','));

            // Find intersections.
            List <Point> intersections = line1.Intersect(line2).ToList();

            // Remove 0,0 intersection.
            intersections.RemoveAt(0);

            // Find fewest combines steps to intersection.
            return(intersections.Select(x => line1.IndexOf(x) + line2.IndexOf(x))
                   .OrderBy(x => x).First());
        }
예제 #17
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            // Parse input.
            string[] lines = input.GetInputLines();

            List <Point> line1 = GetLine(lines[0].Split(','));
            List <Point> line2 = GetLine(lines[1].Split(','));

            // Find intersections.
            List <Point> intersections = line1.Intersect(line2).ToList();

            // Remove 0,0 intersection.
            intersections.RemoveAt(0);

            // Calculate closest manhattan distance.
            return(intersections.Select(x => Math.Abs(x.X) + Math.Abs(x.Y))
                   .OrderBy(x => x).First());
        }
예제 #18
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            string passport = "";
            int    valid    = 0;

            for (int i = 0; i < lines.Length; ++i)
            {
                if (!string.IsNullOrEmpty(lines[i]) && lines[i].Length != 1)
                {
                    passport += lines[i] + " ";
                }
                else
                {
                    string[] keyvalues = passport.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (keyvalues.Length == 7)
                    {
                        bool isValid = true;

                        if (passport.Contains("cid"))
                        {
                            isValid = false;
                        }

                        if (isValid)
                        {
                            valid++;
                        }
                    }
                    else if (keyvalues.Length == 8)
                    {
                        valid++;
                    }

                    passport = "";
                }
            }

            // > 229
            // < 243
            return(valid);
        }
예제 #19
0
        public override object Puzzle1(AOCInput input)
        {
            string[] boardingTickets = input.GetInputLines();

            int highestID = 0;

            foreach (string ticket in boardingTickets)
            {
                int rowMin    = 0;
                int rowMax    = 127;
                int columnMin = 0;
                int columnMax = 8;

                for (int i = 0; i < ticket.Length; ++i)
                {
                    if (ticket[i] == 'F')
                    {
                        rowMax -= ((rowMax + 1) - rowMin) / 2;
                    }
                    else if (ticket[i] == 'B')
                    {
                        rowMin += ((rowMax + 1) - rowMin) / 2;
                    }
                    else if (ticket[i] == 'L')
                    {
                        columnMax -= ((columnMax + 1) - columnMin) / 2;
                    }
                    else if (ticket[i] == 'R')
                    {
                        columnMin += ((columnMax + 1) - columnMin) / 2;
                    }
                }

                int id = rowMin * 8 + columnMin;

                if (id > highestID)
                {
                    highestID = id;
                }
            }

            return(highestID);
        }
예제 #20
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            int posX = 0;
            int posY = 0;

            int trees = 0;

            while (posY < lines.Length)
            {
                if (lines[posY][(posX % lines[posY].Length)] == '#')
                {
                    trees++;
                }

                posX += 3;
                posY += 1;
            }

            return(trees);
        }
예제 #21
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            // Get input.
            string[] lines = input.GetInputLines().Select(x => x.Trim()).ToArray();
            Dictionary <string, Orbit> orbits = GetOrbits(lines);

            int orbitCount = 0;

            // Count down to the COM.
            foreach (Orbit orbit in orbits.Values)
            {
                Orbit lower = orbit.lower;

                while (lower != null)
                {
                    orbitCount++;
                    lower = lower.lower;
                }
            }

            return(orbitCount);
        }
예제 #22
0
        public override object Puzzle1(AOCInput input)
        {
            const int preambleLength = 25;

            string[] lines   = input.GetInputLines();
            long[]   numbers = lines.Select(long.Parse).ToArray();

            long[] preamble = new long[preambleLength];

            for (int i = 0; i < numbers.Length - 1; ++i)
            {
                preamble[i % preambleLength] = numbers[i];

                if (i >= preambleLength)
                {
                    if (!CheckRule(preamble, numbers[i + 1]))
                    {
                        return(numbers[i + 1]);
                    }
                }
            }

            return(null);
        }
예제 #23
0
        public override object Puzzle1(AOCInput input)
        {
            string[] program = input.GetInputLines();

            int accumulator = 0;

            List <int> visited = new List <int>();

            for (int programCounter = 0; programCounter < program.Length; ++programCounter)
            {
                if (visited.Contains(programCounter))
                {
                    return(accumulator);
                }

                visited.Add(programCounter);

                switch (program[programCounter][0])
                {
                case 'n':
                    break;

                case 'a':
                    int accChange = GetValueFromInstruction(program[programCounter]);
                    accumulator += accChange;
                    break;

                case 'j':
                    int jmpValue = GetValueFromInstruction(program[programCounter]);
                    programCounter += (jmpValue - 1);
                    break;
                }
            }

            return(null);
        }
예제 #24
0
        public override object Puzzle2(AOCInput input)
        {
            input.SetExampleInput(@"mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1");

            string[] lines = input.GetInputLines();

            Dictionary <int, ulong> memory = new Dictionary <int, ulong>();

            ulong        mask  = 0;
            List <ulong> masks = new List <ulong>();

            foreach (string line in lines)
            {
                string[] split = line.Split(new string[] { "mem[", "]", "=", " " }, System.StringSplitOptions.RemoveEmptyEntries);

                if (split[0].Equals("mask"))
                {
                    mask = 0;

                    for (int i = 0; i < split[1].Length; ++i)
                    {
                        mask = mask << 1;

                        if (split[1][i] == '1')
                        {
                            mask++;
                        }
                    }

                    masks.Add(mask);

                    for (int i = 0; i < split[1].Length; ++i)
                    {
                        if (split[1][i] == 'X')
                        {
                            int maskCount = masks.Count;
                            for (int j = 0; j < maskCount; ++j)
                            {
                                ulong newMask = ((ulong)1 << split[1].Length - 1 - i);
                                newMask = newMask | masks[j];
                                masks.Add(newMask);
                            }
                        }
                    }
                }
                else
                {
                    int   address = int.Parse(split[0]);
                    ulong value   = ulong.Parse(split[1]);

                    for (int i = 0; i < masks.Count; ++i)
                    {
                        if (!memory.ContainsKey(address))
                        {
                            memory.Add(address, 0);
                        }

                        memory[address + i] = value | masks[i];
                    }
                }
            }

            ulong total = 0;

            for (int i = 0; i < memory.Count; ++i)
            {
                total += memory.ElementAt(i).Value;
            }

            return(total);
        }
예제 #25
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            return(lines.Select(x => GetFuel(int.Parse(x))).Sum());
        }
예제 #26
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            return(lines.Select(x => int.Parse(x)).Select(x => GetWeightRecursive(x) - x).Sum());
        }
예제 #27
0
        public override object Puzzle2(AOCInput input)
        {
            string[] instructions = input.GetInputLines();

            int posX = 0;
            int posY = 0;
            int dir  = 0;

            int waypointX = 10;
            int waypointY = -1;

            int[,] directions = new int[, ]
            {
                { 1, 0 },  // east
                { 0, 1 },  // south
                { -1, 0 }, // west
                { 0, -1 }, // north
            };

            foreach (string instruction in instructions)
            {
                char action = instruction[0];
                int  amount = int.Parse(new string(instruction.Skip(1).ToArray()));

                int moveDir    = dir;
                int moveAmount = 0;

                switch (action)
                {
                case 'N':
                    moveDir    = 3;
                    moveAmount = amount;
                    break;

                case 'E':
                    moveDir    = 0;
                    moveAmount = amount;
                    break;

                case 'S':
                    moveDir    = 1;
                    moveAmount = amount;
                    break;

                case 'W':
                    moveDir    = 2;
                    moveAmount = amount;
                    break;

                case 'L':
                    int counterClockwiseRotations = amount / 90;

                    for (int i = 0; i < counterClockwiseRotations; ++i)
                    {
                        int temp = waypointX;
                        waypointX = waypointY;
                        waypointY = -temp;
                    }

                    break;

                case 'R':
                    int clockwiseRotations = amount / 90;

                    for (int i = 0; i < clockwiseRotations; ++i)
                    {
                        int temp = waypointX;
                        waypointX = -waypointY;
                        waypointY = temp;
                    }

                    break;

                case 'F':
                    posX += waypointX * amount;
                    posY += waypointY * amount;
                    break;
                }

                waypointX += directions[Modulo(moveDir, 4), 0] * moveAmount;
                waypointY += directions[Modulo(moveDir, 4), 1] * moveAmount;
            }

            return(Math.Abs(posX) + Math.Abs(posY));
        }
예제 #28
0
        public override object Puzzle1(AOCInput input)
        {
            string[] instructions = input.GetInputLines();

            int posX = 0;
            int posY = 0;

            int dir = 0;

            int[,] directions = new int[, ]
            {
                { 1, 0 },  // east
                { 0, 1 },  // south
                { -1, 0 }, // west
                { 0, -1 }, // north
            };

            foreach (string instruction in instructions)
            {
                char action = instruction[0];
                int  amount = int.Parse(new string(instruction.Skip(1).ToArray()));

                int moveDir    = dir;
                int moveAmount = 0;

                switch (action)
                {
                case 'N':
                    moveDir    = 3;
                    moveAmount = amount;
                    break;

                case 'E':
                    moveDir    = 0;
                    moveAmount = amount;
                    break;

                case 'S':
                    moveDir    = 1;
                    moveAmount = amount;
                    break;

                case 'W':
                    moveDir    = 2;
                    moveAmount = amount;
                    break;

                case 'L':
                    dir -= amount / 90;
                    break;

                case 'R':
                    dir += amount / 90;
                    break;

                case 'F':
                    moveAmount = amount;
                    break;
                }

                posX += directions[Modulo(moveDir, 4), 0] * moveAmount;
                posY += directions[Modulo(moveDir, 4), 1] * moveAmount;
            }

            return(Math.Abs(posX) + Math.Abs(posY));
        }
예제 #29
0
        public override object Puzzle2(AOCInput input)
        {
            string[] program = input.GetInputLines();

            for (int i = 0; i < program.Length; ++i)
            {
                if (program[i][0] == 'n' || program[i][0] == 'j')
                {
                    if (program[i][0] == 'n')
                    {
                        program[i] = program[i].Replace('n', 'j');
                    }
                    else
                    {
                        program[i] = program[i].Replace('j', 'n');
                    }

                    int        programCounter;
                    int        accumulator = 0;
                    List <int> visited     = new List <int>();

                    for (programCounter = 0; programCounter < program.Length; ++programCounter)
                    {
                        if (visited.Contains(programCounter))
                        {
                            break;
                        }

                        visited.Add(programCounter);

                        switch (program[programCounter][0])
                        {
                        case 'n':
                            break;

                        case 'a':
                            int accChange = GetValueFromInstruction(program[programCounter]);
                            accumulator += accChange;
                            break;

                        case 'j':
                            int jmpValue = GetValueFromInstruction(program[programCounter]);
                            programCounter += (jmpValue - 1);
                            break;
                        }
                    }

                    if (programCounter == program.Length)
                    {
                        return(accumulator);
                    }
                    else
                    {
                        // restore program.
                        if (program[i][0] == 'n')
                        {
                            program[i] = program[i].Replace('n', 'j');
                        }
                        else
                        {
                            program[i] = program[i].Replace('j', 'n');
                        }
                    }
                }
            }

            return(null);
        }
예제 #30
0
        public override object Puzzle1(AOCInput input)
        {
            string[] lines = input.GetInputLines();

            Dictionary <int, ulong> memory = new Dictionary <int, ulong>();

            ulong zeroesMask = 0;
            ulong onesMask   = 0;

            foreach (string line in lines)
            {
                string[] split = line.Split(new string[] { "mem[", "]", "=", " " }, System.StringSplitOptions.RemoveEmptyEntries);

                if (split[0].Equals("mask"))
                {
                    zeroesMask = 0;
                    onesMask   = 0;

                    for (int i = 0; i < split[1].Length; ++i)
                    {
                        zeroesMask = zeroesMask << 1;
                        onesMask   = onesMask << 1;

                        if (split[1][i] == 'X')
                        {
                            continue;
                        }

                        if (split[1][i] == '0')
                        {
                            zeroesMask++;
                        }
                        else
                        {
                            onesMask++;
                        }
                    }
                }
                else
                {
                    int   address = int.Parse(split[0]);
                    ulong value   = ulong.Parse(split[1]);

                    if (!memory.ContainsKey(address))
                    {
                        memory.Add(address, 0);
                    }

                    value &= ~zeroesMask;
                    value |= onesMask;

                    memory[address] = value;
                }
            }

            ulong total = 0;

            for (int i = 0; i < memory.Count; ++i)
            {
                total += memory.ElementAt(i).Value;
            }

            return(total);
        }