dragonbs

Ad Astra

Mar 27th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         string input = Console.ReadLine();
  10.         int totalCalories = 0;
  11.         StringBuilder output = new StringBuilder();
  12.  
  13.         string regex = @"([|#])(?<name>[A-Za-z\s]+)\1(?<date>\d{2}[\/]\d{2}[\/]\d{2})\1(?<calories>\d+)\1";
  14.         MatchCollection matches = Regex.Matches(input, regex);
  15.         for (int i = 0; i < matches.Count; i++)
  16.         {
  17.             string name = matches[i].Groups["name"].Value;
  18.             string date = matches[i].Groups["date"].Value;
  19.             int calories = int.Parse(matches[i].Groups["calories"].Value);
  20.             totalCalories += calories;
  21.  
  22.             output.Append($"Item: {name}, Best before: {date}, Nutrition: {calories}\n");
  23.         }
  24.         int days = totalCalories / 2000;
  25.         Console.WriteLine($"You have food to last you for: {days} days!");
  26.         Console.WriteLine(output);
  27.     }
  28. }
Add Comment
Please, Sign In to add comment