Created
June 15, 2022 20:49
-
-
Save nakov/0fce1683471f56e83281f43fa61c169a to your computer and use it in GitHub Desktop.
Meal Plan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Drones | |
{ | |
public class Airfield | |
{ | |
public List<Drone> Drones { get; set; } | |
public string Name { get; set; } | |
public int Capacity { get; set; } | |
public double LandingStrip { get; set; } | |
public Airfield(string name, int capacity, double landingStrip) | |
{ | |
this.Name = name; | |
this.Capacity = capacity; | |
this.LandingStrip = landingStrip; | |
this.Drones = new List<Drone>(); | |
} | |
public int Count => this.Drones.Count(d => d.Available == true); | |
public string AddDrone(Drone drone) | |
{ | |
if (string.IsNullOrEmpty(drone.Name) || | |
string.IsNullOrEmpty(drone.Brand) || | |
drone.Range < 5 || drone.Range > 15) | |
{ | |
return "Invalid drone."; | |
} | |
if (this.Drones.Count >= this.Capacity) | |
{ | |
return "Airfield is full."; | |
} | |
this.Drones.Add(drone); | |
return $"Successfully added {drone.Name} to the airfield."; | |
} | |
public bool RemoveDrone(string name) | |
{ | |
int count = this.Drones.RemoveAll(d => d.Name == name); | |
return count > 0; | |
//bool found = false; | |
//var newDrones = new List<Drone>(); | |
//foreach (var d in this.Drones) | |
//{ | |
// if (d.Name != name) | |
// newDrones.Add(d); | |
// found = true; | |
//} | |
//this.Drones = newDrones; | |
//return found; | |
} | |
public int RemoveDroneByBrand(string brand) | |
{ | |
int count = this.Drones.RemoveAll(d => d.Brand == brand); | |
return count; | |
//int count = 0; | |
//var newDrones = new List<Drone>(); | |
//foreach (var d in this.Drones) | |
//{ | |
// if (d.Brand != brand) | |
// newDrones.Add(d); | |
// count++; | |
//} | |
//this.Drones = newDrones; | |
//return found; | |
} | |
public Drone FlyDrone(string name) | |
{ | |
Drone drone = this.Drones.FirstOrDefault(d => d.Name == name); | |
if (drone == null) | |
return null; | |
drone.Available = false; | |
return drone; | |
} | |
public List<Drone> FlyDronesByRange(int range) | |
{ | |
List<Drone> matchingDrones = | |
this.Drones.Where(d => d.Range >= range).ToList(); | |
foreach (var d in matchingDrones) | |
{ | |
d.Available = false; | |
} | |
return matchingDrones; | |
} | |
public string Report() | |
{ | |
var dronesAvailable = this.Drones.Where(d => d.Available == true); | |
return | |
$"Drones available at {this.Name}:" + Environment.NewLine + | |
string.Join(Environment.NewLine, dronesAvailable); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class BeaversAtWork | |
{ | |
static void Main() | |
{ | |
// Read the matrix | |
int n = int.Parse(Console.ReadLine()); | |
char[,] matrix = ReadMatrix(n); | |
(int row, int col) = FindBeaberLocation(matrix); | |
int totalBranchesCount = CalcTotalBranchesCount(matrix); | |
// Process the input commands | |
Stack<char> branches = new Stack<char>(); | |
int branchesLeft = totalBranchesCount; | |
int branchesOnTheBeach = 0; | |
while (true) | |
{ | |
if (branchesLeft == 0) | |
break; | |
string command = Console.ReadLine(); | |
if (command == "end") | |
break; | |
if (command == "up") | |
MoveTo(row - 1, col, command); | |
else if (command == "down") | |
MoveTo(row + 1, col, command); | |
else if (command == "left") | |
MoveTo(row, col - 1, command); | |
else if (command == "right") | |
MoveTo(row, col + 1, command); | |
else | |
throw new Exception("Invalid command: " + command); | |
} | |
if (branchesLeft == 0) | |
{ | |
string branchesList = string.Join(", ", branches.Reverse()); | |
Console.WriteLine($"The Beaver successfully collect {branches.Count} wood branches: {branchesList}."); | |
} | |
else | |
{ | |
Console.WriteLine($"The Beaver failed to collect every wood branch. There are {branchesLeft} branches left."); | |
} | |
PrintMatrix(matrix); | |
void MoveTo(int newRow, int newCol, string dir) | |
{ | |
// Outside of the pond | |
if (newRow < 0 || newRow >= n || | |
newCol < 0 || newCol >= n) | |
{ | |
// Move branch outside of the pond --> throw the last branch (if exists) | |
if (branches.Count > 0) | |
{ | |
branches.Pop(); | |
branchesOnTheBeach++; | |
} | |
return; | |
} | |
// On a branch | |
if (char.IsLower(matrix[newRow, newCol])) | |
{ | |
// Collect the branch | |
branches.Push(matrix[newRow, newCol]); | |
branchesLeft--; | |
matrix[row, col] = '-'; | |
matrix[newRow, newCol] = 'B'; | |
row = newRow; | |
col = newCol; | |
return; | |
} | |
// On a fish | |
if (matrix[newRow, newCol] == 'F') | |
{ | |
// Eat the fish, move to new location (and collect the branch if any) | |
matrix[newRow, newCol] = '-'; // Remove the fish | |
matrix[row, col] = '-'; // Remove the beaver | |
if (dir == "up") | |
{ | |
if (newRow != 0) | |
newRow = 0; | |
else | |
newRow = n-1; | |
} | |
if (dir == "down") | |
{ | |
if (newRow != n-1 ) | |
newRow = n-1; | |
else | |
newRow = 0; | |
} | |
if (dir == "left") | |
{ | |
if (newCol != 0) | |
newCol = 0; | |
else | |
newCol = n-1; | |
} | |
if (dir == "right") | |
{ | |
if (newCol != n - 1) | |
newCol = n-1; | |
else | |
newCol = 0; | |
} | |
if (char.IsLower(matrix[newRow, newCol])) | |
{ | |
// Collect the branch | |
branches.Push(matrix[newRow, newCol]); | |
branchesLeft--; | |
} | |
matrix[newRow, newCol] = 'B'; | |
row = newRow; | |
col = newCol; | |
return; | |
} | |
// Otherwise | |
//if (matrix[newRow, newCol] == '-') | |
{ | |
// Move to the new position | |
matrix[row, col] = '-'; | |
matrix[newRow, newCol] = 'B'; | |
row = newRow; | |
col = newCol; | |
return; | |
} | |
} | |
} | |
static char[,] ReadMatrix(int n) | |
{ | |
var matrix = new char[n, n]; | |
for (int row = 0; row < n; row++) | |
{ | |
string[] rowChars = Console.ReadLine().Split(" ").ToArray(); | |
for (int col = 0; col < n; col++) | |
matrix[row, col] = rowChars[col][0]; | |
} | |
return matrix; | |
} | |
static (int row, int col) FindBeaberLocation(char[,] matrix) | |
{ | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
for (int col = 0; col < matrix.GetLength(1); col++) | |
{ | |
if (matrix[row, col] == 'B') | |
return (row, col); | |
} | |
} | |
throw new Exception("Bearber not found in the matrix!"); | |
} | |
static int CalcTotalBranchesCount(char[,] matrix) | |
{ | |
int branchesCount = 0; | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
for (int col = 0; col < matrix.GetLength(1); col++) | |
{ | |
if (char.IsLower(matrix[row, col])) | |
branchesCount++; | |
} | |
} | |
return branchesCount; | |
} | |
static void PrintMatrix(char[,] matrix) | |
{ | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
for (int col = 0; col < matrix.GetLength(1); col++) | |
{ | |
if (col > 0) | |
Console.Write(" "); | |
Console.Write(matrix[row, col]); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Drones | |
{ | |
public class Drone | |
{ | |
public string Name { get; set; } | |
public string Brand { get; set; } | |
public int Range { get; set; } | |
public bool Available { get; set; } = true; | |
public Drone(string name, string brand, int range) | |
{ | |
Name = name; | |
Brand = brand; | |
Range = range; | |
} | |
public override string ToString() | |
{ | |
return | |
$"Drone: {this.Name}" + Environment.NewLine + | |
$"Manufactured by: {this.Brand}" + Environment.NewLine + | |
$"Range: {this.Range} kilometers"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class MealPlan | |
{ | |
static void Main() | |
{ | |
Dictionary<string, int> caloriesByMeal = new Dictionary<string, int>() | |
{ | |
{ "salad", 350 }, | |
{ "soup", 490 }, | |
{ "pasta", 680 }, | |
{ "steak", 790 }, | |
}; | |
string[] meals = Console.ReadLine().Split(" "); | |
int mealsSum = meals.Select(m => caloriesByMeal[m]).Sum(); | |
Stack<int> daysStack = | |
new Stack<int>(Console.ReadLine().Split(" ").Select(int.Parse)); | |
int daysSum = daysStack.Sum(); | |
for (int mealIndex = 0; mealIndex < meals.Length; mealIndex++) | |
{ | |
string meal = meals[mealIndex]; | |
int mealCals = caloriesByMeal[meal]; | |
var dayCals = daysStack.Pop(); | |
int calsLeft = dayCals - mealCals; | |
if (calsLeft > 0) | |
daysStack.Push(calsLeft); | |
else if (calsLeft < 0) | |
{ | |
if (daysStack.Count == 0) | |
{ | |
// All limits are eaten --> print the left meals | |
Console.WriteLine($"John ate enough, he had {mealIndex + 1} meals."); | |
var mealsLeft = new List<string>(); | |
for (int i = mealIndex + 1; i < meals.Length; i++) | |
{ | |
mealsLeft.Add(meals[i]); | |
} | |
Console.WriteLine($"Meals left: {string.Join(", ", mealsLeft)}."); | |
return; | |
} | |
var prevDayCals = daysStack.Pop(); | |
calsLeft = prevDayCals + calsLeft; | |
daysStack.Push(calsLeft); | |
} | |
} | |
// All meals are eaten --> print the days left | |
Console.WriteLine($"John had {meals.Length} meals."); | |
var daysLeft = string.Join(", ", daysStack); | |
Console.WriteLine($"For the next few days, he can eat {daysLeft} calories."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment