private static void part2() { var input = InputFetcher.GetInputAsLines(InputFetcher.day7p1); foreach (var item in input) { string definitionSplitter = "bags contain"; var inputsplit = item.Split(definitionSplitter); var name = inputsplit[0].Substring(0, inputsplit[0].Length).Trim(); var children = inputsplit[1].Split(','); var childDefs = new Dictionary <string, int>(); foreach (var child in children) { var childef = child.Trim().Split(' '); int number = 0; if (child == " no other bags.") { break; } number = int.Parse(childef[0]); var desc = string.Join(' ', childef[1], childef[2]); childDefs.Add(desc, number); } BagRuleRepov2.Add(new Bag(name, childDefs)); } List <string> result = new List <string>(); var a = BagRuleRepov2.Rules; int resultcount = 0; CountRequiredNumberOfBags(BagRuleRepov2.Rules.Single(b => b.Name == "shiny gold"), ref resultcount); Console.WriteLine($"Total possible holders: {resultcount-1}"); }
private static void part1() { var input = InputFetcher.GetInputAsLines(InputFetcher.day7p1); foreach (var item in input) { string definitionSplitter = "bags contain"; var inputsplit = item.Split(definitionSplitter); var name = inputsplit[0].Substring(0, inputsplit[0].Length).Trim(); var children = inputsplit[1].Split(','); foreach (var child in children) { var childef = child.Trim().Split(' '); var number = childef[0]; var desc = string.Join(' ', childef[1], childef[2]); BagRuleRepo.Add(name, new[] { desc }); } } List <string> result = new List <string>(); AddHolderAndCheckIfIsChild("shiny gold", result); var total = result.Distinct(); Console.WriteLine($"Total possible holders: {total.Count()}"); }
private static void RunPart2() { var input = InputFetcher.GetInputAsLines(InputFetcher.day2p1); var inputs = input.Select(i => new PasswordInputP2(i)); var validInputs = inputs.Where(i => i.IsValid()); var invalidInputs = inputs.Where(i => !i.IsValid()); Console.WriteLine($"Part 2 - Read {input.Length} inputs. Valid: {validInputs.Count()}. Invalid; {invalidInputs.Count()}"); }
private static void part1() { var input = InputFetcher.GetInputAsLines(InputFetcher.day8p1); int pointer = 0; List <int> processedLines = new List <int>(); while (!processedLines.Contains(pointer)) { processedLines.Add(pointer); pointer += processLine(input[pointer]); } Console.WriteLine($"Accumulator currently at {accumulator}"); }
static void part1() { int answerCount = 0; var input = InputFetcher.GetInputAsString(InputFetcher.day6p1); var groups = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var group in groups) { var asnwers = string.Join(',', group.Replace("\r\n", "")); answerCount += asnwers.Distinct().Count(); } Console.WriteLine($"Unique answers: {answerCount}"); }
private static void part2() { var input = InputFetcher.GetInputAsLines(InputFetcher.day8p1); int itemcounter = 0; foreach (var item in input) { accumulator = 0; if (input[itemcounter].StartsWith("jmp")) { input[itemcounter] = input[itemcounter].Replace("jmp", "nop"); } else if (input[itemcounter].StartsWith("nop")) { input[itemcounter] = input[itemcounter].Replace("nop", "jmp"); } int pointer = 0; List <int> processedLines = new List <int>(); while (!processedLines.Contains(pointer)) { if (pointer == input.Length) { Console.WriteLine($"Completed program. Accumulator value: {accumulator}"); } processedLines.Add(pointer); pointer += processLine(input[pointer]); if (pointer == input.Length) { Console.WriteLine($"Completed program. Accumulator value: {accumulator}"); } } if (input[itemcounter].StartsWith("jmp")) { input[itemcounter] = input[itemcounter].Replace("jmp", "nop"); } else if (input[itemcounter].StartsWith("nop")) { input[itemcounter] = input[itemcounter].Replace("nop", "jmp"); } itemcounter++; } }
static void part2() { int answerCount = 0; var input = InputFetcher.GetInputAsString(InputFetcher.day6p1); var groups = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var group in groups) { var answers = group.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); List <List <char> > generated = answers.Select(l => l.Select(c => c).ToList()).ToList(); var intersection = generated.Aggregate((previousList, nextList) => previousList.Intersect(nextList).ToList()); answerCount += intersection.Select(c => c).Count(); } Console.WriteLine($"Unique answers: {answerCount}"); }
private static void RunPart2() { var validPassportCounter = 0; var input = InputFetcher.GetInputAsString(InputFetcher.day4p1); var passports = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var passport in passports) { var pass = new Passport(passport); if (pass.IsValid()) { validPassportCounter++; } } Console.WriteLine($"PArt 2: Number of valid passports: {validPassportCounter}"); }
/// <summary> /// Creates a new SolverData for the specified program arguments /// </summary> /// <param name="args">Program arguments</param> /// <exception cref="ArgumentException">If the <paramref name="args"/> are of the inappropriate length, or if the year cannot be parsed to an integer</exception> /// <exception cref="ArgumentNullException">If the day is null or empty</exception> public SolverData(string[] args) { if (args.Length is not ARGS) { throw new ArgumentException($"Arguments have invalid data, {args.Length} arguments when expected {ARGS}.", nameof(args)); } if (!int.TryParse(args[0], out this.year)) { throw new ArgumentException($"Year ({args[0]}) could not be parsed to integer.", $"{nameof(args)}[0]"); } if (!int.TryParse(args[1], out this.day)) { throw new ArgumentException($"Day ({args[1]}) could not be parsed to integer.", $"{nameof(args)}[1]"); } this.input = InputFetcher.EnsureInput(this.year, this.day); this.fullName = $"{QUALIFIER}{this.year}.Day{this.day:D2}"; }
private static void part1() { var input = InputFetcher.GetInputAsLines(InputFetcher.day5p1); var maxId = 0; foreach (var item in input) { var inputarray = item.Select(c => c).ToArray(); var currentMaxRow = 127; var currentMinRow = 0; var currentMaxSeat = 7; var currentMinSeat = 0; for (int i = 0; i < 10; i++) { var current = inputarray[i]; if (current == 'B') { currentMinRow += ((currentMaxRow - currentMinRow) / 2) + 1; } if (current == 'F') { currentMaxRow -= ((currentMaxRow - currentMinRow + 1) / 2); } if (current == 'R') { currentMinSeat += ((currentMaxSeat - currentMinSeat) / 2) + 1; } if (current == 'L') { currentMaxSeat -= ((currentMaxSeat - currentMinSeat + 1) / 2); } } int seatId = (currentMaxRow * 8) + currentMaxSeat; if (seatId > maxId) { maxId = seatId; } } Console.WriteLine($"max seat id: {maxId}"); }
private static void SolvePart2() { var originalInput = InputFetcher.GetInputAsLines(InputFetcher.day3p1); maxWidth = originalInput[0].Length; var possibleMoves = new[] { new { movex = 1, movey = 1 }, new { movex = 3, movey = 1 }, new { movex = 5, movey = 1 }, new { movex = 7, movey = 1 }, new { movex = 1, movey = 2 } }; foreach (var posmove in possibleMoves) { treesEncountered = 0; x = 0; y = 0; xMover = posmove.movex; yMover = posmove.movey; while (y < originalInput.Length - 1) { MoveForward(); var line = originalInput[y]; var chararray = line.ToCharArray(); var detected = chararray[x]; if (detected == treeIndicator) { treesEncountered++; } } treesEncounteredCol.Add(treesEncountered); } Console.WriteLine($"Trees: {treesEncounteredCol.Aggregate((uint)1,(x,y) => x*y)}. "); }
private static void part2() { var input = InputFetcher.GetInputAsLines(InputFetcher.day5p1); var maxId = 896; var possibleIds = Enumerable.Range(8, 888); foreach (var item in input) { var inputarray = item.Select(c => c).ToArray(); var currentMaxRow = 127; var currentMinRow = 0; var currentMaxSeat = 7; var currentMinSeat = 0; for (int i = 0; i < 10; i++) { var current = inputarray[i]; if (current == 'B') { currentMinRow += ((currentMaxRow - currentMinRow) / 2) + 1; } if (current == 'F') { currentMaxRow -= ((currentMaxRow - currentMinRow + 1) / 2); } if (current == 'R') { currentMinSeat += ((currentMaxSeat - currentMinSeat) / 2) + 1; } if (current == 'L') { currentMaxSeat -= ((currentMaxSeat - currentMinSeat + 1) / 2); } } int seatId = (currentMaxRow * 8) + currentMaxSeat; possibleIds = possibleIds.Where(i => i != seatId); } Console.WriteLine($"possible seat id: {string.Join(',',possibleIds)}"); }
private static void SolvePart1() { var originalInput = InputFetcher.GetInputAsLines(InputFetcher.day3p1); maxWidth = originalInput[0].Length; treesEncountered = 0; x = 0; y = 0; xMover = 3; yMover = 1; while (y < originalInput.Length - 1) { MoveForward(); var line = originalInput[y]; var chararray = line.ToCharArray(); var detected = chararray[x]; if (detected == treeIndicator) { treesEncountered++; } } Console.WriteLine($"Trees: {treesEncountered}. OpenSlopes: {originalInput.Length - 1 - treesEncountered}"); }
public void Exercise1() { var data = InputFetcher.FetchInput("day20"); }