private long FindMostEfficientWayToOrganizeBurrow(Burrow burrow) { long energy = Int64.MaxValue; HashSet <Burrow> burrows = new HashSet <Burrow>(); burrows.Add(burrow); while (burrows.Count > 0) { Burrow currentState = burrows.First(); burrows.Remove(currentState); IList <Burrow> nextStates = currentState.GetPossibleNextStates(); foreach (Burrow next in nextStates) { if (next.IsOrganized()) { if (energy > next.Energy) { energy = next.Energy; } } else { if (next.Energy + next.Remaining < energy) { burrows.Add(next); } } } } return(energy); }
private void Start() { if (script == null) { script = GetComponentInParent <Burrow>(); } }
// Update is called once per frame void Update() { // Move towards current target if(currentDestination != null && pos != currentDestination){ if(!rm.Moving){ pos = next_node; if(!occupying){ FindTarget(); } List<Vertex> p = rf.FindPath(pos, currentDestination, wg.GetPathfindingCosts()); if(p != null && p.Count > 1){ next_node = p[1]; rm.Move(wg.VertexToVector3(next_node)); } else { currentDestination = pos; } } } else if(currentDestination != null && target != null && pos == currentDestination && !occupying){ target = wl.OccupyBurrow(pos); if(target != null){ occupying = true; } } else { if(!occupying){ FindTarget(); }else{ UpdateTarget(); } } }
void FindTarget() { Burrow b = wl.GetClosestBurrowOccupy(pos); if(b != null){ currentDestination = b.main_block; target = b; } }
public void Merge(Burrow b) { blocks.AddRange(b.blocks); food = food + b.food; if(food > FoodCapacity()){ food = FoodCapacity(); } rabbits = rabbits + b.rabbits; }
public void EndSleep(Vertex v) { Burrow b = VertexInBurrow(v); if (b != null && b.rabbits > 0) { b.rabbits--; } }
void FindTarget() { Burrow b = wl.GetClosestBurrowOccupy(pos); if (b != null) { currentDestination = b.main_block; target = b; } }
public int DepositFood(Vertex v, int food) { Burrow b = VertexInBurrow(v); if (b != null) { return(b.DepositFood(food)); } return(food); }
public void Merge(Burrow b) { blocks.AddRange(b.blocks); food = food + b.food; if (food > FoodCapacity()) { food = FoodCapacity(); } rabbits = rabbits + b.rabbits; }
public Burrow OccupyBurrow(Vertex v) { Burrow b = VertexInBurrow(v); if (b != null && !ferreted_burrows.Contains(b)) { ferreted_burrows.Add(b); return(b); } return(null); }
public async Task WriteBurrowAsync(Burrow burrow) { _logger.LogDebug("Start writing burrow: {} to cache.", burrow.ToString()); var jsonBurrow = JsonSerializer.Serialize(burrow); await _cache.SetStringAsync(burrow.Id.ToString(), jsonBurrow, new DistributedCacheEntryOptions().SetSlidingExpiration( TimeSpan.FromSeconds(_configuration.SlidingExpiration))); _logger.LogDebug("Finished writing burrow with id {} to cache.", burrow.Id.ToString()); }
public async Task <Burrow> Create() { var now = DateTime.UtcNow; var burrow = new Burrow { Id = Guid.NewGuid(), Create = now, Modified = now }; await _cache.WriteBurrowAsync(burrow); return(burrow); }
public bool EatFood(Vertex v) { Burrow b = VertexInBurrow(v); if (b != null && b.food > 0) { b.food--; return(true); } return(false); }
public bool StartSleep(Vertex v) { Burrow b = VertexInBurrow(v); if (b != null && b.rabbits < b.RabbitCapacity()) { b.rabbits++; return(true); } return(false); }
public Burrow GetClosestBurrowSleep(Vertex v) { float cur_distance = -1f; Burrow closest = null; foreach (Burrow b in burrows) { if ((cur_distance == -1f || Vertex.Distance(b.main_block, v) < cur_distance) && b.rabbits < b.RabbitCapacity() && !ferreted_burrows.Contains(b)) { cur_distance = Vertex.Distance(b.main_block, v); closest = b; } } return(closest); }
public Burrow GetClosestBurrowOccupy(Vertex v) { float cur_distance = -1f; Burrow closest = null; foreach (Burrow b in burrows) { if ((cur_distance == -1f || Vertex.Distance(b.main_block, v) <= cur_distance) && !ferreted_burrows.Contains(b)) { cur_distance = Vertex.Distance(b.main_block, v); closest = b; } } return(closest); }
public async Task <Burrow> Update(Guid guid, Burrow burrow) { var existing = await _repository.ReadBurrowAsync(guid); if (existing.Modified != burrow.Modified) { throw new Exception("burrow was already modified"); } burrow.Modified = DateTime.UtcNow; await _repository.WriteBurrowAsync(burrow); _logger.LogDebug("Burrow {} updated with new config.", guid.ToString()); return(burrow); }
public override void Run() { PrintDayHeader(23); string[] lines = System.IO.File.ReadAllLines(@".\Inputs\day23.txt"); Burrow burrow = new Burrow(lines); long energy = FindMostEfficientWayToOrganizeBurrow(burrow); PrintPart(1, $"{energy}"); Burrow extendedBurrow = new Burrow(lines, new string[] { " #D#C#B#A#", " #D#B#A#C#" }); long energy2 = FindMostEfficientWayToOrganizeBurrow(extendedBurrow); PrintPart(2, $"{energy2}"); }
// Update is called once per frame void Update() { // Move towards current target if (currentDestination != null && pos != currentDestination) { if (!rm.Moving) { pos = next_node; if (!occupying) { FindTarget(); } List <Vertex> p = rf.FindPath(pos, currentDestination, wg.GetPathfindingCosts()); if (p != null && p.Count > 1) { next_node = p[1]; rm.Move(wg.VertexToVector3(next_node)); } else { currentDestination = pos; } } } else if (currentDestination != null && target != null && pos == currentDestination && !occupying) { target = wl.OccupyBurrow(pos); if (target != null) { occupying = true; } } else { if (!occupying) { FindTarget(); } else { UpdateTarget(); } } }
private Burrow MoveToRoom(int pos) { Burrow newState = new Burrow(Energy, Rooms, Hallway); int roomNo = Hallway[pos] - 'A'; int distance = Math.Abs(_roomEntrance[Hallway[pos]] - pos); int i = 0; while (i < Rooms[roomNo].Length && Rooms[roomNo][i] == '.') { distance++; i++; } newState.Rooms[roomNo][i - 1] = Hallway[pos]; newState.Hallway[pos] = '.'; newState.Energy = Energy + distance * _energy[Hallway[pos]]; return(newState); }
private Burrow MoveToHallway(int room, int pos) { Burrow newState = new Burrow(Energy, Rooms, Hallway); char roomType = (char)('A' + room); int roomEntrance = _roomEntrance[roomType]; int distance = Math.Abs(pos - roomEntrance) + 1; int i = 0; while (i < Rooms[room].Length && Rooms[room][i] == '.') { distance++; i++; } newState.Hallway[pos] = newState.Rooms[room][i]; newState.Rooms[room][i] = '.'; newState.Energy = Energy + distance * _energy[Rooms[room][i]]; return(newState); }
private static void FillMtrix(char[,] matrix, Snake snake, List <Burrow> burrows) { for (int r = 0; r < matrix.GetLength(0); r++) { char[] rowData = Console.ReadLine().ToCharArray(); for (int c = 0; c < matrix.GetLength(1); c++) { matrix[r, c] = rowData[c]; if (matrix[r, c] == 'S') { snake.Row = r; snake.Col = c; } if (matrix[r, c] == 'B') { Burrow burrow = new Burrow(r, c); burrows.Add(burrow); } } } }
// How to find burrows // Walk map looking for a tunnel with a tunnel over it and dirt bellow it, then continue right until either one of those stops being true // Check each vertex to make sure it doesn't belong to a burrow, if it does make a new burrow out of what has been found and then merge private void PopulateBurrows() { Vertex cur_point = null; Vertex start_of_burrow = null; List <Vertex> vertex_in_burrow = new List <Vertex>(); Vertex one_above = null; Vertex two_above = null; Vertex one_below = null; Burrow growing = null; for (int h = 2; h < wg.height - 1; h++) { for (int w = 1; w < wg.width - 1; w++) { cur_point = new Vertex(w, h); one_above = new Vertex(w, h - 1); two_above = new Vertex(w, h - 2); one_below = new Vertex(w, h + 1); if (start_of_burrow == null && wg.VertexToType(cur_point) == WorldGen.TUNNEL && wg.VertexToType(one_above) == WorldGen.TUNNEL && wg.VertexToType(two_above) == WorldGen.DIRT && wg.VertexToType(one_below) == WorldGen.DIRT) { // We have found the start of a burrow growing = VertexInBurrow(cur_point); start_of_burrow = cur_point; vertex_in_burrow.Add(cur_point); vertex_in_burrow.Add(one_above); } else if (wg.VertexToType(cur_point) == WorldGen.TUNNEL && wg.VertexToType(one_above) == WorldGen.TUNNEL && wg.VertexToType(two_above) == WorldGen.DIRT && wg.VertexToType(one_below) == WorldGen.DIRT) { if (VertexInBurrow(cur_point) != null) { // We have run into an old burrow, attempt to merge growing = VertexInBurrow(cur_point); vertex_in_burrow.Add(one_above); vertex_in_burrow.Add(cur_point); //growing.Merge(new Burrow(vertex_in_burrow)); } else { vertex_in_burrow.Add(cur_point); vertex_in_burrow.Add(one_above); } } else if (start_of_burrow != null && (wg.VertexToType(cur_point) != WorldGen.TUNNEL || wg.VertexToType(one_above) != WorldGen.TUNNEL || wg.VertexToType(two_above) != WorldGen.DIRT || wg.VertexToType(one_below) != WorldGen.DIRT)) { // We have found the end if (vertex_in_burrow.Count < 4) { // burrow was too small if (growing != null) { burrows.Remove(growing); } } else { if (growing != null) { growing.blocks = vertex_in_burrow; } else { growing = new Burrow(vertex_in_burrow); burrows.Add(growing); } } growing = null; start_of_burrow = null; vertex_in_burrow = new List <Vertex>(); } } } }
public void Interact() { if (Holding == null) { //GameObject ItemDistanceCheck; //ItemDistanceCheck = transform.GetChild(0).GetComponent<InteractHitBox>().GetItem(); if (transform.GetChild(0).GetComponent <InteractHitBox>().Item.Count != 0) { CheckforItemsinHitbox(); } else if (Dispenser != null) { Vector3 SpawnPoint; SpawnPoint = (transform.position + new Vector3(0.2f * Dir, 0.7f)); if (Dispenser.tag == "seeddispenser") { Holding = Instantiate(Dispenser.GetComponent <SeedDispenser>().Seed, SpawnPoint, Quaternion.identity); } if (Dispenser.tag == "waterdispenser") { Holding = Dispenser.GetComponent <WaterPumpScript>().Pump(gameObject, SpawnPoint); //Holding = Instantiate(Dispenser.GetComponent<WaterPumpScript>().WaterBag, SpawnPoint, Quaternion.identity); } if (Dispenser.tag == "fertilizerdispenser") { Holding = Instantiate(Dispenser.GetComponent <FertilizerDispenser>().Fertilizer, SpawnPoint, Quaternion.identity); } } //FOR WHEN THE BURROW HAS AN INCOMPLETE TIMER else if (Burrow != null && Burrow.GetComponent <BurrowInteractTimer>().SeedType != null) { Debug.Log("Working on Planted Burrow"); GameObject BurrowSeedType; BurrowSeedType = Burrow.GetComponent <BurrowInteractTimer>().SeedType; Burrow.GetComponent <BurrowInteractTimer>().TimerStart(gameObject, BurrowSeedType); } } else if (Holding != null) { //TO PLANT SEEDS if (Holding.tag == "seed" && Burrow != null && Burrow.GetComponent <BurrowBehavior>().readyToPlant) { if (transform.GetChild(0).GetComponent <InteractHitBox>().PlantedGhost != null) { Destroy(transform.GetChild(0).GetComponent <InteractHitBox>().PlantedGhost); } PlantSeed(Holding, Burrow); } //TO DROP ITEMS else { GameObject TempHold; TempHold = Holding; //Holding.GetComponent<Rigidbody2D>().isKinematic = false; ThrowTurnOffKinTurnOnCol(); Holding = null; //if(TempHold.GetComponent<SeedScript>().collided == true) // TempHold.transform.position += new Vector3(0, transform.position.y) } } }
void PickDestination() { if (need_sleep && CanGetSleep()) { ready_for_mate = false; // Find closest burrow with sleeping room Burrow b = wl.GetClosestBurrowSleep(mySquare); if (b != null) { currentDestination = b.main_block; if (mySquare == currentDestination && Time.time > last_action + speed) { anim.SetBool("Sleep", true); if (wl.StartSleep(mySquare)) { current_action = "sleeping"; sleeping = true; last_sleep = Time.time; } } else { current_action = "finding sleep"; } } } else if (need_food && CanGetFood()) { ready_for_mate = false; // Find closest burrow with food Burrow b = wl.GetClosestBurrowFood(mySquare); if (b != null) { currentDestination = b.GetRandomBlock(rrand); if (mySquare == currentDestination && Time.time > last_action + speed) { anim.SetTrigger("Dig"); if (wl.EatFood(mySquare)) { current_action = "eating"; hunger = full; need_food = false; } } else { current_action = "finding food"; } } } else if (ready_for_mate && AreThereMales()) { // animation??? anim.SetBool("Mating", true); current_action = "waiting for mate"; } else if (horny && sex == FEMALE && CanGetSleep() && AreThereMales()) { Burrow b = wl.GetClosestBurrowSleep(mySquare); if (b != null) { currentDestination = b.main_block; if (mySquare == currentDestination && Time.time > last_action + speed) { ready_for_mate = true; } else { current_action = "finding mating burrow"; } } } else if (horny && sex == MALE && CanGetFemale() != null) { RabbitLogic female = CanGetFemale(); if (female.mySquare == mySquare) { anim.SetBool("Hump", true); current_action = "mating"; sexing = true; sex_start = Time.time; female.Mate(this); last_mating = Time.time; } else { currentDestination = female.mySquare; current_action = "moving to female"; } //else pick new Destination or work } else { if (profession == "Burrower") { // First try to find a square to dig currentDestination = wl.GetClosestDig(mySquare); if (currentDestination == null) { // If that fails find a square to fill in currentDestination = wl.GetClosestFill(mySquare); if (currentDestination != null) { current_action = "moving to fill"; filling = true; } else { current_action = "idle"; } } else { current_action = "moving to dig"; digging = true; } if (mySquare == currentDestination && Time.time > last_action + speed) { anim.SetTrigger("Dig"); if (digging) { current_action = "digging"; wl.Dig(mySquare, str); digging = false; } else if (filling) { current_action = "filling"; wl.Fill(mySquare, str); filling = false; } } } else if (profession == "Forager") { if (food_hold == 0) { currentDestination = wl.GetClosestFood(mySquare); if (mySquare == currentDestination && Time.time > last_action + speed) { anim.SetTrigger("Dig"); food_hold = wl.TakeFood(mySquare, str); } else if (currentDestination != null) { current_action = "moving to forage"; } else { current_action = "idle"; } } else { //return to closest burrow with space Burrow b = wl.GetClosestBurrowDepositFood(mySquare); if (b != null) { current_action = "returning with food"; currentDestination = b.main_block; if (mySquare == currentDestination && Time.time > last_action + speed) { anim.SetTrigger("Dig"); food_hold = wl.DepositFood(mySquare, food_hold); } } else { current_action = "looking for burrow to store food"; } } } else if (profession == "Guard") { Burrow b = wl.GetClosestBurrowSleep(mySquare); if (b != null) { currentDestination = b.GetRandomBlock(rrand); if (mySquare == currentDestination) { current_action = "cowering"; } else { current_action = "finding cover"; } } } } }
public async Task <IActionResult> PutConfig([FromRoute] Guid guid, [FromBody] Burrow burrowDto) { var burrow = await _updater.Update(guid, burrowDto); return(Ok(burrow)); }
// How to find burrows // Walk map looking for a tunnel with a tunnel over it and dirt bellow it, then continue right until either one of those stops being true // Check each vertex to make sure it doesn't belong to a burrow, if it does make a new burrow out of what has been found and then merge private void PopulateBurrows() { Vertex cur_point = null; Vertex start_of_burrow = null; List<Vertex> vertex_in_burrow = new List<Vertex>(); Vertex one_above = null; Vertex two_above = null; Vertex one_below = null; Burrow growing = null; for(int h = 2; h < wg.height - 1; h++){ for(int w = 1; w < wg.width - 1; w++){ cur_point = new Vertex(w, h); one_above = new Vertex(w, h - 1); two_above = new Vertex(w, h - 2); one_below = new Vertex(w, h + 1); if(start_of_burrow == null && wg.VertexToType(cur_point) == WorldGen.TUNNEL && wg.VertexToType(one_above) == WorldGen.TUNNEL && wg.VertexToType(two_above) == WorldGen.DIRT && wg.VertexToType(one_below) == WorldGen.DIRT){ // We have found the start of a burrow growing = VertexInBurrow(cur_point); start_of_burrow = cur_point; vertex_in_burrow.Add(cur_point); vertex_in_burrow.Add(one_above); } else if(wg.VertexToType(cur_point) == WorldGen.TUNNEL && wg.VertexToType(one_above) == WorldGen.TUNNEL && wg.VertexToType(two_above) == WorldGen.DIRT && wg.VertexToType(one_below) == WorldGen.DIRT){ if(VertexInBurrow(cur_point) != null){ // We have run into an old burrow, attempt to merge growing = VertexInBurrow(cur_point); vertex_in_burrow.Add(one_above); vertex_in_burrow.Add(cur_point); //growing.Merge(new Burrow(vertex_in_burrow)); } else { vertex_in_burrow.Add(cur_point); vertex_in_burrow.Add(one_above); } } else if(start_of_burrow != null && (wg.VertexToType(cur_point) != WorldGen.TUNNEL || wg.VertexToType(one_above) != WorldGen.TUNNEL || wg.VertexToType(two_above) != WorldGen.DIRT || wg.VertexToType(one_below) != WorldGen.DIRT)){ // We have found the end if(vertex_in_burrow.Count < 4){ // burrow was too small if(growing != null){ burrows.Remove(growing); } } else { if(growing != null){ growing.blocks = vertex_in_burrow; } else { growing = new Burrow(vertex_in_burrow); burrows.Add(growing); } } growing = null; start_of_burrow = null; vertex_in_burrow = new List<Vertex>(); } } } }