[text] day 2

Viewer

  1. string[] lines = System.IO.File.ReadAllLines(@"C:\Users\chris\source\repos\advent1\advent1\TextFile2.txt");
  2. int total = 0;
  3. static int calculateScore(char opp, char you)
  4. {
  5.     var score = 0;
  6.     score += checkSymbol(you);
  7.     score += checkWin(opp, you);
  8.     if (opp == you) { score += 3; }
  9.     return score;
  10. static int checkWin(char opp, char you)
  11. {
  12.     if (opp == 'A' && you == 'Y' || opp == 'B' && you == 'Z' || opp == 'C' && you == 'X')
  13.     {
  14.         return 6;
  15.     }
  16.     return 0;
  17. }
  18. static int checkSymbol(char input)
  19. {
  20.     if (input == 'X' || input == 'A')
  21.     {
  22.         return 1;
  23.     }
  24.     if (input == 'Y' || input == 'B')
  25.     {
  26.         return 2;
  27.     }
  28.     return 3;
  29. }
  30. //// part 2 ////
  31. static int makeDraw(char opp)
  32. {
  33.     return 3 + checkSymbol(opp);
  34. }
  35. static int makeLoss(char opp)
  36. {
  37.     if (opp == 'A')
  38.     {
  39.         return 3; 
  40.     }
  41.     if (opp == 'B')
  42.     {
  43.         return 1;
  44.     }
  45.         return 2;
  46. }
  47. static int makeWin(char opp)
  48. {
  49.     if (opp == 'A')
  50.     {
  51.         return 8;
  52.     }
  53.     if (opp == 'B')
  54.     {
  55.         return 9;
  56.     }
  57.     return 7;
  58. }
  59.     foreach (var line in lines)
  60.     {
  61.     char opp = Convert.ToChar(line.Substring(0, 1));
  62.     char you = Convert.ToChar(line.Substring(2, 1));
  63.  
  64.     if (you == 'X')
  65.     {
  66.         total += makeLoss(opp);
  67.     }
  68.     else if (you == 'Y')
  69.     {
  70.         total += makeDraw(opp);
  71.     }
  72.     else{
  73.         total += makeWin(opp);
  74.     }
  75.     //total += calculateScore(opp, you);
  76. }
  77. Console.WriteLine(total);

Editor

You can edit this paste and save as new:


File Description
  • day 2
  • Paste Code
  • 02 Dec-2022
  • 1.5 Kb
You can Share it: