예제 #1
0
        static List <Passport> LoadPassports(string input)
        {
            var passports       = new List <Passport>();
            var currentPassport = new Passport();

            passports.Add(currentPassport);

            FileIterator.ForEachLine(input, (string details) =>
            {
                if (details == "")
                {
                    currentPassport = new();
                    passports.Add(currentPassport);
                    return;
                }

                var fields = details.Split(' ');
                foreach (var field in fields)
                {
                    StoreField(currentPassport, field);
                }
            });

            return(passports);
        }
예제 #2
0
        int[] ParseLayers(string filename)
        {
            var intermediate = new List <Tuple <int, int> >();

            FileIterator.ForEachLine <string>(filename, line =>
            {
                var spec = line.Split(new char[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (spec.Length != 2)
                {
                    Oh.ForFucksSake();
                }

                var range = int.Parse(spec[0]);
                var depth = int.Parse(spec[1]);
                intermediate.Add(new Tuple <int, int>(range, depth));
            });

            var r = new int[intermediate.Last().Item1 + 1];

            foreach (var t in intermediate)
            {
                r[t.Item1] = t.Item2;
            }

            return(r);
        }
예제 #3
0
        static List <Answers> LoadAnswers(string input)
        {
            var answers      = new List <Answers>();
            var currentGroup = new Answers();

            answers.Add(currentGroup);

            FileIterator.ForEachLine(input, (string line) =>
            {
                if (line == "")
                {
                    currentGroup = new Answers();
                    answers.Add(currentGroup);
                    return;
                }

                currentGroup.Increment(GROUP_SIZE);
                foreach (var c in line)
                {
                    currentGroup.Increment(c);
                }
            });

            return(answers);
        }
예제 #4
0
        Dictionary <int, List <Connector> > LoadConnectors(string datafile)
        {
            var id         = 0;
            var connectors = new Dictionary <int, List <Connector> >();

            FileIterator.ForEachLine(datafile, (string line) =>
            {
                var sockets = line.Split('/');
                var input   = int.Parse(sockets[0]);
                var output  = int.Parse(sockets[1]);

                id++;
                var connector = new Connector(id, input, output);
                var outputs   = connectors.GetOrCreate(input, () => new List <Connector>());
                outputs.Add(connector);

                if (connector.Input != connector.Output)
                {
                    connector = new Connector(id, output, input);
                    outputs   = connectors.GetOrCreate(output, () => new List <Connector>());
                    outputs.Add(connector);
                }
            });

            return(connectors);
        }
예제 #5
0
        static (int[] counts, int threshold) ParseCountsThreshold(string inputFilename)
        {
            int[] counts    = null;
            var   threshold = 0;

            FileIterator.ForEachLine <string>(inputFilename, line => {
                if (counts == null)
                {
                    counts = new int[line.Length];
                }

                for (var i = 0; i < line.Length; i++)
                {
                    if (line[i] == '1')
                    {
                        counts[i]++;
                    }
                }
                threshold++;
            });

            threshold /= 2;

            return(counts, threshold);
        }
예제 #6
0
        int Solve2(string datafile)
        {
            var particles = new Dictionary <int, Particle>();
            int id        = 0;

            FileIterator.ForEachLine <string>(datafile, line =>
            {
                particles[id] = ParseParticle(id, line);
                id++;
            });

            for (var i = 0; i < 39; i++)
            {
                var ccache = CreateCollisionCache();
                foreach (var part in particles.Values)
                {
                    part.Update();
                    ccache.GetOrCreate(part.PositionId, CreateParticleList).Add(part.Id);
                }

                foreach (var collisions in ccache.Values)
                {
                    if (collisions.Count >= 2)
                    {
                        foreach (var partId in collisions)
                        {
                            particles.Remove(partId);
                        }
                    }
                }
            }

            return(particles.Count);
        }
예제 #7
0
        Dictionary <ulong, NodeState> LoadGrid(string datafile, out int startX, out int startY)
        {
            var grid = new Dictionary <ulong, NodeState>();
            var x    = 0;
            var y    = 0;

            FileIterator.ForEachLine <string>(datafile, line =>
            {
                x = 0;
                foreach (var c in line)
                {
                    if (c == '#')
                    {
                        grid[EncodeCoords(x, y)] = NodeState.Infected;
                    }
                    x++;
                }
                y++;
            });

            startX = x / 2;
            startY = y / 2;

            return(grid);
        }
예제 #8
0
        GridRules LoadRules(string datafile)
        {
            var rules = new GridRules();

            FileIterator.ForEachLine <string>(datafile, line =>
            {
                ParseRule(rules, line);
            });

            return(rules);
        }
예제 #9
0
        static Move[] Parse(string inputFile)
        {
            List <Move> moves = new();

            FileIterator.ForEachLine <string>(inputFile, line => {
                var parts = line.Split(" ");
                moves.Add(new (parts[0] switch
                {
                    "forward" => Direction.Forward,
                    "up" => Direction.Up,
                    "down" => Direction.Down,
                    _ => throw new InvalidOperationException()
                }, int.Parse(parts[1])));
예제 #10
0
        public void Solution01()
        {
            var count = 0;

            FileIterator.ForEachLine <string>("data/0401.txt", line =>
            {
                if (IsValid(line))
                {
                    count++;
                }
            });

            count.Should().Be(466);
        }
예제 #11
0
        static Entry[] LoadEntries(string filename)
        {
            List <Entry> entries = new();

            FileIterator.ForEachLine <string>(filename, line =>
            {
                var parts   = line.Split(" | ");
                var init    = parts[0].Split(" ").Select(s => StringSort(s)).ToArray();
                var display = parts[1].Split(" ").Select(s => StringSort(s)).ToArray();

                entries.Add(new(init, display));
            });

            return(entries.ToArray());
        }
예제 #12
0
        public void Problem(string input, int answer1, int answer2)
        {
            var orbitsDef = new Dictionary <string, string>();

            FileIterator.ForEachLine <string>(input, line =>
            {
                var pair           = line.Split(')');
                orbitsDef[pair[1]] = pair[0];
            });

            var orbits = BuildOrbits(orbitsDef);

            CalculateNodeDepth(orbits).Should().Be(answer1);
            PathLength(orbits).Should().Be(answer2);
        }
예제 #13
0
        public void Problem(string input, int answer1, int answer2)
        {
            var pathId = 1;
            var grid   = new Grid();

            FileIterator.ForEachLine <string>(input, path => PaintPath(grid, path, pathId++));

            var intersections = grid.Keys.Where(k => grid[k].pathId == INTERSECTION);
            var nearest       = intersections.Select(i => Math.Abs(i.Item1) + Math.Abs(i.Item2)).Min();

            nearest.Should().Be(answer1);

            var shortest = intersections.Select(i => grid[i].time).Min();

            shortest.Should().Be(answer2);
        }
예제 #14
0
        Graph <int> BuildGraph(string datafile)
        {
            var graph = new Graph <int>();

            FileIterator.ForEachLine <string>(datafile, line =>
            {
                var tokens = line.Replace(",", "").Split(' ');
                var id     = int.Parse(tokens[0]);
                graph.AddNodeIfNotInGraph(id);

                for (var i = 2; i < tokens.Length; i++)
                {
                    var linkId = int.Parse(tokens[i]);
                    graph.AddNodeIfNotInGraph(linkId);
                    graph.AddTwoWayLink(id, linkId);
                }
            });

            return(graph);
        }
예제 #15
0
        public void Problem2()
        {
            var seats = new bool[128 * 8];

            FileIterator.ForEachLine("Data/Day05.txt", (string line) => seats[ParseSeatId(line)] = true);

            var searching = false;
            var seatId    = 0;

            foreach (var taken in seats)
            {
                if (searching && !taken)
                {
                    break;
                }
                searching = taken;
                seatId++;
            }

            seatId.Should().Be(629);
        }
예제 #16
0
        string Solve(string datafile, out int numSteps)
        {
            var map = new Map();

            FileIterator.ForEachLine <string>(datafile, line =>
            {
                map.Add(line.ToCharArray());
            });

            var x = -1;

            // Find entry
            for (var i = 0; i < map[0].Length; i++)
            {
                if (map.GetCell(i, 0) == '|')
                {
                    x = i;
                    break;
                }
            }

            return(FollowPath(map, x, out numSteps));
        }
예제 #17
0
        int Solve1(string datafile)
        {
            var particles = new List <Particle>();
            int id        = 0;

            FileIterator.ForEachLine <string>(datafile, line =>
            {
                particles.Add(ParseParticle(id++, line));
            });

            var lowestAcc      = particles[0].Acc.DistanceFrom0Sqrd();
            var lowestParticle = particles[0];

            foreach (var part in particles)
            {
                if (part.Acc.DistanceFrom0Sqrd() < lowestAcc)
                {
                    lowestAcc      = part.Acc.DistanceFrom0Sqrd();
                    lowestParticle = part;
                }
            }

            return(lowestParticle.Id);
        }