public static long DaySeven(IList <long> input) { long largest = 0; var perms = Permutations(new List <int> { 0, 1, 2, 3, 4 }); foreach (var perm in perms) { long output = 0; for (int i = 0; i < perm.Count; ++i) { Queue <long> q = new Queue <long>(); q.Enqueue(perm[i]); q.Enqueue(output); var nextinput = input.Select(x => x).ToList(); var icc = new IntCodeCompiler(nextinput, q); icc.Calculate(); output = icc.LastOutput; } if (largest < output) { largest = output; } } return(largest); }
public void SetUp(IntCodeCompiler d13) { d13.Calculate(); while (d13.OutputQueue.Count >= 3) { int x = (int)d13.OutputQueue.Dequeue(); int y = (int)d13.OutputQueue.Dequeue(); int t = (int)d13.OutputQueue.Dequeue(); _board.Add(new Tuple <int, int>(x, y), (GameTiles)t); } }
static IDictionary <Tuple <int, int>, int> PaintMyWagon(IntCodeCompiler d11, int initialPanel) { IDictionary <Tuple <int, int>, int> panels = new Dictionary <Tuple <int, int>, int>(); panels.Add(new Tuple <int, int>(0, 0), initialPanel); int direction = 0; Tuple <int, int> currentLocation = new Tuple <int, int>(0, 0); d11.Calculate(); while (d11.State != CompilerState.Halted) { if (d11.State == CompilerState.PausedWaitingForInput) { if (!panels.ContainsKey(currentLocation)) { panels.Add(currentLocation, 0); } d11.ProvideInput(panels[currentLocation] % 2); } d11.Calculate(); if (d11.OutputQueue.Count >= 2) { long colour = d11.OutputQueue.Dequeue(); long turn = d11.OutputQueue.Dequeue(); if (panels[currentLocation] % 2 != colour) { panels[currentLocation]++; } direction = (direction + (turn == 0 ? 270 : 90)) % 360; switch (direction) { case (0): currentLocation = new Tuple <int, int>(currentLocation.Item1, currentLocation.Item2 + 1); break; case (90): currentLocation = new Tuple <int, int>(currentLocation.Item1 + 1, currentLocation.Item2); break; case (180): currentLocation = new Tuple <int, int>(currentLocation.Item1, currentLocation.Item2 - 1); break; case (270): currentLocation = new Tuple <int, int>(currentLocation.Item1 - 1, currentLocation.Item2); break; } } } return(panels); }
public int Play(IntCodeCompiler d13) { int blocks = 1; d13.Calculate(); while (d13.State != CompilerState.Halted || blocks != 0) { if (d13.State == CompilerState.PausedWaitingForInput) { while (d13.OutputQueue.Count >= 3) { int x = (int)d13.OutputQueue.Dequeue(); int y = (int)d13.OutputQueue.Dequeue(); int t = (int)d13.OutputQueue.Dequeue(); if (x == -1 && y == 0) { _score = t; } else { var p = new Tuple <int, int>(x, y); if (_board.ContainsKey(p)) { _board[p] = (GameTiles)t; } else { _board.Add(new Tuple <int, int>(x, y), (GameTiles)t); } } } blocks = OutputBoard(); } if (blocks == 0) { break; } Console.WriteLine("score: {0}, {1} blocks remain.", Score, blocks); Console.Write("input > "); var i = 0; // Convert.ToInt64(Console.ReadLine()); System.Threading.Thread.Sleep(100); d13.ProvideInput(i); d13.Calculate(); if (d13.State == CompilerState.Halted && blocks > 0) { d13.ResetAndCalculate(); } } return(_score); }
public static void DayElevenB(IntCodeCompiler d11) { var panels = PaintMyWagon(d11, 1); int minX = panels.Select(kvp => kvp.Key.Item1).Min(); int minY = panels.Select(kvp => kvp.Key.Item2).Min(); int maxX = panels.Select(kvp => kvp.Key.Item1).Max(); int maxY = panels.Select(kvp => kvp.Key.Item2).Max(); for (int y = maxY; y >= minY; --y) { StringBuilder sb = new StringBuilder(); for (int x = minX; x <= maxX; ++x) { Tuple <int, int> location = new Tuple <int, int>(x, y); sb.Append(panels.ContainsKey(location) && panels[location] % 2 == 1 ? '#' : '.'); } Console.WriteLine(sb.ToString()); } }
public int MapArea(IntCodeCompiler d15) { Tuple <int, int> location = new Tuple <int, int>(0, 0); Stack <Tuple <int, int, int, bool> > moves = new Stack <Tuple <int, int, int, bool> >(); for (int i = 4; i >= 1; --i) { moves.Push(new Tuple <int, int, int, bool>(0, 0, i, false)); } // the visited dictionary should have the minimum step count IDictionary <Tuple <int, int>, int?> visited = new Dictionary <Tuple <int, int>, int?> { [location] = 0 }; _board[location] = 'D'; d15.Calculate(); Tuple <int, int> oxygen = null; while (moves.Count > 0) { bool success = false; Tuple <int, int> nextLocation = null; Tuple <int, int, int, bool> reverseMove = null; int i = 0; int r = 0; bool reversedMove = false; while (!success) { if (moves.Count == 0) { return(visited[oxygen].Value); } var move = moves.Pop(); if (move.Item1 != location.Item1 || move.Item2 != location.Item2) { throw new InvalidOperationException("I'm not where I expected to be"); } // Console.Write("input (1-4) > "); i = move.Item3; // Convert.ToInt32(Console.ReadLine()); int x = location.Item1; int y = location.Item2; switch (i) { case 1: y++; r = 2; break; case 2: y--; r = 1; break; case 3: x++; r = 4; break; case 4: x--; r = 3; break; } // create the next location and the reverse move, then add it if we make the move nextLocation = new Tuple <int, int>(x, y); reverseMove = new Tuple <int, int, int, bool>(x, y, r, true); reversedMove = move.Item4; // have we been here before? don't check if it's the reverse move if (visited.ContainsKey(nextLocation) && !move.Item4) { if (visited[nextLocation] == -1) { if (moves.Count == 0) { return(visited[oxygen].Value); } continue; } if (visited[nextLocation] > visited[location] + 1) { visited[nextLocation] = visited[location] + 1; } } else { success = true; } } d15.ProvideInput(i); d15.Calculate(); int o = (int)d15.OutputQueue.Dequeue(); // case 1 or 2 we need to add the reverse move, then add any moves using the stepcount+1 switch (o) { case 0: _board[nextLocation] = '#'; visited[nextLocation] = null; break; case 1: case 2: _board[location] = oxygen != null && location == oxygen ? 'O' : '.'; _board[nextLocation] = 'D'; if (visited.ContainsKey(nextLocation)) { if (visited[nextLocation] > visited[location] + 1) { visited[nextLocation] = visited[location] + 1; } } else { visited[nextLocation] = visited[location] + 1; } if (o == 2) { oxygen = nextLocation; } if (!reversedMove) { moves.Push(reverseMove); for (int d = 1; d <= 4; ++d) { if (d != r) { moves.Push(new Tuple <int, int, int, bool>(nextLocation.Item1, nextLocation.Item2, d, false)); } } } location = nextLocation; break; } // OutputBoard(); } return(visited[oxygen].Value); }
public static int DayElevenA(IntCodeCompiler d11) { var panels = PaintMyWagon(d11, 0); return(panels.Where(kvp => kvp.Value > 0).Count()); }