Exemplo n.º 1
0
        private static uint BioDiversity(CharMap map)
        {
            var(min, max) = map.MinMax();
            var  width = max.Y - min.Y + 1;
            uint val   = 0;

            foreach (var pos in map.AllPoints(c => c == '#'))
            {
                var position = pos.Y * width + pos.X;
                val += 1U << position;
            }
            return(val);
        }
Exemplo n.º 2
0
        private static string[] CalculatePath(CharMap map)
        {
            var pos       = map.AllPoints(ch => "^v<>".Contains(ch)).First();
            var vc        = map[pos];
            var direction =
                vc == '^' ? Direction.Up :
                vc == '>' ? Direction.Right :
                vc == 'v' ? Direction.Down :
                vc == '<' ? Direction.Left : 0;                 // 0 can't happen

            var path    = new List <string>();
            var stretch = 0;

            while (true)
            {
                var originalDirection = direction;
                if (map[pos.Move(direction)] == '#')
                {
                    pos = pos.Move(direction);
                    stretch++;
                    continue;
                }
                if (stretch > 0)
                {
                    path.Add(stretch.ToString());
                    stretch = 0;
                }
                if (map[pos.Move(direction.TurnLeft())] == '#')
                {
                    direction = direction.TurnLeft();
                    path.Add("L");
                }
                else if (map[pos.Move(direction.TurnRight())] == '#')
                {
                    direction = direction.TurnRight();
                    path.Add("R");
                }
                else
                {
                    return(path.ToArray());
                }
                //Console.WriteLine(path);
            }
        }
Exemplo n.º 3
0
        protected override int Part1(string[] input)
        {
            var map = new CharMap();

            var lines = input;

            for (var y = 0; y < lines.Length; y++)
            {
                var line = lines[y];
                for (var x = 0; x < line.Length; x++)
                {
                    map[x][y] = line[x];
                }
            }

            var seen = new HashSet <uint>();

            while (true)
            {
                //map.ConsoleWrite(false);
                ////Console.ReadKey();

                var bio = BioDiversity(map);
                if (seen.Contains(bio))
                {
                    return((int)bio);
                }
                seen.Add(bio);
                var nextmap = new CharMap();
                foreach (var pos in map.AllPoints().ToArray())                 // ToArray should not be needed?
                {
                    var n = pos.LookAround().Count(p => map[p] == '#');
                    nextmap[pos] = map[pos] == '#'
                                                ? n == 1 ? '#' : '.'
                                                : n == 1 || n == 2 ? '#' : '.';
                }
                map = nextmap;
            }

            throw new Exception("No result found");
        }