private static void PrintOutputs(LongCodeComputer c) { var msg = string.Concat(c.Outputs.Select(x => (char)x)); Console.WriteLine(msg); c.Outputs.Clear(); }
private static void TryAllCombinations(LongCodeComputer c, string[] items) { for (int itemCount = 1; itemCount <= items.Length; itemCount++) { foreach (var dropping in new Combinations <string>(items, itemCount)) { foreach (var item in dropping) { TypeIn(c, "drop " + item); } TypeIn(c, "inv"); _ = c.RunUntilInputRequired(); PrintOutputs(c); TypeIn(c, "west"); if (!c.RunUntilInputRequired()) { return; } PrintOutputs(c); foreach (var item in dropping) { TypeIn(c, "take " + item); } } } }
public void InputOutputWorks_WithCall() { var computer = new LongCodeComputer(new long[] { 3, 0, 4, 0, 99 }); var result = computer.RunWith(37); Assert.AreEqual(37, result); }
private static Dictionary <Point, HullColor> PaintHull(long[] input, HullColor?initial = null) { var panels = new Dictionary <Point, HullColor>(); var c = new LongCodeComputer(input); var position = new Point(); var heading = Direction.Up; if (initial.HasValue) { panels[Point.Empty] = initial.Value; } while (true) { var currentPanel = panels.GetValueOrDefault(position, HullColor.Black); if (!c.RunWith((long)currentPanel, 2)) { break; } panels[position] = (HullColor)c.Outputs.Dequeue(); heading = c.Outputs.Dequeue() == 0 ? heading.TurnCounterClockwise() : heading.TurnClockwise(); position = position.MoveTo(heading); } return(panels); }
public void CompareJump_4_lessthan_8(long input, long expected) { var computer = new LongCodeComputer(new long[] { 3, 3, 1107, -1, 8, 3, 4, 3, 99 }); computer.Inputs.Enqueue(input); computer.Run(10); Assert.AreEqual(expected, computer.Outputs.First()); }
public void CanCreate() { var state = new long[] { 17 }; var computer = new LongCodeComputer(state); Assert.IsTrue(computer.Memory.Count == 1); Assert.IsTrue(computer.Memory[0] == 17); }
public void CompareJump_1_equals_8(long input, long expected) { var computer = new LongCodeComputer(new long[] { 3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8 }); computer.Inputs.Enqueue(input); computer.Run(10); Assert.AreEqual(expected, computer.Outputs.First()); }
public void InputOutputWorks() { var computer = new LongCodeComputer(new long[] { 3, 0, 4, 0, 99 }); computer.Inputs.Enqueue(37); computer.Run(); Assert.AreEqual(37, computer.Outputs.First()); }
public void TestRelativeBase() { var program = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 }; var computer = new LongCodeComputer(program); computer.Run(); CollectionAssert.AreEqual(program, computer.Outputs); }
public void TestLargeNumbers() { var program = new long[] { 104, 1125899906842624, 99 }; var computer = new LongCodeComputer(program); computer.Run(); Assert.AreEqual(program[1], computer.Outputs.First()); }
public void LimitedExecution() { var computer = new LongCodeComputer(new long[] { 1, 1, 1, 4, 99, 5, 6, 0, 99 }); computer.Run(1); var expected = new long[] { 1, 1, 1, 4, 2, 5, 6, 0, 99 }; CollectionAssertAreEqual(expected, computer.Memory); }
public void Program_4() { var computer = new LongCodeComputer(new long[] { 1, 1, 1, 4, 99, 5, 6, 0, 99 }); computer.Run(); var expected = new long[] { 30, 1, 1, 4, 2, 5, 6, 0, 99 }; CollectionAssertAreEqual(expected, computer.Memory); }
static void Main() { var sw = new Stopwatch(); sw.Start(); _input = File.ReadAllText("../../../input.txt").ParseLongs(); _pointCache = new Dictionary <Point, bool>(); var computer = LongCodeComputer.FromFile(@"../../../input.txt"); var sum = 0; for (int y = 0; y < 50; y++) { for (int x = 0; x < 50; x++) { var inBeam = getPoint(x, y); sum += inBeam ? 1 : 0; Console.Write(inBeam ? '#' : '.'); } Console.WriteLine(); } Console.WriteLine($"Part 1: {sum}"); var probe = new Point(0, 10); var diagMove = new Size(1, -1); var topLeft = Point.Empty; while (topLeft == Point.Empty) { while (!getPoint(probe)) { probe = probe.MoveTo(Direction.Right); } var diagonal = new BinarySearchInt(dx => getPoint(probe + (diagMove * dx))).FindLast(); if (diagonal >= 98) { var rect = new Rectangle(probe.X, probe.Y - 99, 100, 100); if (allPointsInBeam(rect)) { topLeft = rect.Location; break; } } probe = probe.MoveTo(Direction.Down); } Console.WriteLine($"Part 2: topleft point is at {topLeft} => answer = {topLeft.X * 10000 + topLeft.Y}"); //Console.WriteLine("15231022"); sw.Stop(); Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }
public void Program_3() { var computer = new LongCodeComputer(new long[] { 2, 4, 4, 5, 99, 0 }); computer.Run(); var expected = new long[] { 2, 4, 4, 5, 99, 9801 }; CollectionAssertAreEqual(expected, computer.Memory); }
public void Program_D5_negative_instr() { var computer = new LongCodeComputer(new long[] { 1101, 100, -1, 4, 0 }); computer.Run(5); var expected = new long[] { 1101, 100, -1, 4, 99 }; CollectionAssertAreEqual(expected, computer.Memory); }
public void Program_2() { var computer = new LongCodeComputer(new long[] { 2, 3, 0, 3, 99 }); computer.Run(); var expected = new long[] { 2, 3, 0, 6, 99 }; CollectionAssertAreEqual(expected, computer.Memory); }
public void ImmediateParamWorks() { var computer = new LongCodeComputer(new long[] { 1002, 4, 3, 4, 33 }); computer.Run(2); var expected = new long[] { 1002, 4, 3, 4, 99 }; CollectionAssertAreEqual(expected, computer.Memory); }
public void InputOutputWorks2(long input, long expected) { var computer = new LongCodeComputer(new long[] { 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 }); computer.Inputs.Enqueue(input); computer.Run(); Assert.AreEqual(expected, computer.Outputs.First()); }
public void Program_1() { var computer = new LongCodeComputer(new long[] { 1, 0, 0, 0, 99 }); computer.Run(); var expected = new long[] { 2, 0, 0, 0, 99 }; CollectionAssertAreEqual(expected, computer.Memory); Assert.AreEqual(2, computer.StepCount); }
private static bool getPoint(Point p) { return(_pointCache.GetOrAdd(p, pos => { var computer = new LongCodeComputer(_input); computer.Inputs.Enqueue(pos.X); computer.Inputs.Enqueue(pos.Y); var p = computer.RunForOutputs(1); return computer.Outputs.Dequeue() == 1; })); }
public void CanOverride() { var state = new long[] { 17, 33 }; var computer = new LongCodeComputer(state, new Dictionary <int, long> { { 1, 6 } }); Assert.IsTrue(computer.Memory.Count == 2); Assert.IsTrue(computer.Memory[0] == 17); Assert.IsTrue(computer.Memory[1] == 6); }
private static IEnumerable <Packet> CollectPackets(LongCodeComputer c) { while (c.Outputs.Count >= 3) { var addr = c.Outputs.Dequeue(); var x = c.Outputs.Dequeue(); var y = c.Outputs.Dequeue(); yield return(new Packet { addr = addr, x = x, y = y }); } }
static void Main() { var sw = new Stopwatch(); sw.Start(); //var input = File.ReadAllText("../../../input.txt").ParseLongs(); var c = LongCodeComputer.FromFile(@"../../../input.txt"); c.Store(0, 2); var path = "R8L12R8R8L12R8L10L10R8L12L12L10R10L10L10R8L12L12L10R10L10L10R8R8L12R8L12L12L10R10R8L12R8"; var segments = new Stack <(char name, string value)>(); _ = FindPathFunctions(path, segments); foreach (var func in segments) { Console.WriteLine($"Function {func.name} should be {func.value}"); } Console.ReadLine(); Console.WindowHeight = Math.Max(Console.WindowHeight, 40); Queue(c, "A,A,B,C,B,C,B,A,C,A\n"); Queue(c, "R,8,L,12,R,8\n"); Queue(c, "L,10,L,10,R,8\n"); Queue(c, "L,12,L,12,L,10,R,10\n"); Queue(c, "Y\n"); c.RunForOutputs(2167); Console.Clear(); c.Outputs.Clear(); while (c.RunForOutputs(2101)) { Paint(c.Outputs); c.Outputs.Clear(); } var collectedDust = c.Outputs.Dequeue(); Console.WriteLine($"Part 2: {collectedDust} dust collected."); sw.Stop(); Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }
static void Main() { var input = File.ReadAllText(@"../../../input.txt").ParseLongs(); var sw = new Stopwatch(); sw.Start(); var c = new LongCodeComputer(input); c.Store(0, 2); _ = c.RunUntilInputRequired(); AddTiles(c.Outputs); var blockCount = tiles.Count(x => x.Value == 2); Console.WriteLine($"Part 1: {blockCount}"); _ = Console.ReadLine(); var width = 42; var height = 22; Console.BufferWidth = Math.Max(width, Console.BufferWidth); Console.BufferHeight = Math.Max(height + 10, Console.BufferHeight); var frameCounter = 0; do { AddTiles(c.Outputs); if (frameCounter++ % 1 == 0) { PaintGame(); } var relPos = Math.Sign(ballPosition.CompareTo(paddlePosition)); c.Inputs.Enqueue(relPos); } while (c.RunUntilInputRequired()); // Last Paint AddTiles(c.Outputs); PaintGame(); _ = Console.ReadLine(); }
static void Main() { var sw = new Stopwatch(); sw.Start(); var c = LongCodeComputer.FromFile(@"../../../input.txt"); var allCommands = new List <string>(); if (File.Exists(@"../../../commands.txt")) { allCommands = File.ReadAllLines(@"../../../commands.txt").ToList(); foreach (var item in allCommands) { c.QueueInput(item); } } _ = c.RunUntilInputRequired(); PrintOutputs(c); var items = new string[] { "asterisk", "ornament", "cake", "space heater", "festive hat", "semiconductor", "food ration", "sand" }; TryAllCombinations(c, items); while (c.RunUntilInputRequired()) { PrintOutputs(c); var cmd = Console.ReadLine(); allCommands.Add(cmd); c.QueueInput(cmd); } PrintOutputs(c); Console.WriteLine($"Part 1: "); sw.Stop(); Console.WriteLine("Save commands?"); if (Console.ReadLine() == "y") { File.WriteAllLines(@"../../../commands.txt", allCommands); } Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }
static void Main() { var input = File.ReadAllText("../../../input.txt").ParseLongs(); var sw = new Stopwatch(); sw.Start(); var c = new LongCodeComputer(input); var result = c.RunWith(1); Console.WriteLine($"Part 1: {result}"); c = new LongCodeComputer(input); result = c.RunWith(2); Console.WriteLine($"Part 2: {result}"); Console.WriteLine($"After {c.StepCount} steps."); sw.Stop(); Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }
private static void QueuePacket(LongCodeComputer c, Packet p) { c.Inputs.Enqueue(p.x); c.Inputs.Enqueue(p.y); }
static void Main() { var sw = new Stopwatch(); sw.Start(); var computers = new LongCodeComputer[50]; for (int i = 0; i < 50; i++) { computers[i] = LongCodeComputer.FromFile(@"../../../input.txt"); computers[i].Inputs.Enqueue(i); computers[i].DefaultInput = -1; } Packet?natPacket = null; Packet?lastNatPacket = null; Packet?firstNatPacket = null; var idlePeriod = 0; var runNetwork = true; while (runNetwork) { foreach (var c in computers) { c.Run(2); } var packets = computers.SelectMany(c => CollectPackets(c)).ToList(); if (packets.Any()) { idlePeriod = 0; foreach (var p in packets) { if (p.addr == 255) { natPacket = p; } else { QueuePacket(computers[p.addr], p); } } } else { idlePeriod++; } if (idlePeriod > 1000 && natPacket.HasValue) { // Send packet to fight network idling QueuePacket(computers[0], natPacket.Value); idlePeriod = 0; firstNatPacket ??= natPacket; Console.WriteLine($"Sent packet {natPacket.Value.y}"); if (lastNatPacket.HasValue && lastNatPacket.Value.y == natPacket.Value.y) { Console.WriteLine($"Duplicate y! {natPacket.Value.y}"); runNetwork = false; } lastNatPacket = natPacket; natPacket = null; } } // 19420 is too high Console.WriteLine($"Part 1: santa packet has y value {firstNatPacket.Value.y}"); sw.Stop(); Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }
private static void TypeIn(LongCodeComputer c, string txt) { Console.WriteLine(txt); c.QueueInput(txt); }
static void Main() { var sw = new Stopwatch(); sw.Start(); var c = LongCodeComputer.FromFile(@"../../../input.txt"); Console.WindowHeight = Math.Max(Console.WindowHeight, 40); c.QueueInput("NOT A J"); c.QueueInput("NOT B T"); c.QueueInput("OR T J"); c.QueueInput("NOT C T"); c.QueueInput("OR T J"); c.QueueInput("AND D J"); c.QueueInput("WALK"); Console.Clear(); c.Outputs.Clear(); c.Run(); if (c.Outputs.Last() < 256) { Console.WriteLine($"Bad code!"); Paint(c.Outputs); _ = Console.ReadLine(); return; } var shipDamage = c.Outputs.Last(); Console.WriteLine($"Part 1: damage = {shipDamage}"); c = LongCodeComputer.FromFile(@"../../../input.txt"); c.QueueInput("NOT A J"); c.QueueInput("NOT B T"); c.QueueInput("OR T J"); c.QueueInput("NOT C T"); c.QueueInput("OR T J"); c.QueueInput("AND D J"); c.QueueInput("NOT E T"); c.QueueInput("NOT T T"); c.QueueInput("OR H T"); c.QueueInput("AND T J"); c.QueueInput("RUN"); c.Outputs.Clear(); c.Run(); if (c.Outputs.Last() < 256) { Console.WriteLine($"Bad code!"); Paint(c.Outputs); _ = Console.ReadLine(); return; } shipDamage = c.Outputs.Last(); Console.WriteLine($"Part 2: damage = {shipDamage}"); sw.Stop(); Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }