Esempio n. 1
0
        static int CalculateAlignmentParametersSum(OurMap m)
        {
            Position           curPos        = m.mapPos.Where(x => (x.Value != '.') && (x.Value != '#')).First().Key;
            Position           dir           = FindDirection(m, curPos, '#');
            HashSet <Position> visited       = new HashSet <Position>();
            List <Position>    intersections = new List <Position>();
            bool done = false;

            while (!done)
            {
                if (visited.Contains(curPos))
                {
                    intersections.Add(curPos);
                }
                visited.Add(curPos);
                List <Position> nextMoves = new List <Position>()
                {
                    dir
                };
                int dirIndex = directions.IndexOf(dir);
                nextMoves.Add(directions[(dirIndex - 1 + 4) % 4]);
                nextMoves.Add(directions[(dirIndex + 1) % 4]);
                done = true;
                for (int i = 0; i < nextMoves.Count && done; i++)
                {
                    if (MapPositionHasValue(m, curPos + nextMoves[i], '#'))
                    {
                        dir  = nextMoves[i];
                        done = false;
                    }
                }
                curPos += dir;
            }
            return(intersections.Select(a => a.x * a.y).Sum());
        }
Esempio n. 2
0
        static void IterateMap3D(ref OurMap m)
        {
            OurMap next = new OurMap(m);
            int    z0   = m.mapPos.Min(w => w.Key.z) - 1;
            int    z1   = m.mapPos.Max(w => w.Key.z) + 1;

            for (int z = z0; z <= z1; z++)
            {
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                    {
                        Position p = new Position(x, y, z);
                        int      n = CalculateNeighbours3D(m, p);
                        if (n != 1 && m.mapPos.ContainsKey(p) && m.mapPos[p] == '#')
                        {
                            next.mapPos[p] = '.';
                        }
                        if ((n == 1 || n == 2) && (!m.mapPos.ContainsKey(p) || m.mapPos[p] == '.'))
                        {
                            next.mapPos[p] = '#';
                        }
                    }
                }
            }
            m = next;
        }
Esempio n. 3
0
        static int CalculateOxygenFillMinutes(OurMap m)
        {
            Dictionary <Position, int> steps    = new Dictionary <Position, int>();
            List <Position>            moveFrom = new List <Position>();
            Position p1 = m.mapPos.Where(x => x.Value == 2).First().Key;

            moveFrom.Add(p1);
            steps[p1] = 0;
            do
            {
                List <Position> nextMoveFrom = new List <Position>();
                foreach (Position p in moveFrom)
                {
                    for (int i = 0; i < directions.Count; i++)
                    {
                        Position newPos = p + directions[i];
                        if (m.mapPos[newPos] > 0)
                        {
                            int newSteps = steps[p] + 1;
                            if (!steps.ContainsKey(newPos) || steps[newPos] > newSteps)
                            {
                                nextMoveFrom.Add(newPos);
                                steps[newPos] = newSteps;
                            }
                        }
                    }
                }
                moveFrom = nextMoveFrom;
            }while (moveFrom.Count > 0);
            return(steps.Max(x => x.Value));
        }
Esempio n. 4
0
        static int IterateMap2D(ref OurMap m)
        {
            int    id   = 0;
            OurMap next = new OurMap(m);

            for (int y = 4; y >= 0; y--)
            {
                for (int x = 4; x >= 0; x--)
                {
                    id <<= 1;
                    Position p = new Position(x, y, 0);
                    int      n = CalculateNeighbours2D(m, p);
                    if (n != 1 && m.mapPos[p] == '#')
                    {
                        next.mapPos[p] = '.';
                    }
                    if ((n == 1 || n == 2) && m.mapPos[p] == '.')
                    {
                        next.mapPos[p] = '#';
                    }
                    if (next.mapPos[p] == '#')
                    {
                        id |= 1;
                    }
                }
            }
            m = next;
            return(id);
        }
Esempio n. 5
0
        static Object PartB()
        {
            List <long> input = ReadInput();
            IntComputer c1    = new IntComputer(input, 0);
            PosComputer p1    = new PosComputer(c1);
            OurMap      m     = BuildMap(p1, false);
            int         b     = CalculateOxygenFillMinutes(m);

            Console.WriteLine("Part B: Result is {0}", b);
            return(b);
        }
