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); }
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); }
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); }
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); }
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); }
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); }
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); }
GridRules LoadRules(string datafile) { var rules = new GridRules(); FileIterator.ForEachLine <string>(datafile, line => { ParseRule(rules, line); }); return(rules); }
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])));
public void Solution01() { var count = 0; FileIterator.ForEachLine <string>("data/0401.txt", line => { if (IsValid(line)) { count++; } }); count.Should().Be(466); }
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()); }
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); }
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); }
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); }
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); }
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)); }
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); }