Exemple #1
0
        static Object PartA()
        {
            var input = ReadIndata.Strings(inputPath);
            LinkedList <int> circularBuffer = new LinkedList <int>(input[0].Select(x => (int)x - '0'));
            int n          = circularBuffer.Count;
            var valueToCup = new List <LinkedListNode <int> >(n + 1);

            for (int i = 0; i <= n; i++)
            {
                valueToCup.Add(null);
            }
            var pos = circularBuffer.First;

            for (int i = 1; i <= n; i++)
            {
                valueToCup[pos.Value] = pos;
                pos = pos.Next;
            }
            StepCrabCups(circularBuffer, valueToCup, 100);
            pos = circularBuffer.Find(1);
            var    pp  = pos.NextOrFirst();
            string ans = "";

            while (pp != pos)
            {
                ans += pp.Value.ToString();
                pp   = pp.NextOrFirst();
            }
            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #2
0
        static Object PartB()
        {
            var input = ReadIndata.Ints(inputPath);

            input.Add(0);
            input.Add(input.Max() + 3);
            input.Sort();
            long ans   = 1;
            int  i     = 0;
            int  z     = input.Count;
            var  combs = new int[] { 0, 1, 1, 2, 4, 7 };

            while (i < z - 1)
            {
                int n = 1;
                while ((i + n < z) && (input[i + n] - input[i + n - 1] == 1))
                {
                    n++;
                }
                ans *= combs[n];
                i   += n;
            }
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #3
0
        static Object PartA()
        {
            var      input  = ReadIndata.Strings(inputPath);
            Position pos    = new Position();
            int      dirIdx = 1;

            foreach (var s in input)
            {
                char c = s[0];
                int  n = int.Parse(s.Substring(1));
                if (c == 'F')
                {
                    pos += CoordsXY.directions4[Utils.Modulo(dirIdx, 4)] * n;
                }
                else if (c == 'L')
                {
                    dirIdx -= n / 90;
                }
                else if (c == 'R')
                {
                    dirIdx += n / 90;
                }
                else
                {
                    pos += CoordsXY.directions4[cardinals.IndexOf(c)] * n;
                }
            }
            int ans = pos.ManhattanDistance();

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #4
0
        static Object PartB()
        {
            var input = ReadIndata.Strings(inputPath);
            LinkedList <int> circularBuffer = new LinkedList <int>(input[0].Select(x => (int)x - '0'));
            int n = 1_000_000;

            for (int i = 10; i <= n; i++)
            {
                circularBuffer.AddLast(i);
            }
            var valueToCup = new List <LinkedListNode <int> >(n + 1);

            for (int i = 0; i <= n; i++)
            {
                valueToCup.Add(null);
            }
            var pos = circularBuffer.First;

            for (int i = 1; i <= n; i++)
            {
                valueToCup[pos.Value] = pos;
                pos = pos.Next;
            }
            StepCrabCups(circularBuffer, valueToCup, 10_000_000);
            pos = circularBuffer.Find(1);
            var  p   = pos.NextOrFirst();
            var  pp  = p.NextOrFirst();
            long ans = p.Value * (long)pp.Value;

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
        static Object PartA()
        {
            var   input = ReadIndata.Strings(inputPath);
            Regex re    = new Regex(@"(e|se|sw|w|nw|ne)");

            foreach (var s in input)
            {
                var pos = new Position();
                foreach (Match match in re.Matches(s))
                {
                    pos += CoordsHex.directionsWide[match.Value];
                }
                if (black.Contains(pos))
                {
                    black.Remove(pos);
                }
                else
                {
                    black.Add(pos);
                }
            }
            int ans = black.Count();

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #6
0
        static Object PartB()
        {
            var      input = ReadIndata.Strings(inputPath);
            Position wpos  = new Position(10, 1);
            Position spos  = new Position();

            foreach (var s in input)
            {
                char c = s[0];
                int  n = int.Parse(s.Substring(1));
                if (c == 'F')
                {
                    spos += wpos * n;
                }
                else if (c == 'L')
                {
                    wpos = Position.Rotate4Steps(wpos, -n / 90);
                }
                else if (c == 'R')
                {
                    wpos = Position.Rotate4Steps(wpos, n / 90);
                }
                else
                {
                    wpos += CoordsXY.directions4[cardinals.IndexOf(c)] * n;
                }
            }
            int ans = spos.ManhattanDistance();

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #7
0
        static Object PartA()
        {
            var    input = ReadIndata.Strings(inputPath);
            string mask  = "";
            var    mem   = new Dictionary <int, long>();

            foreach (var s in input)
            {
                var t = s.Split("[] =".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (t.Contains("mask"))
                {
                    mask = t[1];
                }
                else
                {
                    var a = Convert.ToString(long.Parse(t[2]), 2).PadLeft(bits, '0');
                    var b = "";
                    for (int i = 0; i < bits; i++)
                    {
                        b += mask[i] == 'X' ? a[i] : mask[i];
                    }
                    mem[int.Parse(t[1])] = Convert.ToInt64(b, 2);
                }
            }
            long ans = mem.Select(x => x.Value).Sum();

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
        static Object PartA()
        {
            var       input = ReadIndata.Longs(inputPath);
            long      ans   = 0;
            bool      found = true;
            const int n     = 25;
            int       t     = n;

            while (found)
            {
                found = false;
                ans   = input[t];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if ((i != j) && (input[t - n + i] + input[t - n + j] == ans))
                        {
                            found = true;
                        }
                    }
                }
                t++;
            }
            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #9
0
        static Object PartB()
        {
            List <long> input = ReadIndata.Longs(inputPath);

            input.Sort();
            long ans = 0;

            for (int i = 0; i < input.Count; i++)
            {
                for (int j = 0; j < input.Count; j++)
                {
                    if (j != i)
                    {
                        long a = input[i];
                        long b = input[j];
                        long c = 2020 - a - b;
                        if (input.Contains(c))
                        {
                            ans = a * b * c;
                            break;
                        }
                    }
                }
            }
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #10
0
        static Object PartB()
        {
            var        input = ReadIndata.Longs(inputPath);
            long       ans   = 0;
            const long tgt   = 167829540;
            int        t     = 0;

            while (ans <= 0)
            {
                long sum = 0;
                int  n   = 0;
                for (n = 0; n < 2 || sum < tgt; n++)
                {
                    sum += input[t + n];
                }
                if (sum == tgt)
                {
                    var r = input.Skip(t).Take(n).ToList();
                    ans = r.Min() + r.Max();
                }
                t++;
            }
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #11
0
        static Object PartB()
        {
            List <string>          input = ReadIndata.Strings(inputPath);
            int                    ans   = 0;
            Dictionary <char, int> dict  = new Dictionary <char, int>();
            int                    n     = 0;

            foreach (string s in input)
            {
                if (s == "")
                {
                    ans += dict.Where(x => x.Value == n).Count();
                    dict = new Dictionary <char, int>();
                    n    = 0;
                }
                else
                {
                    foreach (char c in s)
                    {
                        if (!dict.ContainsKey(c))
                        {
                            dict[c] = 0;
                        }
                        dict[c]++;
                    }
                    n++;
                }
            }
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #12
0
        static Object PartB()
        {
            var input    = ReadIndata.Strings(inputPath);
            var computer = new Computer(input);

            computer.Run();
            int ans = 0;

            foreach (int offs in computer.orderVisited)
            {
                var c = new Computer(computer.source);
                var i = c.program[offs];
                var n = i.opCode.name;
                if (n != "acc")
                {
                    i.opCode        = c.instructionSet[n == "jmp" ? "nop" : "jmp"];
                    c.program[offs] = i;
                    if (c.Run())
                    {
                        ans = c.acc;
                        break;
                    }
                }
            }
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #13
0
        static Object PartB()
        {
            var input = ReadIndata.Ints(inputPath);
            int ans   = MemoryGame(input, 30000000);

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #14
0
        static Object PartA()
        {
            var input = ReadIndata.Ints(inputPath);
            int ans   = 0;

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #15
0
        static Object PartA()
        {
            List <string> input = ReadIndata.Strings(inputPath);
            HashSet <int> ids   = GetIds(input);
            int           ans   = ids.Max();

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #16
0
        static Object PartB()
        {
            List <string> input = ReadIndata.Strings(inputPath);
            HashSet <int> ids   = GetIds(input);
            int           ans   = ids.Where(x => !ids.Contains(x + 1) && ids.Contains(x + 2)).Min() + 1;

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #17
0
        static Object PartA()
        {
            var input = ReadIndata.Strings(inputPath);
            var c     = new Computer(input);

            c.Run();
            var ans = c.acc;

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #18
0
        static Object PartB()
        {
            List <string> input = ReadIndata.Strings(inputPath);
            long          ans   = CalcTrees(input, 1, 1);

            ans *= CalcTrees(input, 1, 3);
            ans *= CalcTrees(input, 1, 5);
            ans *= CalcTrees(input, 1, 7);
            ans *= CalcTrees(input, 2, 1);
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #19
0
        static Object PartA()
        {
            var input        = ReadIndata.Strings(inputPath);
            int cardPk       = int.Parse(input[0]);
            int doorPk       = int.Parse(input[1]);
            int cardLoopSize = CalcLoopSize(7, cardPk);
            //int doorLoopSize = CalcLoopSize(7, doorPk);
            long ans = Calc(doorPk, cardLoopSize);

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #20
0
        static Object PartB()
        {
            var  input = ReadIndata.Strings(inputPath);
            long ans   = 0;

            foreach (string s in input)
            {
                ans += Calc(s, true);
            }
            //foreach (string s in input)
            //    ans += Calc(s, EvaluateB);
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #21
0
        // Day 22: Crab Combat - Play card game, first simple, then with recursion and more

        static List <int> ReadInput(string path)
        {
            var strs = ReadIndata.Strings(path);
            var list = new List <int>();

            foreach (var line in strs)
            {
                if (line.Length > 0 && char.IsDigit(line[0]))
                {
                    list.Add(int.Parse(line));
                }
            }
            return(list);
        }
Exemple #22
0
        static void ReadInput(string path)
        {
            var input = ReadIndata.Strings(path);

            void AddFoodToItem(Dictionary <string, HashSet <int> > col, string key, int f)
            {
                if (!col.ContainsKey(key))
                {
                    col[key] = new HashSet <int>();
                }
                col[key].Add(f);
            }

            void AddItemToFood(Dictionary <int, HashSet <string> > col, int f, string item)
            {
                if (!col.ContainsKey(f))
                {
                    col[f] = new HashSet <string>();
                }
                col[f].Add(item);
            }

            int food = 0;

            foreach (string s in input)
            {
                var v  = s.Split("(contains");
                var iv = v[0].Split(" ", StringSplitOptions.RemoveEmptyEntries);
                var av = v[1].Split(" ,)".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                {
                    foreach (string ingr in iv)
                    {
                        ingredients.Add(ingr);
                        AddItemToFood(foodIngredients, food, ingr);
                        AddFoodToItem(foodsWithIngredient, ingr, food);
                    }
                }
                {
                    foreach (string aller in av)
                    {
                        allergenes.Add(aller);
                        AddItemToFood(foodAllergenes, food, aller);
                        AddFoodToItem(foodsWithAllergen, aller, food);
                    }
                }
                food++;
            }
        }
Exemple #23
0
        // Day 03: Toboggan Trajectory - Check occurrences in 2D character map

        static Object PartA()
        {
            List <string> input = ReadIndata.Strings(inputPath);
            int           ans   = 0;
            int           c     = 0;

            for (int r = 0; r < input.Count; r++)
            {
                if (input[r][c % input[0].Count()] == '#')
                {
                    ans++;
                }
                c += 3;
            }
            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #24
0
        static Object PartA()
        {
            var input = ReadIndata.Ints(inputPath);

            input.Add(0);
            input.Add(input.Max() + 3);
            input.Sort();
            var d = new int[] { 0, 0, 0, 0 };

            for (int i = 1; i < input.Count; i++)
            {
                d[input[i] - input[i - 1]]++;
            }
            int ans = d[1] * d[3];

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #25
0
        static Object PartB()
        {
            var input = ReadIndata.Strings(inputPath);
            Map m     = Map.Build(input);
            Map m2    = new Map(m);
            int n     = 0;

            do
            {
                m  = m2;
                m2 = StepMapB(m);
                n++;
            }while (m != m2);
            int ans = m.data.Cast <char>().Where(x => x == '#').Count();

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #26
0
        static Object PartA()
        {
            List <int> input = ReadIndata.Ints(inputPath);

            input.Sort();
            int ans = 0;

            foreach (int a in input)
            {
                int b = 2020 - a;
                if (input.Contains(b))
                {
                    ans = a * b;
                    break;
                }
            }
            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #27
0
        static Object PartB()
        {
            var    input = ReadIndata.Strings(inputPath);
            string mask  = "";
            var    mem   = new Dictionary <long, long>();

            foreach (var s in input)
            {
                var t = s.Split("[] =".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (t.Contains("mask"))
                {
                    mask = t[1];
                }
                else
                {
                    var a    = Convert.ToString(long.Parse(t[1]), 2).PadLeft(bits, '0');
                    var b    = "";
                    var offs = new List <long>()
                    {
                        0
                    };
                    for (int i = 0; i < bits; i++)
                    {
                        var m = mask[i];
                        if (m == 'X')
                        {
                            offs.Add(1L << bits - 1 - i);
                        }
                        b += m == 'X' ? '0' : (m == '1' ? '1' : a[i]);
                    }
                    var membase = Convert.ToInt64(b, 2);
                    var value   = long.Parse(t[2]);
                    foreach (var z in Algorithms.GetCombinations(offs))
                    {
                        mem[membase + z.Sum()] = value;
                    }
                }
            }
            long ans = mem.Select(x => x.Value).Sum();

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #28
0
        // Day 13: Shuttle Search - Modular arithmetic & The Chinese Remainder Theorem (CRT)

        static Object PartA()
        {
            var        input   = ReadIndata.Strings(inputPath);
            List <int> ids     = input[1].Split(',').Where(x => x != "x").Select(x => int.Parse(x)).ToList();
            int        minutes = int.Parse(input[0]);
            int        min     = int.MaxValue;
            int        ans     = 0;

            foreach (int i in ids)
            {
                int a = i - (minutes % i);
                if (a < min)
                {
                    min = a;
                    ans = a * i;
                }
            }
            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Exemple #29
0
        static Object PartB()
        {
            var        input = ReadIndata.Strings(inputPath);
            List <int> ids   = new List <int>();
            List <int> offs  = new List <int>();
            int        i     = 0;

            foreach (string s in input[1].Split(','))
            {
                if (s != "x")
                {
                    int id = int.Parse(s);
                    ids.Add(id);
                    offs.Add(id - i);
                }
                i++;
            }
            long ans = Utils.CRT(ids, offs);

            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Exemple #30
0
        // Day 06: Custom Customs - Parse groups and questions, perform some and/or logic

        static Object PartA()
        {
            List <string>  input = ReadIndata.Strings(inputPath);
            int            ans   = 0;
            HashSet <char> set   = new HashSet <char>();

            foreach (string s in input)
            {
                if (s == "")
                {
                    ans += set.Count;
                    set  = new HashSet <char>();
                }
                else
                {
                    set.UnionWith(s.ToHashSet());
                }
            }
            ans += set.Count;
            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }