Advertisement
Spocoman

02. Ad Astra

Nov 28th, 2023 (edited)
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace AdAstra
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Regex regex = new Regex(@"([|#])(?<name>[A-Za-z\s]+)\1(?<date>\d{2}\/\d{2}\/\d{2})\1(?<calories>\d{1,5})\1");
  13.             MatchCollection matches = regex.Matches(Console.ReadLine());
  14.             List<Dictionary<string, string>> products = new List<Dictionary<string, string>>();
  15.             int sumCalories = 0;
  16.  
  17.             foreach (Match match in matches)
  18.             {
  19.                 GroupCollection m = regex.Match(match.ToString()).Groups;
  20.  
  21.                 products.Add(new Dictionary<string, string>()
  22.                 {
  23.                     { "name", m["name"].ToString() },
  24.                     { "date", m["date"].ToString() },
  25.                     { "calories", m["calories"].ToString() }
  26.                 });
  27.  
  28.                 sumCalories += int.Parse(m["calories"].ToString());
  29.             }
  30.  
  31.             Console.WriteLine($"You have food to last you for: {sumCalories / 2000} days!");
  32.  
  33.             foreach (var p in products) {
  34.                 Console.WriteLine($"Item: {p["name"]}, Best before: {p["date"]}, Nutrition: {p["calories"]}");
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement