public static int Day2B() { var validPasswords = 0; AOCInput input = new AOCInput().GetInput("Inputs/Day2A.json"); foreach (var item in input.InputString) { // "2-4 p: vpkpp" var arr = item.Split(" "); var range = arr[0].Split("-"); var idxA = int.Parse(range[0]) - 1; var idxB = int.Parse(range[1]) - 1; var character = arr[1][0]; var password = arr[2]; if ((password[idxA] == character && password[idxB] != character) || (password[idxA] != character && password[idxB] == character)) { validPasswords++; } } return(validPasswords); }
public override object Puzzle2(AOCInput input) { string[] lines = input.GetInputLines(); int[] busses = lines[1].Split(',').Select(x => x.Contains("x") ? 0 : int.Parse(x)).ToArray(); long step = 0; long stepSize = busses[0]; for (int offset = 1; offset < busses.Length; ++offset) { if (busses[offset] == 0) { continue; } step += stepSize; if ((step + offset) % busses[offset] != 0) { --offset; continue; } stepSize = stepSize * busses[offset]; } return(step); }
/// <summary> /// The second puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle2(AOCInput input) { // Get input. string[] lines = input.GetInputLines().Select(x => x.Trim()).ToArray(); Dictionary <string, Orbit> orbits = GetOrbits(lines); // Get target and start orbit. Orbit start = orbits["YOU"].lower; Orbit target = orbits["SAN"].lower; List <Orbit> visited = new List <Orbit>(); start.dist = 0; visited.Add(start); bool found = false; while (!found) { Orbit cur = visited.OrderBy(x => x.dist).First(); List <Orbit> neighbours = cur.GetOrbits(); foreach (Orbit neighbour in neighbours) { if (neighbour == null) { continue; } if (neighbour.dist > cur.dist + 1) { neighbour.dist = cur.dist + 1; neighbour.path = cur; visited.Add(neighbour); if (neighbour == target) { found = true; } } } visited.Remove(cur); } // Reconstruct path found, keep track of steps required. Orbit path = target; int transfers = 0; while (path != null) { path = path.path; if (path != null) { transfers++; } } return(transfers); }
public override object Puzzle1(AOCInput input) { string[] lines = input.GetInputLines(); string group = ""; int sum = 0; foreach (string line in lines) { if (!string.IsNullOrEmpty(line)) { group += line; } else { sum += group.Distinct().Count(); group = ""; } } // Add last group. sum += group.Distinct().Count(); return(sum); }
public static int Day5B() { var highestSeat = 0; AOCInput input = new AOCInput().GetInput("Inputs/Day5A.json"); var seats = new List <int>(); foreach (var line in input.InputString) { var newLine = line.Replace('F', '0').Replace('B', '1').Replace('L', '0').Replace('R', '1'); var seat = newLine.Substring(0, 7); var column = newLine.Substring(7, 3); seats.Add(Convert.ToInt32(seat, 2) * 8 + Convert.ToInt32(column, 2)); } seats.Sort(); int j = 6; for (int i = 0; i < seats.Count; i++) { if (j != seats[i]) { return(j); } j++; } return(highestSeat); }
public override object Puzzle2(AOCInput input) { string[] lines = input.GetInputLines(); string group = ""; bool newGroup = true; int sum = 0; foreach (string line in lines) { if (!string.IsNullOrEmpty(line)) { if (newGroup) { group = line; newGroup = false; } else { group = new String(group.Intersect(line).ToArray()); } } else { sum += group.Distinct().Count(); group = ""; newGroup = true; } } // Add last group. sum += group.Distinct().Count(); return(sum); }
public static double Day1B() { AOCInput input = new AOCInput().GetInput("Inputs/Day1A.json"); var hash = new HashSet <int>(); for (int i = 0; i < input.Input.Count; i++) { for (int j = i + 1; j < input.Input.Count; j++) { var pair = input.Input[i] + input.Input[j]; var counterpart = 2020 - pair; if (hash.Contains(counterpart)) { return(input.Input[i] * input.Input[j] * counterpart); } else { if (!hash.Contains(input.Input[i])) { hash.Add(input.Input[i]); } if (!hash.Contains(input.Input[j])) { hash.Add(input.Input[j]); } } } } return(0); }
/// <summary> /// The first puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle1(AOCInput input) { string[] map = input.GetInputLines(); for (int y1 = 0; y1 < map.Length; ++y1) { for (int x1 = 0; x1 < map[y1].Length; ++x1) { // If astroid, we might need to place station here. if (map[y1][x1] == '#') { for (int y2 = 0; y2 < map.Length; ++y2) { for (int x2 = 0; x2 < map[y2].Length; ++x2) { // If same astroid, do nothing. if (y1 == y2 && x1 == x2) { continue; } int xDist = x1 - x2; int yDist = y1 - y2; } } } } } return(null); }
public override object Puzzle2(AOCInput input) { string[] lines = input.GetInputLines(); int[] numbers = new int[lines.Length]; for (int i = 0; i < lines.Length; i++) { numbers[i] = int.Parse(lines[i]); } for (int i = 0; i < numbers.Length; ++i) { for (int j = 0; j < numbers.Length; ++j) { for (int k = 0; k < numbers.Length; ++k) { if (i == j || i == k || j == k) { continue; } if (numbers[i] + numbers[j] + numbers[k] == 2020) { return(numbers[i] * numbers[j] * numbers[k]); } } } } return(null); }
/// <summary> /// The second puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle2(AOCInput input) { int[] range = input.GetInput("-").Select(x => int.Parse(x)).ToArray(); int count = 0; for (int i = range[0]; i < range[1]; ++i) { string password = i.ToString(); bool ascending = password.Zip(password.Skip(1), (a, b) => a <= b).All(x => x); bool same = false; for (int j = 0; j < password.Length - 1; ++j) { if (password[j] == password[j + 1]) { if ((j + 2 >= password.Length || password[j] != password[j + 2]) && (j - 1 < 0 || password[j] != password[j - 1])) { same = true; break; } } } if (same && ascending) { count++; } } return(count); }
public override object Puzzle1(AOCInput input) { string[] lines = input.GetInputLines(); int[] joltages = lines.Select(int.Parse).OrderBy(x => x).ToArray(); int device = joltages[joltages.Length - 1] + 3; int curJoltage = 0; int diff1 = 0; int diff2 = 0; int diff3 = 1; for (int i = 0; i < joltages.Length; ++i) { if (joltages[i] - curJoltage == 1) { diff1++; } else if (joltages[i] - curJoltage == 2) { diff2++; } else if (joltages[i] - curJoltage == 3) { diff3++; } curJoltage = joltages[i]; } return(diff1 * diff3); }
public override object Puzzle2(AOCInput input) { string[] lines = input.GetInputLines(); int answer = 0; for (int i = 0; i < lines.Length; ++i) { string[] split = lines[i].Split(new char[] { '-', ' ', ':' }); int first = int.Parse(split[0]); int second = int.Parse(split[1]); char character = split[2][0]; string password = split[4]; bool hasFirst = password[first - 1] == character; bool hasSecond = password[second - 1] == character; if (hasFirst ^ hasSecond) { answer++; } } return(answer); }
public override object Puzzle2(AOCInput input) { string[] lines = input.GetInputLines(); int[] patternX = { 1, 3, 5, 7, 1 }; int[] patternY = { 1, 1, 1, 1, 2 }; long product = 1; for (int i = 0; i < patternX.Length; ++i) { int posX = 0; int posY = 0; int trees = 0; while (posY < lines.Length) { if (lines[posY][(posX % lines[posY].Length)] == '#') { trees++; } posX += patternX[i]; posY += patternY[i]; } product *= trees; } return(product); }
public override object Puzzle1(AOCInput input) { string[] lines = input.GetInputLines(); int answer = 0; for (int i = 0; i < lines.Length; ++i) { string[] split = lines[i].Split(new char[] { '-', ' ', ':' }); int min = int.Parse(split[0]); int max = int.Parse(split[1]); char character = split[2][0]; string password = split[4]; int count = 0; for (int j = 0; j < password.Length; ++j) { if (password[j] == character) { count++; } } if (count >= min && count <= max) { answer++; } } return(answer); }
public override object Puzzle1(AOCInput input) { string[] lines = input.GetInputLines(); int earliest = int.Parse(lines[0]); int[] busses = lines[1].Split(',').Where(x => !x.Contains("x")).Select(int.Parse).ToArray(); // Search at lest for the minimum id; int attempts = earliest / busses.Min() + 1; int closestDistance = int.MaxValue; int timeToWait = 0; int busID = 0; for (int i = 0; i < attempts; ++i) { for (int j = 0; j < busses.Length; ++j) { int distance = Math.Abs(earliest - busses[j] * i); if (distance < closestDistance && busses[j] * i > earliest) { closestDistance = distance; timeToWait = (busses[j] * i) - earliest; busID = busses[j]; } } } return(timeToWait * busID); }
public static void Convert(IntCodeProgram program, string filename) { var compiler = new IntCodeCompiler(); string output = ""; long pointer = 0; while (pointer < program.MemoryLength) { var instruction = program[pointer]; var opcode = instruction % 100; if (opcode == 99) { output += "Halt"; break; } else if (opcode > compiler.Instructions.Length || compiler.Instructions[opcode] == null) { output += "! Missing Instruction !\n"; break; } else { output += InstructionToCode(program, instruction, opcode, pointer); pointer += compiler.InstructionsSkips[opcode]; } } if (Application.isPlaying) { AOCExecutor.ActionForMain.Enqueue(() => AOCInput.WriteToFile(filename, output)); } else { AOCInput.WriteToFile(filename, output); } }
public static void ConvertWhileRunning(IntCodeCompiler intCodeCompiler, IntCodeProgram program, string filename, bool useExecutorActionForMain) { string output = ""; int maxSteps = 100; while (!program.IsDone || maxSteps-- > 0) { var instruction = program.OpCodeAtPointer; var opcode = instruction % 100; if (opcode == 99) { output += "Halt"; break; } else if (opcode > intCodeCompiler.Instructions.Length || intCodeCompiler.Instructions[opcode] == null) { output += $"! Missing Instruction {opcode}!\n"; break; } else { output += InstructionToCode(program, instruction, opcode, program.Pointer); program = intCodeCompiler.ComputeStep(program); } } if (useExecutorActionForMain) { AOCExecutor.ActionForMain.Enqueue(() => AOCInput.WriteToFile(filename, output)); } else { AOCInput.WriteToFile(filename, output); } }
public override object Puzzle2(AOCInput input) { string[] rules = input.GetInputLines(); Dictionary <string, Dictionary <string, int> > bags = new Dictionary <string, Dictionary <string, int> >(); foreach (string rule in rules) { string[] splitRule = rule.Split(new string[] { "bags contain", "bags", "bag", ",", "." }, StringSplitOptions.RemoveEmptyEntries); Dictionary <string, int> contains = new Dictionary <string, int>(); if (!rule.Contains("no other bags.")) { for (int i = 1; i < splitRule.Length; ++i) { int amount = int.Parse(splitRule[i].Trim().Split(' ')[0]); string name = splitRule[i].Trim().Remove(0, 1 + amount.ToString().Length); contains.Add(name, amount); } } bags.Add(splitRule[0].Trim(), contains); } return(Count(bags, "shiny gold")); }
/// <summary> /// The first puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle1(AOCInput input) { int[] software = input.GetInput(",").Select(x => int.Parse(x)).ToArray(); IntcodeComputer computer; int[] sequence = new int[] { 0, 1, 2, 3, 4 }; var permutations = GetPermutations <int>(sequence, 5); long maxOutput = 0; int maxSequence = 0; for (int i = 0; i < permutations.Count(); ++i) { long output = 0; for (int j = 0; j < 5; ++j) { computer = new IntcodeComputer((long[])software.Clone(), false); computer.Run(permutations.ElementAt(i).ElementAt(j), output); output = computer.Outputs.Last(); } if (output > maxOutput) { maxOutput = output; maxSequence = i; } } return(maxOutput); }
/// <summary> /// The second puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle2(AOCInput input) { long[] program = input.GetInput(",").Select(x => long.Parse(x)).ToArray(); IntcodeComputer computer = new IntcodeComputer(program); computer.Run(5); return computer.Outputs[0]; }
private void WriteToFilePainting(Dictionary <Vector2Int, List <long> > painting, string fileName) { var output = $"{painting.Keys.Count} panels painted in {painting.Sum(x => x.Value.Count)} Steps.\n"; foreach (var item in painting) { output += $"{item.Key}: {string.Join(", ", item.Value)}\n"; } AOCInput.WriteToFile(fileName, output); }
/// <summary> /// The second puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle2(AOCInput input) { long[] software = input.GetInput(",").Select(x => long.Parse(x)).ToArray(); IntcodeComputer computer = new IntcodeComputer(software, false); computer.Run(2); return(computer.Outputs.Last()); }
public static long Day3B() { //Right 1, down 1. //Right 3, down 1. (This is the slope you already checked.) //Right 5, down 1. //Right 7, down 1. //Right 1, down 2. var treesA = new TreeCount(1); var treesB = new TreeCount(3); var treesC = new TreeCount(5); var treesD = new TreeCount(7); var treesE = new TreeCount(1); AOCInput input = new AOCInput().GetInput("Inputs/Day3A.json"); int i = 0; int mapLength = input.InputString[i].Length; while (i < input.InputString.Count) { treesA.Count = CheckForTree(input.InputString[i][treesA.Idx], treesA.Count); treesA.Idx += treesA.Slope; treesA.Idx = WrapMap(treesA.Idx, mapLength); treesB.Count = CheckForTree(input.InputString[i][treesB.Idx], treesB.Count); treesB.Idx += treesB.Slope; treesB.Idx = WrapMap(treesB.Idx, mapLength); treesC.Count = CheckForTree(input.InputString[i][treesC.Idx], treesC.Count); treesC.Idx += treesC.Slope; treesC.Idx = WrapMap(treesC.Idx, mapLength); treesD.Count = CheckForTree(input.InputString[i][treesD.Idx], treesD.Count); treesD.Idx += treesD.Slope; treesD.Idx = WrapMap(treesD.Idx, mapLength); i++; } i = 0; while (i < input.InputString.Count) { treesE.Count = CheckForTree(input.InputString[i][treesE.Idx], treesE.Count); treesE.Idx += treesE.Slope; treesE.Idx = WrapMap(treesE.Idx, mapLength); i += 2; } Console.WriteLine($"A: {treesA.Count}, B: {treesB.Count}, C: {treesC.Count}, D: {treesD.Count}, E: {treesE.Count}"); return(treesA.Count * treesB.Count * treesC.Count * treesD.Count * treesE.Count); }
public override object Puzzle2(AOCInput input) { string[] lines = input.GetInputLines(); int[] joltages = lines.Select(int.Parse).OrderBy(x => x).ToArray(); int device = joltages[joltages.Length - 1] + 3; _cachedArrangements = new Dictionary <int, long>(); return(CalculateArrangements(0, joltages, 0) + 1); }
/// <summary> /// The first puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle1(AOCInput input) { int[] program = input.GetInput(",").Select(x => int.Parse(x)).ToArray(); program[1] = 12; program[2] = 2; IntcodeComputer computer = new IntcodeComputer(program); return(computer.Run()); }
/// <summary> /// The second puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle2(AOCInput input) { int[] software = input.GetInput(",").Select(x => int.Parse(x)).ToArray(); IntcodeComputer[] computer = new IntcodeComputer[5]; int[] sequence = new int[] { 5, 6, 7, 8, 9 }; var permutations = GetPermutations <int>(sequence, 5); long maxOutput = 0; int maxSequence = 0; for (int i = 0; i < permutations.Count(); ++i) { long output = 0; // Initialize computers. for (int j = 0; j < computer.Length; ++j) { computer[j] = new IntcodeComputer((long[])software.Clone(), false); computer[j].Run(permutations.ElementAt(i).ElementAt(j), output); output = computer[j].Outputs.Last(); } int curComp = 0; // Computer feedback loop. while (true) { if (computer[curComp].Halted) { break; } computer[curComp].Run(output); output = computer[curComp].Outputs.Last(); curComp = (curComp + 1) % 5; } // Check if output is highest. if (output > maxOutput) { maxOutput = output; maxSequence = i; } } return(maxOutput); }
public static int Day2A() { var validPasswords = 0; AOCInput input = new AOCInput().GetInput("Inputs/Day2A.json"); foreach (var item in input.InputString) { // "2-4 p: vpkpp" var arr = item.Split(" "); var range = arr[0].Split("-"); var min = int.Parse(range[0]); var max = int.Parse(range[1]); var character = arr[1][0]; var password = arr[2]; // Console.WriteLine($"min: {min}, max: {max}, character: {character}, password: {password}"); Dictionary <char, int> dict = new Dictionary <char, int>(); for (int i = 0; i < password.Length; i++) { if (dict.ContainsKey(password[i])) { dict[password[i]]++; } else { dict.Add(password[i], 1); } if (password[i] == character && dict[password[i]] > max) { break; } } if (dict.ContainsKey(character) && dict[character] >= min && dict[character] <= max) { validPasswords++; } } return(validPasswords); }
public override object Puzzle2(AOCInput input) { string[] boardingTickets = input.GetInputLines(); List <int> ids = new List <int>(); foreach (string ticket in boardingTickets) { int rowMin = 0; int rowMax = 127; int columnMin = 0; int columnMax = 8; for (int i = 0; i < ticket.Length; ++i) { if (ticket[i] == 'F') { rowMax -= ((rowMax + 1) - rowMin) / 2; } else if (ticket[i] == 'B') { rowMin += ((rowMax + 1) - rowMin) / 2; } else if (ticket[i] == 'L') { columnMax -= ((columnMax + 1) - columnMin) / 2; } else if (ticket[i] == 'R') { columnMin += ((columnMax + 1) - columnMin) / 2; } } int id = rowMin * 8 + columnMin; ids.Add(id); } foreach (int id in ids) { if (ids.Contains(id - 2) && !ids.Contains(id - 1)) { return(id - 1); } } return(-1); }
public override object Puzzle2(AOCInput input) { const int preambleLength = 25; string[] lines = input.GetInputLines(); long[] numbers = lines.Select(long.Parse).ToArray(); long[] preamble = new long[preambleLength]; long answer = 0; for (int i = 0; i < numbers.Length - 1; ++i) { preamble[i % preambleLength] = numbers[i]; if (i >= preambleLength) { if (!CheckRule(preamble, numbers[i + 1])) { answer = numbers[i + 1]; } } } int size = 2; while (true) { for (int i = 0; i < numbers.Length - size; ++i) { long sum = 0; for (int j = 0; j < size; ++j) { sum += numbers[i + j]; } if (sum == answer) { return(numbers.Skip(i).Take(size).Min() + numbers.Skip(i).Take(size).Max()); } } size++; } return(null); }
/// <summary> /// The second puzzle. /// </summary> /// <param name="input">The AOCInput object for getting the input.</param> /// <returns>The answer.</returns> public override object Puzzle2(AOCInput input) { // Parse input. string[] lines = input.GetInputLines(); List <Point> line1 = GetLine(lines[0].Split(',')); List <Point> line2 = GetLine(lines[1].Split(',')); // Find intersections. List <Point> intersections = line1.Intersect(line2).ToList(); // Remove 0,0 intersection. intersections.RemoveAt(0); // Find fewest combines steps to intersection. return(intersections.Select(x => line1.IndexOf(x) + line2.IndexOf(x)) .OrderBy(x => x).First()); }