public override string Part1(string input, object?args) { var m = new MovingIn2D { Face = ECardinals.Up, Map = ParseMap(input), DefaultMapValue = "." }; var infectCount = 0; for (var i = 0; i < (int)args; i++) { if (m.MapValue == ".") { m.Turn(ETurnOrders.Left); m.MapValue = "#"; infectCount++; m.MoveForward(1, false, true); } else if (m.MapValue == "#") { m.Turn(ETurnOrders.Right); m.MapValue = "."; m.MoveForward(1, false, true); } } return(infectCount.ToString()); }
public override string Part1(string input, object?args) { var person = new MovingIn2D(); foreach (var direction in input.Split(',')) { person.Turn(ParseTurnOrders(direction)); person.MoveForward(ParseSteps(direction), false, true); } return(person.DistanceFrom(0, 0).ToString()); }
public override string Part1(string input, object?args) { var sub = new MovingIn2D(); var commands = input.Split("\r\n", StringSplitOptions.RemoveEmptyEntries); foreach (var command in commands) { var(direction, distance) = CommandParser(command); sub.Move(direction, distance, false, true); } return((sub.LocationX * -sub.LocationY).ToString()); }
private (string, string) Move(string input) { var steps = 0; var ret = new StringBuilder(); var p = new MovingIn2D { Map = input.StringTo2ArrayOfArrays("").InvertArray().ToDictionaryOfPointObject(), Face = ECardinals.Down, InvalidDestinationString = " ", MoveAllowed = true, LocationX = input.Lines()[0].IndexOf('|'), LocationY = input.Lines().Length - 1, ThrowOnInvalidMove = true }; while (p.MoveAllowed) { try { p.MoveForward(1, false, false); steps += 1; if (char.IsLetter(p.MapValue[0])) { ret.Append(p.MapValue); } } catch (InvalidOperationException) { if (p.MapValue == "+") { p.Face = (ECardinals)Enum.Parse(typeof(ECardinals), (((int)p.Face + 1) % 4).ToString()); try { p.Peek(); } catch (InvalidOperationException) { p.Face = (ECardinals)Enum.Parse(typeof(ECardinals), (((int)p.Face + 2) % 4).ToString()); } } else { break; } } } return(ret.ToString(), (steps + 1).ToString()); }
public override string Part2(string input, object?args) { var sub = new MovingIn2D(); var commands = input.Split("\r\n", StringSplitOptions.RemoveEmptyEntries); int hor; int ver; int aim = 0; foreach (var command in commands) { (hor, ver, aim) = CommandParser2(command, aim); sub.Move(ECardinals.Right, hor, false, true); sub.Move(ECardinals.Down, ver, false, true); } return((sub.LocationX * -sub.LocationY).ToString()); }
public override string Part2(string input, object?args) { _bDone = false; var person = new MovingIn2D(); person.AlreadyVisited += OnAlreadyVisited; foreach (var direction in input.Split(',')) { person.Turn(ParseTurnOrders(direction)); person.MoveForward(ParseSteps(direction), true, true); if (_bDone) { break; } } return(person.DistanceFrom(0, 0).ToString()); }
public override string Part1(string input, object?args) { var p = new MovingIn2D { LocationX = 1, LocationY = 1, InvalidDestinationString = "#", Map = (Dictionary <Point, object>)args }; var retVal = new StringBuilder(); foreach (var line in input.Lines()) { foreach (var order in line) { p.Move(ParseCardinal(order.ToString()), 1, false, false); } retVal.Append(p.MapValue); } return(retVal.ToString()); }
protected int Part2Result(int input) { _dic = new Dictionary <Point, int>(); var p = new MovingIn2D { Face = ECardinals.Down, LocationX = 0, LocationY = 0 }; var currentNo = 1; _dic.Add(new Point(0, 0), 1); var reps = 0; while (currentNo != input) { for (var i = 0; i < 2; i++) { p.Turn(ETurnOrders.Left); var rep = 0; while (rep <= reps) { p.MoveForward(1, true, true); currentNo = GetCurrentNo(new Point(p.LocationX, p.LocationY)); if (input < currentNo) { return(currentNo); } _dic.Add(new Point(p.LocationX, p.LocationY), currentNo); rep++; } } reps++; } return(currentNo); }
private void OnAlreadyVisited(MovingIn2D sender, int x, int y) { _bDone = true; sender.MoveAllowed = false; }