public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { return(_reinders.Select(r => r.GetDistanceAtTime(2503)).Max().ToString()); } if (part == Aoc.Framework.Part.Part2) { long[] scores = new long[_reinders.Length]; for (int t = 1; t <= 2503; ++t) { // Sort reinders var leaderboard = _reinders.Select((r, i) => (i, r.GetDistanceAtTime(t))).OrderByDescending(r => r.Item2); // Get the top score var top = leaderboard.First().Item2; // Only get the firsts var firsts = leaderboard.Where(r => r.Item2 == top).Select(r => r.i); // Count the points foreach (int n in firsts) { scores[n]++; } } return(scores.Max().ToString()); } return(""); }
private Int64 DecompressLength(string compressed, Aoc.Framework.Part part) { int cursor = 0; Int64 length = 0; while (cursor < compressed.Length) { if (compressed[cursor] == '(') { int end = compressed.IndexOf(')', cursor); int[] repeat = compressed.Substring(cursor + 1, end - cursor - 1).Split("x").Select(item => Int32.Parse(item)).ToArray(); if (part == Aoc.Framework.Part.Part1) { length += repeat[0] * repeat[1]; } else { string sub = compressed.Substring(end + 1, repeat[0]); length += DecompressLength(sub, part) * repeat[1]; } cursor = end + repeat[0] + 1; } else { cursor++; length++; } } return(length); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { _instructions = Aoc.Framework.Input.GetStringVector(this); Cpu cpu = new Cpu(-1); cpu.OnExecute += OnExecute; Cpu.CpuState state = Cpu.CpuState.Running; cpu.Registers["a"] = 7; while (state == Cpu.CpuState.Running) { state = cpu.Execute(_instructions); } return(cpu.Registers["a"].ToString()); } if (part == Aoc.Framework.Part.Part2) { _instructions = Aoc.Framework.Input.GetStringVector(this); Cpu cpu = new Cpu(-1); cpu.OnExecute += OnExecute; Cpu.CpuState state = Cpu.CpuState.Running; cpu.Registers["a"] = 12; while (state == Cpu.CpuState.Running) { state = cpu.Execute(_instructions); } return(cpu.Registers["a"].ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ",")); _cpu.Input.Enqueue(1); _cpu.Run(); while (_cpu.Output.Count > 0) { long result = _cpu.Output.Dequeue(); if (_cpu.Output.Count == 0) { return(result.ToString()); } } return("Not found"); } if (part == Aoc.Framework.Part.Part2) { _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ",")); _cpu.Input.Enqueue(5); _cpu.Run(); return(_cpu.Output.Dequeue().ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Naively activate each gates _circuit.Tick(); // Get the output return(_circuit.Registers["a"].ToString()); } if (part == Aoc.Framework.Part.Part2) { // Naively activate each gates _circuit.Tick(); // Reset the circuit UInt16 result = _circuit.Registers["a"]; _circuit.Reset(); // Hardwire the output b _circuit.Gates.Where(g => g.Output == "b").ToList(); _circuit.Gates.Add(new GateWire { Inputs = new [] { result.ToString() }, Output = "b" }); // Naively activate each gates _circuit.Tick(); // Get the output return(_circuit.Registers["a"].ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { return(_frequencies.Sum().ToString()); } if (part == Aoc.Framework.Part.Part2) { long counter = 0; long sum = 0; HashSet <long> seen = new HashSet <long> { 0 }; while (true) { sum += _frequencies[counter++ % _frequencies.Length]; if (seen.Contains(sum)) { return(sum.ToString()); } seen.Add(sum); } } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { long i = 1; while (true) { byte[] md5 = Md5.Compute(_input, i); if ((md5[0] == 0) && (md5[1] == 0) && ((md5[2] & 0xF0) == 0)) { return(i.ToString()); } i++; } } if (part == Aoc.Framework.Part.Part2) { long i = 1; while (true) { byte[] md5 = Md5.Compute(_input, i); if ((md5[0] == 0) && (md5[1] == 0) && (md5[2] == 0)) { return(i.ToString()); } i++; } } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { _board[1, 1] = 0; Queue <Point> queue = new Queue <Point>(); queue.Enqueue(new Point(1, 1)); while (_board[31, 39] == null) { var position = queue.Dequeue(); EnqueueNeighbors(queue, position); } return(_board[31, 39].Value.ToString()); } if (part == Aoc.Framework.Part.Part2) { int count = 0; for (int i = 0; i < 100; ++i) { for (int j = 0; j < 100; ++j) { if ((_board[i, j] >= 0) && (_board[i, j] <= 50)) { count++; } } } return(count.ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { return(_seats.Max().ToString()); } if (part == Aoc.Framework.Part.Part2) { int i = 1; bool active = false; while (true) { if (!_seats.Contains(i)) { if (active) { return(i.ToString()); } } else { active = true; } i++; } } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { Dictionary <string, int> target = new Dictionary <string, int> { { "children", 3 }, { "cats", 7 }, { "samoyeds", 2 }, { "pomeranians", 3 }, { "akitas", 0 }, { "vizslas", 0 }, { "goldfish", 5 }, { "trees", 3 }, { "cars", 2 }, { "perfumes", 1 } }; string[][] lines = Aoc.Framework.Input.GetStringMatrix(this, " "); for (int i = 0; i < lines.Length; ++i) { string[] line = lines[i]; bool valid = true; for (int index = 2; index < line.Length; index += 2) { string property = line[index][0..^ 1];
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { return(ResolveFight(3).ToString()); } if (part == Aoc.Framework.Part.Part2) { Reset(3); int nbElf = _map.Values.Count(creature => creature.Type == CreatureType.Elf); int elfAttack = 4; long outcome = 0; while (true) { outcome = ResolveFight(elfAttack); if (nbElf == _map.Values.Count(creature => creature.Type == CreatureType.Elf)) { // No casuality on the elf side ! break; } elfAttack++; } return(outcome.ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { Int64 sum = 0; foreach (Tuple <string, int, string> room in _rooms) { if (ValidateRoom(room.Item1, room.Item3)) { sum += room.Item2; } } return(sum.ToString()); } if (part == Aoc.Framework.Part.Part2) { foreach (Tuple <string, int, string> room in _rooms) { if (ValidateRoom(room.Item1, room.Item3)) { if (DecryptRoom(room.Item1, room.Item2) == "northpole object storage") { return(room.Item2.ToString()); } } } return(""); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { HashSet <int> group = new HashSet <int>(); BuildGroup(group, 0); return(group.Count.ToString()); } if (part == Aoc.Framework.Part.Part2) { int groupCount = 0; HashSet <int> all = new HashSet <int>(); foreach (int id in _links.Keys) { if (!all.Contains(id)) { groupCount++; BuildGroup(all, id); } } return(groupCount.ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Find the best power cell var best = FindBestCell(3); return($"{best.Item1},{best.Item2}"); } if (part == Aoc.Framework.Part.Part2) { (Point, int)best = (new Point(0, 0), 0); Board2D <int> power = new Board2D <int>(); int bestSize = 0; // Find the best power cell for (int size = 1; size <= 30; ++size) { IncrementPowerGrid(power, size); var result = power.Cells.OrderByDescending(cell => cell.Item2).First(); if (result.Item2 > best.Item2) { best = result; bestSize = size; } } return($"{best.Item1.X},{best.Item1.Y},{bestSize}"); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { Board2D <bool> current = _grid; for (int i = 0; i < 100; ++i) { // Evaluate next step Board2D <bool> next = new Board2D <bool>(); for (int x = 0; x < 100; ++x) { for (int y = 0; y < 100; ++y) { if (GetNextState(current, x, y)) { next[x, y] = true; } } } current = next; } return(current.Values.Count.ToString()); } if (part == Aoc.Framework.Part.Part2) { Board2D <bool> current = _grid; for (int i = 0; i < 100; ++i) { // Force the corners to be on current[0, 0] = true; current[0, 99] = true; current[99, 0] = true; current[99, 99] = true; // Evaluate next step Board2D <bool> next = new Board2D <bool>(); for (int x = 0; x < 100; ++x) { for (int y = 0; y < 100; ++y) { if (GetNextState(current, x, y)) { next[x, y] = true; } } } current = next; } // Force the corners to be on current[0, 0] = true; current[0, 99] = true; current[99, 0] = true; current[99, 99] = true; return(current.Values.Count.ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Run the robot return(Paint(0).ToString()); } if (part == Aoc.Framework.Part.Part2) { // Run the robot Paint(1); // Draw the result StringBuilder result = new StringBuilder(); result.AppendLine(); for (int h = 0; h >= -5; --h) { for (int w = 0; w < 40; ++w) { result.Append((_hull[w, h] == 1) ? "#" : " "); } result.AppendLine(); } return(result.ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { _input = Aoc.Framework.Input.GetIntVector(this); Int32 steps = 0; Int32 current = 0; while ((current >= 0) && (current < _input.Length)) { Int32 jump = _input[current]; if (part == Aoc.Framework.Part.Part1) { _input[current]++; } else { if (_input[current] >= 3) { _input[current]--; } else { _input[current]++; } } current += jump; steps++; } // Finished ! return(steps.ToString()); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { char[] password = _input.ToCharArray(); while (NextPassword(password)) { if (IsPasswordValid(password)) { return(string.Join("", password)); } } } if (part == Aoc.Framework.Part.Part2) { char[] password = _input.ToCharArray(); while (NextPassword(password)) { if (IsPasswordValid(password)) { break; } } while (NextPassword(password)) { if (IsPasswordValid(password)) { return(string.Join("", password)); } } } return(""); }
private void Evaluate(Aoc.Framework.Part part, int[] chain, int len) { int strength = GetStrength(chain); if (part == Aoc.Framework.Part.Part1) { _max = Math.Max(strength, _max); } if (part == Aoc.Framework.Part.Part2) { // Check length first if (len > _length) { _length = len; _max = strength; } // Check strength if ((len == _length) && (strength > _max)) { _max = strength; } } }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ",")); _cpu.Code[1] = 12; _cpu.Code[2] = 2; _cpu.Run(); return(_cpu.Code[0].ToString()); } if (part == Aoc.Framework.Part.Part2) { for (int noun = 0; noun <= 99; noun++) { for (int verb = 0; verb <= 99; verb++) { _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ",")); _cpu.Code[1] = noun; _cpu.Code[2] = verb; _cpu.Run(); if (_cpu.Code[0] == 19690720) { return((100 * noun + verb).ToString()); } } } return("Not found"); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { foreach (int v in _input) { if (_set.Contains(2020 - v)) { return((v * (2020 - v)).ToString()); } } return("Not found"); } if (part == Aoc.Framework.Part.Part2) { for (int i = 0; i < _input.Length - 2; i++) { for (int j = 1; j < _input.Length - 1; ++j) { if (_set.Contains(2020 - _input[i] - _input[j])) { return(((2020 - _input[i] - _input[j]) * _input[i] * _input[j]).ToString()); } } } return("Not found"); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Prepare the data Queue <(long, long)> queue = new Queue <(long, long)>(); for (int i = 0; i < _count; ++i) { queue.Enqueue((i + 1, 1)); } // Process the queue while (queue.TryDequeue(out var elf)) { if (!queue.TryDequeue(out var next)) { // The elf is the last one, he won return(elf.Item1.ToString()); } // Steal the presents ! queue.Enqueue((elf.Item1, elf.Item2 + next.Item2)); } return("not found"); } if (part == Aoc.Framework.Part.Part2) { // The same with another structure SparseArray <long> elves = new SparseArray <long>(_count); for (long i = 0; i < elves.Length; ++i) { elves[i] = 1; } // Process the array long current = 0; long opposite = elves.RealLength / 2; while (elves.RealLength > 1) { elves[current] += elves[opposite]; elves.RemoveAt(opposite); if (elves.RealLength % 2 == 0) { opposite = elves.Next(opposite, 2); } else { opposite = elves.Next(opposite, 1); } current = elves.Next(current); } return((current + 1).ToString()); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Fetch all the item string[] commands = { "west", "west", "west", "west", "take dark matter", "east", "south", "take fixed point", "west", "take food ration", "east", "north", "east", "south", "take astronaut ice cream", "south", "take polygon", "east", "take easter egg", "east", "take weather machine", "north", "inv" }; _cpu.Reset(_code); Step(); foreach (string command in commands) { Step(command); } // Now try any combinaison of them string[] items = { "polygon", "fixed point", "astronaut ice cream", "easter egg", "dark matter", "food ration", "weather machine" }; int inventory = 0; // Drop evrything foreach (string item in items) { Step($"drop {item}"); } while (_cpu.State != IntCpu.RunningState.Halted) { int next = inventory + 1; for (int i = 0; i < 7; ++i) { if ((((next >> i) & 0x01) == 0x1) && (((inventory >> i) & 0x01) == 0x0)) { Step($"take {items[i]}"); } if ((((next >> i) & 0x01) == 0x0) && (((inventory >> i) & 0x01) == 0x1)) { Step($"drop {items[i]}"); } } Step($"north"); inventory = next; } return(_result.ToString()); } if (part == Aoc.Framework.Part.Part2) { return("Victory!"); } return(""); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { IEnumerable <int> deck = Enumerable.Range(0, 10007); foreach (string s in _input) { if (s[0..3] == "cut")
public string Run(Aoc.Framework.Part part) { Queue <long> player1 = new Queue <long>(_decks[0]); Queue <long> player2 = new Queue <long>(_decks[1]); Combat(player1, player2, part); return(CalculateScore(player1.Count > 0 ? player1 : player2).ToString()); }
private bool CheckWords(string a, string b, Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { return(a == b); } return(a.IsAnagram(b)); }
private void Burst(Aoc.Framework.Part part) { Direction turnDirection = GetTurnDirection(); _direction = Board2D <Cell> .Turn(_direction, turnDirection); Infect(part); _position = Board2D <Int64> .MoveForward(_position, _direction); }
private Char Next(int index, Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { return(_captcha[(index + 1) % _captcha.Length]); } return(_captcha[(index + (_captcha.Length / 2)) % _captcha.Length]); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Search the best outcome return(Search(_items['@'], _items.Keys.Count(i => char.IsLower(i))).ToString()); } if (part == Aoc.Framework.Part.Part2) { // THANKS GOD PART 2 IS LMUCH EASIER THAN PART1 ! // Update the grid Point center = _items['@']; _board[center] = '#'; _board[center.X, center.Y + 1] = '#'; _board[center.X, center.Y - 1] = '#'; _board[center.X + 1, center.Y] = '#'; _board[center.X - 1, center.Y] = '#'; _board[center.X - 1, center.Y - 1] = '@'; _board[center.X - 1, center.Y + 1] = '@'; _board[center.X + 1, center.Y - 1] = '@'; _board[center.X + 1, center.Y + 1] = '@'; // For each quadrant, ignore doors which key is another quadrant foreach (var kv in _items.Where(kv => char.IsUpper(kv.Key))) { Point door = kv.Value; Point key = _items[char.ToLower(kv.Key)]; if ((door.X < center.X && key.X > center.X) || (door.Y < center.Y && key.Y > center.Y) || (door.X > center.X && key.X < center.X) || (door.Y > center.Y && key.Y < center.Y)) { // Remove the door _board[door] = '.'; } } // Now run each quadrant independently // Top-left vault int nbKeys = _items.Count(kv => char.IsLower(kv.Key) && kv.Value.X < center.X && kv.Value.Y < center.Y); long minSteps = Search(new Point(center.X - 1, center.Y - 1), nbKeys); // Top-right vault nbKeys = _items.Count(kv => char.IsLower(kv.Key) && kv.Value.X > center.X && kv.Value.Y < center.Y); minSteps += Search(new Point(center.X + 1, center.Y - 1), nbKeys); // Bottom-left vault nbKeys = _items.Count(kv => char.IsLower(kv.Key) && kv.Value.X <center.X && kv.Value.Y> center.Y); minSteps += Search(new Point(center.X - 1, center.Y + 1), nbKeys); // Bottom-right vault nbKeys = _items.Count(kv => char.IsLower(kv.Key) && kv.Value.X > center.X && kv.Value.Y > center.Y); minSteps += Search(new Point(center.X + 1, center.Y + 1), nbKeys); return(minSteps.ToString());; } return(""); }
private int EvaluateChecksum(Aoc.Framework.Part part) { int checksum = 0; for (int i = 0; i < _content.Length; ++i) { checksum += (part == Aoc.Framework.Part.Part1) ? EvaluateRowChecksum(i) : EvaluateRowEven(i); } return(checksum); }