Esempio n. 6
0
        static bool PartB(Object correctAnswer = null)
        {
            List <long> input = ReadInput();
            IntComputer c1    = new IntComputer(input, 0);
            PosComputer p1    = new PosComputer(c1);
            OurMap      m     = BuildMap(p1, false);
            int         b     = CalculateOxygenFillMinutes(m);

            Console.WriteLine("Part B: Result is {0}", b);
            return(correctAnswer == null || b == (int)correctAnswer);
        }
Esempio n. 7
0
 static Position FindDirection(OurMap m, Position pos, int value)
 {
     foreach (Position p in directions)
     {
         Position test = pos + p;
         if (MapPositionHasValue(m, test, value))
         {
             return(p);
         }
     }
     throw new ArgumentOutOfRangeException();
 }
Esempio n. 8
0
        static Object PartA()
        {
            List <long> input = ReadInput();
            IntComputer c0    = new IntComputer(input, 0);
            OurMap      m     = BuildMap(c0, 50);

            m.PrintMap();
            int a = m.mapPos.Sum(x => x.Value);

            Console.WriteLine("Part A: Result is {0}", a);
            return(a);
        }
Esempio n. 9
0
        static bool PartA(Object correctAnswer = null)
        {
            List <long> input = ReadInput();
            IntComputer c0    = new IntComputer(input, 0);
            OurMap      m     = BuildMap(c0, 50);

            m.PrintMap();
            int a = m.mapPos.Sum(x => x.Value);

            Console.WriteLine("Part A: Result is {0}", a);
            return(correctAnswer == null || a == (int)correctAnswer);
        }
Esempio n. 10
0
        static bool PartB(Object correctAnswer = null)
        {
            List <long> input = ReadInput();
            IntComputer c0    = new IntComputer(input, 0);
            OurMap      m     = BuildMap(c0, 50);
            int         dy    = 49;
            int         dx    = m.mapPos.Where(g => g.Key.y == dy && g.Value == 1).Min(g => g.Key.x);
            Position    p     = CalculatePosition(c0, dx - 1, dy);
            int         b     = p.y + p.x * 10000;

            Console.WriteLine("Part B: Result is {0}", b);
            return(correctAnswer == null || b == (int)correctAnswer);
        }
Esempio n. 11
0
        static bool PartA(Object correctAnswer = null)
        {
            List <long> input = ReadInput();
            IntComputer c1    = new IntComputer(input, 0);
            OurMap      m     = BuildMap(c1);

            Console.SetCursorPosition(0, 1);
            m.PrintMap();
            int ans = CalculateAlignmentParametersSum(m);

            Console.WriteLine("Part A: Result is {0}", ans);
            //BuildMovementSequence(m);
            return(correctAnswer == null || ans == (int)correctAnswer);
        }
Esempio n. 12
0
        static Object PartA()
        {
            List <long> input = ReadInput();
            IntComputer c1    = new IntComputer(input, 0);
            PosComputer p1    = new PosComputer(c1);
            OurMap      m     = BuildMap(p1, true);

            Console.SetCursorPosition(0, 1);
            m.PrintMap();
            int ans = CalculateStepsToTreasure(m);

            Console.WriteLine("Part A: Result is {0}", ans);
            return(ans);
        }
Esempio n. 13
0
        // --- Part A, Bits version
        static OurMap BuildMap(int rating)
        {
            OurMap m = new OurMap();

            for (int y = 0; y < 5; y++)
            {
                for (int x = 0; x < 5; x++)
                {
                    char c = RatingHasBug(rating, x, y) ? '#' : '.';
                    m.mapPos[new Position(x, y, 0)] = c;
                }
            }
            return(m);
        }
Esempio n. 14
0
        static int CalculateNeighbours2D(OurMap m, Position p)
        {
            int n = 0;

            foreach (Position d in neighbours)
            {
                Position q = p + d;
                if (m.mapPos.ContainsKey(q) && m.mapPos[q] == '#')
                {
                    n++;
                }
            }
            return(n);
        }
