Exemplo n.º 1
0
        void ReadMap(String In)
        {
            Roids = new List <Roid>();
            float x = 0.0f;
            float y = 0.0f;

            foreach (char c in In)
            {
                switch (c)
                {
                case '#':
                    Roid r = new Roid();
                    r.x = x;
                    r.y = y;
                    Roids.Add(r);
                    x += 1.0f;
                    break;

                case '.':
                    x += 1.0f;
                    break;

                case '\n':
                    y += 1.0f;
                    x  = 0.0f;
                    break;

                default:
                    break;
                }
            }
        }
Exemplo n.º 2
0
        Roid Target(float angle, bool AngleMustBeLarger)
        {
            float mina    = float.MaxValue;
            float mindist = float.MaxValue;
            Roid  tr      = null;

            foreach (Roid r in Roids)
            {
                if (r == Station)
                {
                    continue;
                }

                float a = r.a - angle;
                if (a < 0)
                {
                    a += 2.0f * (float)Math.PI;
                }
                if (AngleMustBeLarger && a <= 0.000001f)
                {
                    continue;
                }
                if (a < mina || (a == mina && r.d < mindist))
                {
                    mina    = a;
                    mindist = r.d;
                    tr      = r;
                }
            }

            return(tr);
        }
Exemplo n.º 3
0
        public void Part2()
        {
            CalcAngles();

            float angle = 0.0f;
            Roid  r     = Target(angle, false);

            for (int a = 0; a < 199; a++)
            {
                angle = r.a;
                Roids.Remove(r);
                r = Target(angle, true);
            }

            Console.WriteLine("Day10 Part2 Result = {0}", r.x * 100 + r.y);
        }
Exemplo n.º 4
0
        public void Part1()
        {
            ReadMap(InputData);
            CalcCounts();

            int maxvis = 0;

            foreach (Roid r in Roids)
            {
                if (r.v > maxvis)
                {
                    maxvis  = r.v;
                    Station = r;
                }
            }

            Console.WriteLine("Day10 Part1 Result = {0}", maxvis);
        }
Exemplo n.º 5
0
        bool CanSee(Roid r1, Roid r2)
        {
            foreach (Roid rblock in Roids)
            {
                if (rblock == r1 || rblock == r2)
                {
                    continue;
                }

                float dist2sq = (r2.x - r1.x) * (r2.x - r1.x) + (r2.y - r1.y) * (r2.y - r1.y);
                float distbsq = (rblock.x - r1.x) * (rblock.x - r1.x) + (rblock.y - r1.y) * (rblock.y - r1.y);
                if (distbsq <= dist2sq)
                {
                    float dotp = ((r2.x - r1.x) * (rblock.x - r1.x)) + ((r2.y - r1.y) * (rblock.y - r1.y));
                    if (Math.Abs(dotp - Math.Sqrt(dist2sq) * Math.Sqrt(distbsq)) < 0.000001)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }