private int LoSPeopleAround(IntVector2 position) { int count = 0; foreach (IntVector2 direction in IntVector2.CardinalDirectionsIncludingDiagonals) { IntVector2 testPos = position; bool found; do { found = false; testPos = IntVector2.Add(testPos, direction); if (testPos.X < 0 || testPos.X >= width || testPos.Y < 0 || testPos.Y >= height) { break; } if (seats.ContainsKey(testPos)) { found = true; } if (IsPerson(testPos)) { count++; } }while (!found); } return(count); }
public static string B(string input) { List <string> inputList = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList(); IntVector2 position = new IntVector2(0, 0); IntVector2 waypoint = new IntVector2(10, -1); foreach (string line in inputList) { char direction = line[0]; int magnitude = Convert.ToInt32(line.Substring(1)); switch (direction) { case 'N': waypoint = waypoint.North(magnitude); break; case 'E': waypoint = waypoint.East(magnitude); break; case 'S': waypoint = waypoint.South(magnitude); break; case 'W': waypoint = waypoint.West(magnitude); break; case 'F': position = position.Add(waypoint.Multiply(magnitude)); break; case 'L': while (magnitude > 0) { waypoint = waypoint.Left(); magnitude -= 90; } break; case 'R': while (magnitude > 0) { waypoint = waypoint.Right(); magnitude -= 90; } break; default: throw new Exception("Invalid direction: " + direction); } //Console.WriteLine("Position: " + position + ", Direction: " + facing); } return(position.Distance(new IntVector2(0, 0)).ToString());; }
private int PeopleAround(IntVector2 position) { int count = 0; foreach (IntVector2 direction in IntVector2.CardinalDirectionsIncludingDiagonals) { IntVector2 testPos = IntVector2.Add(position, direction); if (IsPerson(testPos)) { count++; } } return(count); }
public static string B(string input) { HashSet <IntVector2> floor = ParseInput(input); IntVector2[] hexagonalDirections = new IntVector2[6] { new IntVector2(1, 0), new IntVector2(0, 1), new IntVector2(-1, 1), new IntVector2(-1, 0), new IntVector2(0, -1), new IntVector2(1, -1) }; for (int i = 0; i < 100; i++) { Dictionary <IntVector2, int> floorCounter = new Dictionary <IntVector2, int>(); foreach (IntVector2 tile in floor) { foreach (IntVector2 direction in hexagonalDirections) { IntVector2 newLocation = IntVector2.Add(tile, direction); if (!floorCounter.ContainsKey(newLocation)) { floorCounter.Add(newLocation, 0); } floorCounter[newLocation]++; } } HashSet <IntVector2> newFloor = new HashSet <IntVector2>(floor); HashSet <IntVector2> tilesToDo = new HashSet <IntVector2>(floor.Union(floorCounter.Keys)); foreach (IntVector2 location in tilesToDo) { int count = 0; floorCounter.TryGetValue(location, out count); if (floor.Contains(location) && (count == 0 || count > 2)) { newFloor.Remove(location); } else if (!floor.Contains(location) && count == 2) { newFloor.Add(location); } } floor = newFloor; } return(floor.Count.ToString()); }
private static int Slide(Dictionary <IntVector2, TTree> map, IntVector2 velocityVector, int maxX, int maxY) { IntVector2 location = new IntVector2(0, 0); int count = 0; while (location.Y <= maxY) { if (map[location] == TTree.TREE) { count++; } location = IntVector2.Add(location, velocityVector); location.X = location.X % (maxX + 1); if (location.X > maxX) { location.X -= (maxX + 1); } } return(count); }
internal IntVector2 Add(IntVector2 vector) { return(IntVector2.Add(this, vector)); }
private static void LockSectors() { // Arbitrarily set the first freeSector to the centre lockedSectors = new Dictionary <IntVector2, SatSector>(); SatSector first = freeSectors.First(); IntVector2 origin = new IntVector2(0, 0); lockedSectors.Add(origin, first); freeSectors.Remove(first); Queue <IntVector2> toDoQueue = new Queue <IntVector2>(); foreach (IntVector2 direction in IntVector2.CardinalDirections) { toDoQueue.Enqueue(origin.Add(direction)); } while (toDoQueue.Count > 0) { IntVector2 toDo = toDoQueue.Dequeue(); // Find a tile it's next to string edgeToMatch; IntVector2 matchingFrom = null; Direction matchingFromDirection = Direction.South; Direction toDoDirection = Direction.North; for (int i = 0; i < 4; i++) { IntVector2 linkedLocation = toDo.Add(IntVector2.CardinalDirections[i]); if (lockedSectors.ContainsKey(linkedLocation)) { matchingFrom = linkedLocation; toDoDirection = (Direction)i; switch (toDoDirection) { case Direction.North: matchingFromDirection = Direction.South; break; case Direction.East: matchingFromDirection = Direction.West; break; case Direction.South: matchingFromDirection = Direction.North; break; case Direction.West: matchingFromDirection = Direction.East; break; } } } edgeToMatch = lockedSectors[matchingFrom].GetEdge(matchingFromDirection); SatSector matchingSector = null; foreach (SatSector sector in freeSectors) { if (sector.HasEdge(edgeToMatch)) { matchingSector = sector; break; } } // No match. Break out to the next queued location if (matchingSector != null) { // We have a match. Rotate the matchingSector till it lines up with the edgeToMatch matchingSector.RotateTillMatch(edgeToMatch, toDoDirection); // Now fix it in place, and add the surrounding locations to the queue lockedSectors.Add(toDo, matchingSector); freeSectors.Remove(matchingSector); foreach (IntVector2 direction in IntVector2.CardinalDirections) { IntVector2 newLocation = toDo.Add(direction); if (!lockedSectors.ContainsKey(newLocation) && !toDoQueue.Contains(newLocation)) { toDoQueue.Enqueue(newLocation); } } } } }