Esempio n. 15
0
        // --- Part A
        static OurMap BuildMap(List <string> list)
        {
            int    w = list[0].Length;
            int    h = list.Count;
            OurMap m = new OurMap();

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    char c = list[y][x];
                    m.mapPos[new Position(x, y, 0)] = c;
                }
            }
            return(m);
        }
Esempio n. 16
0
        static void BuildMovementSequence(OurMap m)
        {
            Position curPos = m.mapPos.Where(x => (x.Value != '.') && (x.Value != '#')).First().Key;
            int      idx    = initialDirections.IndexOf((char)m.mapPos[curPos]);
            Position dir    = directions[idx];
            string   mf     = "";
            bool     done   = false;
            int      nMoves = 0;

            while (!done)
            {
                List <Position> nextMoves = new List <Position>()
                {
                    dir
                };
                int dirIndex = directions.IndexOf(dir);
                nextMoves.Add(directions[(dirIndex - 1 + 4) % 4]);
                nextMoves.Add(directions[(dirIndex + 1) % 4]);
                done = true;
                for (int i = 0; i < nextMoves.Count && done; i++)
                {
                    if (MapPositionHasValue(m, curPos + nextMoves[i], '#'))
                    {
                        if (i == 0)
                        {
                            nMoves++;
                        }
                        else
                        {
                            if (nMoves > 0)
                            {
                                nMoves++;
                                mf    += nMoves.ToString() + ',';
                                nMoves = 0;
                            }
                            string s = (i == 1) ? "L" : "R";
                            mf += s + ',';
                        }
                        dir  = nextMoves[i];
                        done = false;
                    }
                }
                curPos += dir;
            }
            Console.WriteLine(mf);
        }
Esempio n. 17
0
        static Object PartA()
        {
            List <string> input   = ReadInput();
            OurMap        m       = BuildMap(input);
            HashSet <int> visited = new HashSet <int>();
            int           rating  = -1;

            while (!visited.Contains(rating))
            {
                //Console.SetCursorPosition(0, 1);
                //m.PrintMap();
                visited.Add(rating);
                rating = IterateMap2D(ref m);
            }
            Console.WriteLine("Part A: Result is {0}", rating);
            return(rating);
        }
Esempio n. 18
0
        static OurMap BuildMap(PosComputer c1, bool print)
        {
            OurMap             m         = new OurMap();
            List <PosComputer> computers = new List <PosComputer>();

            SetupComputerMovement(ref c1, 0);
            computers.Add(c1);
            do
            {
                List <PosComputer> newComputers = new List <PosComputer>();
                foreach (PosComputer c in computers)
                {
                    if (c.ic.Execute())
                    {
                        // 0 = Hit wall, 1 = Has moved, 2 = Has moved, found treasure
                        int res = (int)c.ic.reg;
                        m.mapPos[c.nextPos] = res;
                        if (res > 0)
                        {
                            c.curPos = c.nextPos;
                        }
                        // Movement command values: 1 = N, 2 = S, 3 = W, 4 = E
                        for (int i = 0; i < directions.Count; i++)
                        {
                            Position newPos = c.curPos + directions[i];
                            if (!m.mapPos.ContainsKey(newPos))
                            {
                                PosComputer x = new PosComputer(c);
                                SetupComputerMovement(ref x, i);
                                newComputers.Add(x);
                            }
                        }
                    }
                }
                computers = newComputers;
                if (print)
                {
                    Console.SetCursorPosition(0, 1);
                    m.PrintMap();
                }
            }while (computers.Count > 0);
            return(m);
        }
Esempio n. 19
0
        static OurMap BuildMap(IntComputer ic, int size)
        {
            OurMap   m      = new OurMap();
            Position curPos = new Position(0, 0);

            for (int y = 0; y < size; y++)
            {
                for (int x = 0; x < size; x++)
                {
                    IntComputer compu = new IntComputer(ic);
                    compu.reg = x;
                    compu.Execute();
                    compu.reg = y;
                    compu.Execute();
                    compu.Execute();
                    int a = (int)compu.reg;
                    m.mapPos[new Position(x, y)] = a;
                }
            }
            return(m);
        }
