private void WalkRobot(long[] inputInt, IDictionary <int, IDictionary <int, int> > map) { int x = 0; int y = 0; char orientation = 'N'; long[] output = new long[2]; IntCode intCode = new IntCode(inputInt, new long[0]); bool done = false; while (true) { int inputColor = map.ContainsKey(x) && map[x].ContainsKey(y) ? map[x][y] : 0; intCode.SetNewReadBuffer(new long[1] { inputColor }); for (int index = 0; index < 2; index++) { output[index] = intCode.RunProgram(); if (output[index] == 99) { done = true; break; } } if (done) { break; } if (!map.ContainsKey(x)) { map[x] = new Dictionary <int, int>(); } map[x][y] = (int)output[0]; orientation = GetNewOrientation(orientation, output); GetNewPosition(orientation, ref x, ref y); } }
public void Task2() { long[] inputInt = ReadAndParse(); long max = long.MinValue; IntCode intCode1 = new IntCode(inputInt, new long[0]); IntCode intCode2 = new IntCode(inputInt, new long[0]); IntCode intCode3 = new IntCode(inputInt, new long[0]); IntCode intCode4 = new IntCode(inputInt, new long[0]); IntCode intCode5 = new IntCode(inputInt, new long[0]); for (int firstPhase = 5; firstPhase < 10; firstPhase++) { for (int secondPhase = 5; secondPhase < 10; secondPhase++) { if (secondPhase == firstPhase) { continue; } for (int thirdPhase = 5; thirdPhase < 10; thirdPhase++) { if (secondPhase == thirdPhase || thirdPhase == firstPhase) { continue; } for (int forthPhase = 5; forthPhase < 10; forthPhase++) { if (forthPhase == thirdPhase || forthPhase == secondPhase || forthPhase == firstPhase) { continue; } for (int fifthPhase = 5; fifthPhase < 10; fifthPhase++) { if (forthPhase == fifthPhase || fifthPhase == thirdPhase || fifthPhase == secondPhase || fifthPhase == firstPhase) { continue; } long result = 0; long finalResult = 0; bool firstRun = true; while (true) { intCode1.SetNewReadBuffer(firstRun ? new long[2] { firstPhase, result } : new long[1] { result }); result = intCode1.RunProgram(); if (result == 99) { break; } intCode2.SetNewReadBuffer(firstRun ? new long[2] { secondPhase, result } : new long[1] { result }); result = intCode2.RunProgram(); if (result == 99) { break; } intCode3.SetNewReadBuffer(firstRun ? new long[2] { thirdPhase, result } : new long[1] { result }); result = intCode3.RunProgram(); if (result == 99) { break; } intCode4.SetNewReadBuffer(firstRun ? new long[2] { forthPhase, result } : new long[1] { result }); result = intCode4.RunProgram(); if (result == 99) { break; } intCode5.SetNewReadBuffer(firstRun ? new long[2] { fifthPhase, result } : new long[1] { result }); result = intCode5.RunProgram(); if (result == 99) { break; } finalResult = result; firstRun = false; } if (max < finalResult) { max = finalResult; } intCode1.Reset(); intCode2.Reset(); intCode3.Reset(); intCode4.Reset(); intCode5.Reset(); } } } } } //1047153 Console.WriteLine("Day 7 task 2 : " + max); }
public void Task1() { long[] inputInt = ReadAndParse(); long max = int.MinValue; IntCode intCode = new IntCode(inputInt, new long[0]); for (int firstPhase = 0; firstPhase < 5; firstPhase++) { for (int secondPhase = 0; secondPhase < 5; secondPhase++) { if (secondPhase == firstPhase) { continue; } for (int thirdPhase = 0; thirdPhase < 5; thirdPhase++) { if (secondPhase == thirdPhase || thirdPhase == firstPhase) { continue; } for (int forthPhase = 0; forthPhase < 5; forthPhase++) { if (forthPhase == thirdPhase || forthPhase == secondPhase || forthPhase == firstPhase) { continue; } for (int fifthPhase = 0; fifthPhase < 5; fifthPhase++) { if (forthPhase == fifthPhase || fifthPhase == thirdPhase || fifthPhase == secondPhase || fifthPhase == firstPhase) { continue; } intCode.Reset(); intCode.SetNewReadBuffer(new long[2] { firstPhase, 0 }); long result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] { secondPhase, result }); result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] { thirdPhase, result }); result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] { forthPhase, result }); result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] { fifthPhase, result }); result = intCode.RunProgram(); if (max < result) { max = result; } } } } } } //17406 Console.WriteLine("Day 7 task 1 : " + max); }
private (long, IntCode) MoveRobot(IntCode intCode, int[] moves, ref bool end) { int[] movesNew = new int[moves.Length + 1]; for (int index = 0; index < moves.Length; index++) { movesNew[index] = moves[index]; } var returnValue = (0L, intCode); if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 2) { IntCode intCodeCopy = intCode.DeepCopy(); intCode.SetNewReadBuffer(new long[1] { 1 }); long returnCode = intCode.RunProgram(); if (returnCode == 1) { movesNew[moves.Length] = 1; returnValue = MoveRobot(intCode, movesNew, ref end); if (!end) { intCode = intCodeCopy; } } else if (returnCode == 2) { end = true; return(movesNew.Length, intCode.DeepCopy()); } } if (end) { return(returnValue); } if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 1) { IntCode intCodeCopy = intCode.DeepCopy(); intCode.SetNewReadBuffer(new long[1] { 2 }); long returnCode = intCode.RunProgram(); if (returnCode == 1) { movesNew[moves.Length] = 2; returnValue = MoveRobot(intCode, movesNew, ref end); if (!end) { intCode = intCodeCopy; } } else if (returnCode == 2) { end = true; return(movesNew.Length, intCode.DeepCopy()); } } if (end) { return(returnValue); } if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 4) { IntCode intCodeCopy = intCode.DeepCopy(); intCode.SetNewReadBuffer(new long[1] { 3 }); long returnCode = intCode.RunProgram(); if (returnCode == 1) { movesNew[moves.Length] = 3; returnValue = MoveRobot(intCode, movesNew, ref end); if (!end) { intCode = intCodeCopy; } } else if (returnCode == 2) { end = true; return(movesNew.Length, intCode.DeepCopy()); } } if (end) { return(returnValue); } if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 3) { IntCode intCodeCopy = intCode.DeepCopy(); intCode.SetNewReadBuffer(new long[1] { 4 }); long returnCode = intCode.RunProgram(); if (returnCode == 1) { movesNew[moves.Length] = 4; returnValue = MoveRobot(intCode, movesNew, ref end); //if (!end) { intCode = intCodeCopy; } } else if (returnCode == 2) { end = true; return(movesNew.Length, intCode.DeepCopy()); } } return(returnValue); }
private long MoveOxygen(IntCode intCode) { Point point = new Point() { X = 0, Y = 0 }; IDictionary <Point, IntCode> PointState = new Dictionary <Point, IntCode>(); PointState[point] = intCode; int count = 0; while (true) { bool canMove = false; IDictionary <Point, IntCode> PointStateNew = new Dictionary <Point, IntCode>(); foreach (Point pointInitial in PointState.Keys) { Point newPointNorth = new Point() { X = pointInitial.X, Y = pointInitial.Y + 1 }; if (!PointState.ContainsKey(newPointNorth)) { IntCode intCodeCopy = PointState[pointInitial].DeepCopy(); intCodeCopy.SetNewReadBuffer(new long[1] { 1 }); long returnCode = intCodeCopy.RunProgram(); if (returnCode == 1) { PointStateNew[newPointNorth] = intCodeCopy; canMove = true; } } Point newPointSouth = new Point() { X = pointInitial.X, Y = pointInitial.Y - 1 }; if (!PointState.ContainsKey(newPointSouth) && !PointStateNew.ContainsKey(newPointSouth)) { IntCode intCodeCopy = PointState[pointInitial].DeepCopy(); intCodeCopy.SetNewReadBuffer(new long[1] { 2 }); long returnCode = intCodeCopy.RunProgram(); if (returnCode == 1) { PointStateNew[newPointSouth] = intCodeCopy; canMove = true; } } Point newPointEast = new Point() { X = pointInitial.X + 1, Y = pointInitial.Y }; if (!PointState.ContainsKey(newPointEast) && !PointStateNew.ContainsKey(newPointEast)) { IntCode intCodeCopy = PointState[pointInitial].DeepCopy(); intCodeCopy.SetNewReadBuffer(new long[1] { 3 }); long returnCode = intCodeCopy.RunProgram(); if (returnCode == 1) { PointStateNew[newPointEast] = intCodeCopy; canMove = true; } } Point newPointWest = new Point() { X = pointInitial.X - 1, Y = pointInitial.Y }; if (!PointState.ContainsKey(newPointWest) && !PointStateNew.ContainsKey(newPointWest)) { IntCode intCodeCopy = PointState[pointInitial].DeepCopy(); intCodeCopy.SetNewReadBuffer(new long[1] { 4 }); long returnCode = intCodeCopy.RunProgram(); if (returnCode == 1) { PointStateNew[newPointWest] = intCodeCopy; canMove = true; } } } if (canMove) { count++; foreach (Point pointNew in PointStateNew.Keys) { PointState[pointNew] = PointStateNew[pointNew]; } } else { break; } } return(count); }