static int ReadPosition(IntComputer ic, Position pos) { IntComputer compu = new IntComputer(ic); compu.reg = pos.x; compu.Execute(); compu.reg = pos.y; compu.Execute(); compu.Execute(); return((int)compu.reg); }
static long RunDroid(IntComputer ic, string s) { int i = 0; int ret = 0; long lastOut = 0; do { lastOut = ic.reg; ic.reg = s[Math.Min(i, s.Length - 1)]; ret = ic.Execute(); if (ret == 2) { i++; } else if (ret == 3) { if (ic.reg < 255) { Console.Write((char)ic.reg); } } }while (ret > 0); return(lastOut); }
static void PartA() { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.Execute(); Console.WriteLine("Part A: Result is {0}", a.paintedPos.Count); }
static void PartB() { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 2); a.Execute(); Console.WriteLine(); Console.WriteLine("Part B: Result is {0}", a.reg); }
static bool PartA(Object correctAnswer = null) { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.Execute(); Console.WriteLine("Part A: Result is {0}", a.paintedPos.Count); return(correctAnswer == null || a.paintedPos.Count == (int)correctAnswer); }
static Object PartA() { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 1); a.Execute(); Console.WriteLine(); Console.WriteLine("Part A: Result is {0}", a.reg); return(a.reg); }
static void PartB() { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.mem[0] = 2; a.Execute(); Console.SetCursorPosition(0, Console.CursorTop + 1); Console.WriteLine("Part B: Result is {0}", a.reg); }
static void PartA() { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.Execute(); int res = a.paintedPos.Where(x => x.Value == 2).Count(); Console.WriteLine("Part A: Result is {0}", res); }
static bool PartB(Object correctAnswer = null) { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 2); a.Execute(); Console.WriteLine(); Console.WriteLine("Part B: Result is {0}", a.reg); return(correctAnswer == null || a.reg == (long)correctAnswer); }
static void PartB() { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.paintedPos[new Position(0, 0)] = 1; a.Execute(); Console.WriteLine("Part B: Result is:"); a.PrintPanel(); }
static bool PartB(Object correctAnswer = null) { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.mem[0] = 2; a.Execute(); Console.SetCursorPosition(0, Console.CursorTop + 1); Console.WriteLine("Part B: Result is {0}", a.reg); return(correctAnswer == null || a.reg == (long)correctAnswer); }
static bool PartA(Object correctAnswer = null) { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.Execute(); int res = a.paintedPos.Where(x => x.Value == 2).Count(); Console.WriteLine("Part A: Result is {0}", res); return(correctAnswer == null || res == (int)correctAnswer); }
static OurMap BuildMap(IntComputer ic, int size) { OurMap m = new OurMap(); Position curPos = new Position(0, 0); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { IntComputer compu = new IntComputer(ic); compu.reg = x; compu.Execute(); compu.reg = y; compu.Execute(); compu.Execute(); int a = (int)compu.reg; m.mapPos[new Position(x, y)] = a; } } return(m); }
static bool PartB(Object correctAnswer = null) { List <long> input = ReadInput(); IntComputer a = new IntComputer(input, 0); a.paintedPos[new Position(0, 0)] = 1; a.Execute(); Console.WriteLine("Part B: Result is:"); string s = a.PrintPanel(); Console.WriteLine(s); return(correctAnswer == null || s == (string)correctAnswer); }
static OurMap BuildMap(IntComputer ic) { OurMap m = new OurMap(); Position curPos = new Position(0, 0); while (ic.Execute() > 0) { int a = (int)ic.reg; if (a == '\n') { curPos.x = 0; curPos.y++; } else { m.mapPos[curPos] = a; curPos += goRight; } } return(m); }
static int SteerRobot(IntComputer ic) { string A = "L,6,R,12,R,8"; string B = "R,8,R,12,L,12"; string C = "R,12,L,12,L,4,L,4"; string M = "A,B,B,A,C,A,C,A,C,B"; List <char> feed = new List <char>(); feed.AddRange(M.ToList()); feed.Add('\n'); feed.AddRange(A.ToList()); feed.Add('\n'); feed.AddRange(B.ToList()); feed.Add('\n'); feed.AddRange(C.ToList()); feed.Add('\n'); feed.Add('n'); feed.Add('\n'); int ret = 0; int i = 0; int lastReg = 0; do { lastReg = (int)ic.reg; ic.reg = feed[Math.Min(i, feed.Count - 1)]; ret = ic.Execute(); if (ret == 2) { i++; } else if (ret == 3) { //Console.Write((char)ic.reg); } }while (ret > 0); return(lastReg); }
static int RunNetworkA(IntComputer computer) { List <IntComputer> compus = new List <IntComputer>(); int ans = 0; for (int i = 0; i < 50; i++) { compus.Add(new IntComputer(computer)); compus.Last().input.Add(i); } bool done = false; while (!done) { for (int i = 0; i < 50 && !done; i++) { IntComputer c = compus[i]; if (c.Execute() == 3 && c.output.Count == 3) { int addr = (int)c.output[0]; if (addr == 255) { ans = (int)c.output[2]; done = true; } else { compus[addr].input.Add(c.output[1]); compus[addr].input.Add(c.output[2]); c.output.Clear(); } } } } return(ans); }
static int SolveTextAdventure(IntComputer ic, List <string> directions, bool log) { string cmd = "Command?"; string input = ""; string output = ""; string lastOutput = ""; int ret = 0; int i = 0; int row = 0; LogAll("\n============================= NEW SESSION =============================\n\n", log); do { if (i < input.Length) { ic.reg = input[i]; } ret = ic.Execute(); if (ret == 2) { i++; } else if (ret == 3) { char c = (char)ic.reg; if (c == '\n') { Console.WriteLine(output); LogAll(output + '\n', log); if (output == cmd) { if (row < directions.Count) { input = directions[row] + '\n'; Console.Write(input); row++; } else { input = Console.ReadLine() + '\n'; } LogInput(input, log); LogAll(input, log); i = 0; } if (output.Length > 0) { lastOutput = output; } output = ""; } else { output += c; } } }while (ret > 0); int res = -1; foreach (string s in lastOutput.Split(' ')) { if (int.TryParse(s, out res)) { break; } } return(res); }
static int RunNetworkB(IntComputer computer) { List <IntComputer> compus = new List <IntComputer>(); List <long> nat = new List <long>(); int ans = 0; for (int i = 0; i < 50; i++) { compus.Add(new IntComputer(computer)); compus.Last().input.Add(i); } bool done = false; long lastNatY = 0; bool lastNatYSet = false; while (!done) { int sent = 0; for (int i = 0; i < 50 && !done; i++) { IntComputer c = compus[i]; int a = c.Execute(); if (a == 3) { sent++; if (c.output.Count == 3) { int addr = (int)c.output[0]; if (addr == 255) { nat.Clear(); nat.Add(c.output[1]); nat.Add(c.output[2]); c.output.Clear(); //Console.WriteLine("NAT received {0}, {1}", nat[0], nat[1]); } else { //Console.WriteLine("C{0} --> C{1}: sent {2}, {3}", i, addr, c.output[1], c.output[2]); compus[addr].input.Add(c.output[1]); compus[addr].input.Add(c.output[2]); c.output.Clear(); } } } } if (nat.Count > 0 && sent == 0) { //Console.WriteLine("Nothing sent..."); if (compus.Select(w => w.input.Count).Sum() == 0) { if (lastNatYSet && lastNatY == nat[1]) { ans = (int)lastNatY; done = true; } compus[0].input.Add(nat[0]); compus[0].input.Add(nat[1]); lastNatY = nat[1]; //Console.WriteLine("NAT sent y = {0}", lastNatY); if (!lastNatYSet) { lastNatYSet = true; } nat.Clear(); } } sent = 0; } return(ans); }