Esempio n. 20
0
        static OurMap BuildMap(IntComputer ic)
        {
            OurMap   m      = new OurMap();
            Position curPos = new Position(0, 0);

            while (ic.Execute() > 0)
            {
                int a = (int)ic.reg;
                if (a == '\n')
                {
                    curPos.x = 0;
                    curPos.y++;
                }
                else
                {
                    m.mapPos[curPos] = a;
                    curPos          += goRight;
                }
            }
            return(m);
        }
Esempio n. 21
0
        static int CalculateStepsToTreasure(OurMap m)
        {
            Dictionary <Position, int> steps    = new Dictionary <Position, int>();
            List <Position>            moveFrom = new List <Position>();
            Position p1 = new Position(0, 0);

            moveFrom.Add(p1);
            steps[p1] = 0;
            int minSteps = int.MaxValue;

            do
            {
                List <Position> nextMoveFrom = new List <Position>();
                foreach (Position p in moveFrom)
                {
                    if (m.mapPos[p] == 2)
                    {
                        if (steps[p] < minSteps)
                        {
                            minSteps = steps[p];
                        }
                    }
                    for (int i = 0; i < directions.Count; i++)
                    {
                        Position newPos = p + directions[i];
                        if (m.mapPos[newPos] > 0)
                        {
                            int newSteps = steps[p] + 1;
                            if (!steps.ContainsKey(newPos) || steps[newPos] > newSteps)
                            {
                                nextMoveFrom.Add(newPos);
                                steps[newPos] = newSteps;
                            }
                        }
                    }
                }
                moveFrom = nextMoveFrom;
            }while (moveFrom.Count > 0);
            return(minSteps);
        }
Esempio n. 22
0
        static Object PartB()
        {
            List <string> input = ReadInput();
            OurMap        m     = BuildMap(input);

            //m.PrintMap();
            for (int i = 0; i < 200; i++)
            {
                IterateMap3D(ref m);
            }
            //m.PrintMap();
            int ans = 0;

            foreach (var kvp in m.mapPos)
            {
                if (kvp.Value == '#' && !(kvp.Key.x == 2 && kvp.Key.y == 2))
                {
                    ans++;
                }
            }
            Console.WriteLine("Part B: Result is {0}", ans);
            return(ans);
        }
Esempio n. 23
0
 public OurMap(OurMap m)
 {
     mapPos = new Dictionary <Position, char>(m.mapPos);
 }
Esempio n. 24
0
 static bool MapPositionHasValue(OurMap m, Position pos, int value)
 {
     return(m.mapPos.ContainsKey(pos) && m.mapPos[pos] == value);
 }
Esempio n. 25
0
        static int CalculateNeighbours3D(OurMap m, Position p)
        {
            List <Position> n = new List <Position>();

            // left neighbour(s)
            if (p.x == 0)
            {
                n.Add(new Position(1, 2, p.z - 1));
            }
            else if (p.x == 3 && p.y == 2)
            {
                for (int i = 0; i < 5; i++)
                {
                    n.Add(new Position(4, i, p.z + 1));
                }
            }
            else
            {
                n.Add(p + goLeft);
            }

            // right neighbour(s)
            if (p.x == 4)
            {
                n.Add(new Position(3, 2, p.z - 1));
            }
            else if (p.x == 1 && p.y == 2)
            {
                for (int i = 0; i < 5; i++)
                {
                    n.Add(new Position(0, i, p.z + 1));
                }
            }
            else
            {
                n.Add(p + goRight);
            }

            // up neighbour(s)
            if (p.y == 0)
            {
                n.Add(new Position(2, 1, p.z - 1));
            }
            else if (p.y == 3 && p.x == 2)
            {
                for (int i = 0; i < 5; i++)
                {
                    n.Add(new Position(i, 4, p.z + 1));
                }
            }
            else
            {
                n.Add(p + goUp);
            }

            // down neighbour(s)
            if (p.y == 4)
            {
                n.Add(new Position(2, 3, p.z - 1));
            }
            else if (p.y == 1 && p.x == 2)
            {
                for (int i = 0; i < 5; i++)
                {
                    n.Add(new Position(i, 0, p.z + 1));
                }
            }
            else
            {
                n.Add(p + goDown);
            }

            return(n.Where(w => m.mapPos.ContainsKey(w) && m.mapPos[w] == '#').Count());
        }